0

I'm reading through the Oracle doc, but I don't get something.

Suppose I have

public interface a {
    //some methods
}
public class b implements a {
    //some methods
}

What would be the difference between this:

a asd=new b();

and this:

b asf=new b();
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75

4 Answers4

3

There is no such thing. At least not compile.

a asd=new a(); // you can't instantiate an interface

and

b asf=new a(); // you can't instantiate an interface

You can do followings.

b asd=new b();  

and

a asd=new b(); 
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

let say we have class b and interface a:

public interface a{
  void foo();
}
public class b implements a{
  @Override
  void foo(){}
  void bar(){}
}

then we create two instances:

a asd=new b();
b asf=new b();

now asdis instance of 'a' so it can access only methods declared in interface a (in this case method foo)

asf from other hand is instance of b so it can access both method defined in class b

user902383
  • 8,420
  • 8
  • 43
  • 63
0
  1. you cannot instantiate an interface

  2. if we suppose you mean a parent class, in the first case you cannot the access method(s) of class b

Suppose you have these classes:

public class a {
//some methods
}
public class b extends a {
//some methods
    type method1(args) {...}
}

What would be the difference between?

a asd=new b(); // you cannot call method1 using asd

and

b asf=new b();
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
0

a asd = new b() -- can be resolved at runtime

b bsd = new b() -- can be resolved at compile time.

Shriram
  • 4,343
  • 8
  • 37
  • 64