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,290
  • 8
  • 59
  • 73

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
  • 33,712
  • 16
  • 72
  • 110
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,070
  • 8
  • 41
  • 62
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
  • 6,935
  • 5
  • 38
  • 64
0

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

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

Shriram
  • 4,244
  • 8
  • 30
  • 62