Multiple Inheritance Troubles

Java-Extend Interface

Extend Interface

Interface is also extended by other Interface. One Interface can inherit another by use of the keyword extends. This is much like a way for inheriting class. When you inheriting two interfaces, you must provide implementations for all methods defined within the interface inheritance path.

In the following program, the interfaces InterOne and InterTwo are known as the super interfaces of the InterThree interface. On the other hand, the InterThree interface is called the sub interface of the InterOne and InterTwo interfaces. A sub interface should not implement the methods declared in its super interface because it is also an interface. The class that implements a sub interface should furnish the codes not only for the methods declared in that sub interface but also for the methods declared in its super interface.

In the following program, two interfaces named InterOne and InterTwo are first defined. While the interface InterOne declares the method methodOne( ), the interface InterTwo declares the method methodTwo( ). The interface InterThree, which extends the interfaces InterOne and InterTwo, declares a new method methodThree( ). The interface InterThree will actually hold the declarations for the three methods, namely methodOne( ), methodTwo( ) and methodThree( ). The class Test, which implements the interface InterThree, provides codes for the methods methodOne( ), methodTwo( ) and methodThree( ). Within the main( ) method of the Javaapp, an object is created for the class Test. Then, the methods methodOne( ), methodTwo( ) and methodThree( ) are accessed.

Program

extends-interface extends interfaces

Program Source

interface InterOne {
   
    void methodOne();
}

interface InterTwo {

    void methodTwo();
}

interface InterThree extends InterOne, InterTwo {
    int data1 = 90;
    void methodThree();
}

class Test implements InterThree {
    
    public void methodOne()
    {
        System.out.println("MethodOne Called"+data1);
    }
    
    public void methodTwo()
    {
        System.out.println("MethodTwo Called");
    }
    
    public void methodThree()
    {
        System.out.println("MethodThree Called");
    }
}

public class Javaapp {

    public static void main(String[] args) {
        
        Test t = new Test();
        t.methodOne();
        t.methodTwo();
        t.methodThree();
        
    }
}

Leave a Comment