0

Is the below statement correct ? A extends B is correct if both A and B are either interfaces or classes

How can it be true ? If A is an Interface and B is a class then Interface cannot inherit a class. Or can we ? If so, can you give some supporting examples please.

1
  • 1
    "... if both A and B ..." Commented Sep 15, 2020 at 2:12

3 Answers 3

1

Depending on context, 'extends' is used for the general notion of 'type X is a subtype of type Y'.

For example, given:

interface INTERFACE {} class CLASS implements INTERFACE {} 

Then it is certainly correct to say that CLASS implements INTERFACE.

Depending on context, one might say that CLASS extends INTERFACE.

For example, INTERFACE x = new CLASS(); would be legal, which shows that in the end, CLASS is a subtype of INTERFACE here.

As a second example, in generics bounds, the keyword extends is used to indicate subtype relationships, which may be fulfilled with interfaces. For example, this is real java code:

interface INTERFACE {} class CLASS implements INTERFACE {} List<? extends INTERFACE> someList; someList = new ArrayList<CLASS>(); 

Note, extends is used here. Had you written List<? implements INTERFACE>, that'd be a compile time error.

If this is some sort of homework question, the problem with terminology is that there is no Judge Dredd.

There's nobody out there with a book of terms in hand who will slap you into next week if you get it wrong. If someone decides to say in casual conversation that, say, ArrayList extends List (note that ArrayList is a class, List is an interface), it's not particularly useful to talk about right and wrong. The point of opening your mouth and making air pass through your vocal cords is to communicate; to convey notions and ideas.

If the point of the exercise of opening your yap was to convey the notion that ArrayList is a subtype of List and will suffice for any and all places where code needs a List, well, I'd personally say trying to shorten that by saying 'ArrayList extends List' is fair game... - and, insofar as the point is to convey that ArrayList is a subtype, it is entirely correct.

If you decide to hear 'if I write class ArrayList extends List {}, then it'll compile' - okay, then you have a classic issue of bad communication. The talker intended one thing, you understood something else. Happens all the time. Being more careful with word choices and steering closer to the language spec can help, but as I showed already, java itself uses extends in certain contexts to mean 'subtype of', and in other contexts, specifically it means only 'this is my parent class', and these concepts are not quite the same.

Sign up to request clarification or add additional context in comments.

Comments

1

In this case these are the possible outputs I guess -

  1. A extends B (if both are interfaces)
  2. A extends B (If both are classes)

In case of no 1, An interface can extend another interface without any problem.

The rest is answered already by @rzwitserloot.

Comments

0

An interface can extend other interfaces.

A class implements more than 1 interface.

A class extends only 1 class.

An interface can't extend a class

we use the interface concept in java because if we want to use multiple inheritances in java. Until Java7 we can't write a non-abstract method in the interface but from java8 we can write a non-abstract method using default and static keyword.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.