pei wang
pei wang

Reputation: 295

interface and inheritance in java

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?

Upvotes: 3

Views: 2549

Answers (6)

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

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.

Upvotes: 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

Upvotes: 0

shikjohari
shikjohari

Reputation: 2288

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

Upvotes: 0

MadConan
MadConan

Reputation: 3767

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 
}

Upvotes: 3

Nambi
Nambi

Reputation: 12042

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.

Upvotes: 0

Jeff Richley
Jeff Richley

Reputation: 1

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

Upvotes: 4

Related Questions