Java Hierarchical Inheritance Example

In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. 

Java Hierarchical Inheritance Example

class Animal {
    void eat() {
        System.out.println("eating...");
    }
}
class Dog extends Animal {
    void bark() {
        System.out.println("barking...");
    }
}
class Cat extends Animal {
    void meow() {
        System.out.println("meowing...");
    }
}
class TestInheritance3 {
    public static void main(String args[]) {
        Cat c = new Cat();
        c.meow();
        c.eat();
        //c.bark();//C.T.Error  
    }
}
Output
meowing...
eating...

Comments