Reputation: 3390
ClassA
extends Object
. Given the legal instantiations of:
InterfaceB myGuy = new ClassA();
and
ClassA myGuy = new ClassA();
What would the first line of the class declaration for ClassA
likely be?
FYI, my last attempted answer was - "public class classA implements interfaceB {...}"
Yes, this is a homework question from a java noob. I would appreciate a good point in the right direction. Adolescent negative commentary can be kept to yourself...thank you.
Upvotes: 1
Views: 196
Reputation:
It means there is
InterfaceB interfaceB{};
and ClassA
implemented it;
ClassA imeplements interfaceB {}
so you can init. InterfaceB like
InterfaceB interfaceB = new ClassA ();
you know the second one
Upvotes: 0
Reputation: 12545
The clue is obviously the name in the first of the two legal instantiations: InterfaceB
.
When you declare a variable, you can instantiate (with the new
keyword) any type of object that is derived from (by subclassing) or implements (from an interface) the declared type.
So InterfaceB
is either an interface implemented by ClassA
or a superclass of ClassA
.
Upvotes: 1
Reputation: 33078
You know that this line is legal:
InterfaceB myGuy = new ClassA();
In order for that assignment to be legal, ClassA
has to extend or implement InterfaceB
. You know that ClassA
extends Object
, so it can't extend InterfaceB
. So InterfaceB
must be an interface instead of a class.
The first line of the interface declaration for InterfaceB
is:
public interface InterfaceB {
So what would the first line of the class declaration for ClassA
be?
Upvotes: 1