thefonso
thefonso

Reputation: 3390

How do I answer this homework question about a class declaration in Java?

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

Answers (3)

user467871
user467871

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

Jonathon Faust
Jonathon Faust

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

Erick Robertson
Erick Robertson

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

Related Questions