Java Implementing interface in abstract class

Latest posts
Get notified of the Latest Sport News Update from Our Blog
Share

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

Your email address will not be published. Required fields are marked *

Related blogs

Nullam quis risus eget urna mollis ornare vel eu leo. Aenean lacinia bibendum nulla sed 

Subscribe to get 15% discount