3

I have a question of how to relate interface to inheritance, for example:

if class A implements interface X, also class A is the superclass of class B and class C, my question is , does it mean both class B and class c will automatically implement interface X as well?

2
  • 5
    Yes. Polymorphism means that B and C can be used in place of A, so if A implements X, B and C must also (implicitly). Commented Oct 30, 2013 at 12:12
  • Think about it intuitively. If I make a class MySpecialHashMap extends HashMap would you expect that class to be a Map? Of course you would, because HashMap is a Map. Commented Oct 30, 2013 at 12:29

6 Answers 6

4

It depends if class A is an abstract class or not. Assuming that class A fully implements interface X then both B and C will implement X. For example each line of code would be perfectly fine...

X one = new A(); X two = new B(); X three = new C(); 
Sign up to request clarification or add additional context in comments.

Comments

3

if class A implements interface X, also class A is the superclass of class B and class C, my question is , does it mean both class B and class c will automatically implement interface X as well?

Not if A has an implementation. Otherwise, yes they need to.

public interface X{ public void stuff();} public class A implements X { // A not abstract, must implement "stuff()" // therefore, B and C will automatically have an implementation of "stuff()" too } 

but if A is abstract

public abstract A implements X { // not implemented. B and C will need to implement public abstract void stuff(); // not implemented } 

1 Comment

it's customary to up-vote if you like the answer. ;)
0

no not necessary to implement the functions in the superclass

suppose if the

interface X(){ function A(); function B(); } class A implements X(){ @override function A(){ //do something } function B(){ //do something } } class B extends A{ } class C extends A{ } //-> that time your subclass need to implement the functions. 

6 Comments

Hi Nambi, thanks for your post,but my question is actually about both class B and class C extend class A, Class A implements interface X, so do class B and Class C implement interface X automatically?
@peiwang : Jeff Richley answers your question above (correctly too, as opposed to Ruchira and Nambi).
So both class B and class C implement function A and B right? is it compulsory for class B and C to include function A and B ?
@peiwang if your superclass A extends that interface x then your subClass B and C necessary to implements that function.
@peiwang Both B and C inherit methods A() and B() (assuming they are not abstract in class A) and so both class B and C implement X.
|
0

If X is the interface and A the implementation(not an abstract class). Irrespective of any other class down the hierarchy, Class A needs to implement all its methods.

If X is an abstract class, then class B and class C both should provide the implementation of the methods which are not provided in Class A.

In both the cases, Class B and Class C both will be the implementation of the interface X

Comments

0

X : is an interface

A : concrete class implementing X

B : concrete class extending A

C : concrete class extending A

IF A CLASS IMPLEMENTS A CLASS : it means the implementing class has to implement (i.e. define all methods ) from interface

IF A CLASS EXTENDS A CLASS : it means that all the methods from the parent class is available in child class , unless the child class overrides the methods .

now we conclude , since B,C extend all the methods from A , and A has implemented all the methods from X , thus both B,C have the methods from interface X implemented in A .

but we cant say that B,C implement X because B,C have no constraint to implement all methods from interface , but class A has to implement all the methods

8 Comments

B and C ARE implementations of X, whether you had to explicitly implement the methods or not. but we cant say that B,C implement X This is completely wrong. They do have a constraint to implement all of the methods in the interface. It just happens that those methods were implemented by a parent, but that's not necessary either in the case of abstract classes.
B,C are implementations of X , but they don't implement X , they get the the methods in inheritance from A
If B is an implementation of X, then B implements X. I'm not sure why you're trying to invent your own terminology based on nothing. If we differentiate between "implementation" and "implements", then everything would be confusing. It's simple right now.
ohk let me reconsider , will take some time
hi guys, if there is a method called M in interface X, Class A implements method M, can I actually understand as class B and class C inherit the method M from class A? As there is no explicit relation between interface X and class B&C, so I would think class B and class C inherit the method M from class A rather than implement method M from interface X
|
0
Does it mean both class B and class c will automatically implement interface X as well? 

Not really. It is not compulsory to implement all method in supper class in sub classes if super class is not a fully abstract class. But all attribute in supper class will inherited to sub classes.

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.