简体   繁体   中英

Java - Implement two interfaces with different methods

Say there are two interfaces, which have different methods. I am implementing both interfaces in a class and using methods of both the interfaces.

interface A { void show1(); }
interface B { void show(); }

public class test implements A, B{
    @Override
    void show1(){
        System.out.println("show1");
    }

    @Override
    void show(){
        System.out.println("show");
    }
}

Definition of Multiple Inhertiance:

Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class.

Question

Can I say what I did in my program is Multiple Inheritance ? If No, Why ?

Note

I am using Java 7.

I think implementation of multiple interfaces is not multiple inheritance.

Your object may have implementation of multiple interfaces. For example your Car object may have interface like BreakStatus , FuelTank interfaces but it's not defined Car as a sub-type/derived-type of BreakStatus , FuelTank .

Inheritance is: One object extends other object(parent) properties and/or behaviors.

Java object does not support multiple inheritance(extending multiple object) something like class Child extends Parent1, Parent2 {}

NB Java interfaces support extending multiple interfaces, eg interface I3 extends I1, I2 {}

No. Because main purpose of interface is abstraction, which means hide implementation details from outside. So if you implement one or more interfaces, that relationship is a like a relationship and not is a relationship.

Another purpose of using interfaces is loose coupling between classes. (side outcome of abstraction).

See this is a nice example

Yes what you did in your program is multiple inheritance.

The Java programming language supports multiple inheritance of type, which is the ability of a class to implement more than one interface. An object can have multiple types: the type of its own class and the types of all the interfaces that the class implements.

https://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html

Yes you are achieving multiple inheritance using multiple interface, where you can inheriting the definition of two different interfaces.

In java, multiple Inheritance is achieved using Multiple interfaces. Methods in an interface are always abstract by default, which doesn't let them to give their implementation (or method definition ) in interface itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM