Hesam
Hesam

Reputation: 53610

Java, Is it possible to inherit interface?

In my application I have a class, let me call it class A. Four classes, let me call them B , C, D and E extend from this class. Each of these four classes has an interface that its functionality is same.

I want to know if I put this interface in class A all of these classes have access to it. Am I right?

I have another class, G that is implementing this interface. If I implement this interface does it apply to those four libraries at same time?

Update Let me go into detail. I have four classes. Each class has a method that when this class connects to internet will set a flag. like this.

public class ClassB extends {

    public interface OnConnectingToServer {
        public void onGettingDataFromServer(boolean flag);
    }

    public ClassB () {
        if(I'm_downloading_from_internet)
                OnConnectingToServer.onGettingDataFromServer(true);
    }
}

I have this situation for "B", "C", "D" and "E" classes. Class "G", implements this interface. If I want to put each interface in each class therefore, i need to assign four different names ans in "G" class also implements 4 interfaces. I think this way is not a good way.

I'm looking for a way that instead of implementing 4 same interfaces, implementing just one interface. therefore, regardless of triggering the interface by one class or all four classes, just one implementation is done.

Hope to make it more clear. Thanks again.

Upvotes: 0

Views: 278

Answers (4)

mihaisimi
mihaisimi

Reputation: 2029

If you implement the interface (call it I) in class A and B, C + D extend A then B, C and D will implement the I interface as well.

This means you will be able to write:

I aInstance = new A();
I bInstance = new B();

but also:

A aInstance = new A();
A bInstance = new B():

because logically your data model will become:

A is an I 
and  
B is an A 
so this implies:
B is an I

If class G implements the interface but doesn't inherit A you will have to implement the functionality defined in I in the class G as well.

This means you will be able to write:

I aInstance = new A();
I gInstance = new G();

but not this (this won't compile): A gInstance = new G();

Just remember, the interface is only a contract. Any class that implements an interface is bound to respect the contract but no implementation is inherited (no implementation can exist in the interface anyway).

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200296

Don't put the interface definition inside class A. Make it a separate top-level type and then make all your classes implements MyInterface. If class B extends A, then you don't have to explicitly say implements MyInterface since there is no way for a subclass to "unimplement" an interface.

Upvotes: 4

Brian Agnew
Brian Agnew

Reputation: 272417

Briefly, yes. From the Java Language Spec:

A class may be declared to directly implement one or more interfaces, meaning that any instance of the class implements all the abstract methods specified by the interface or interfaces. A class necessarily implements all the interfaces that its direct superclasses and direct superinterfaces do. This (multiple) interface inheritance allows objects to support (multiple) common behaviors without sharing any implementation.

(my emphasis)

Upvotes: 2

Keppil
Keppil

Reputation: 46239

  • Yes, if you let A implement an interface, then all classes that extend that class also implement that interface.
  • Not entirely sure what you mean with letting G implement the interface. Doing that will only affect classes that extend G.

Upvotes: 2

Related Questions