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();
3
  • Second one does not compile Commented Apr 9, 2014 at 12:41
  • You need to clarify your understanding of interface Commented Apr 9, 2014 at 12:43
  • See also stackoverflow.com/questions/3383726/… Commented Apr 9, 2014 at 23:50

4 Answers 4

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(); 
Sign up to request clarification or add additional context in comments.

1 Comment

I meant b, excuse me.
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

Comments

0

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

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

Comments

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();

Comments