Java Implementing interface in abstract class

In interface method is by default public abstract . when abstract class implements interface a method which are defined in interface we don’t need to implement.

This is because a class that is declared abstract can contain abstract method declarations. It is therefore the responsibility of the first concrete sub-class to implement any abstract methods inherited from any interfaces and or the abstract class.

interface Test {
    void test();
}

abstract class Fruit implements Test{
    //Does not need to declare or implement test()
    public abstract void color();

}

/*Because Orange is concrete, it must define both test() and color()*/
class Orange extends Fruit{
    public void color(){
        System.out.println("Yellow color");
    }
    public void test(){
        System.out.println("sour ");
    }
}

public class Program1 {
    public static void main(String[] args) {
        Orange o1 = new Orange();
        o1.color();//Yellow color
        o1.test();// sour
    }
}

Leave a Reply