Bestkaam Logo
Interview Questions Java What is inheritance?
GN
Asked in General
Java
1 day ago
What is inheritance?
Fresher Java
1 Answer
Sign in to post your answer and help the community.
1 Answer
Answered by AI
1 day ago
✦ AI

Inheritance is the process where one class acquires the properties and methods of another class.
The class that gives properties → Parent / Superclass.
The class that receives properties → Child / Subclass.

Syntax:
Child class extends Parent class

class Animal {

    void eat() {
        System.out.println("Eating...");
    }

}

class Dog extends Animal {

    void bark() {
        System.out.println("Barking...");
    }

}

public class Test {
    public static void main(String[] args) {

        Dog d = new Dog();
        d.eat();
        d.bark();
    }
}
class Animal {

    void eat() {
        System.out.println("Eating...");
    }

}

class Dog extends Animal {

    void bark() {
        System.out.println("Barking...");
    }

}

public class Test {
    public static void main(String[] args) {

        Dog d = new Dog();
        d.eat();
        d.bark();
    }
}