DEV Community

Kiruthiga S
Kiruthiga S

Posted on

Java

Inheritance
Allows one class to acquire properties and methods of another class
A subclass can reuse the fields and methods of the parent class without rewriting the code
A subclass can add its own fields and methods or modify existing ones to extend functionality

class Animal {
    void eat() {
        System.out.println("Animal eats");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();
        d.bark();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:
Animal eats
Dog barks

Top comments (0)