Backend
Java
Java questions
What is inheritance in Java and how is it implemented?

What is inheritance in Java and how is it implemented?

Inheritance in Java is a mechanism by which a class can inherit fields and methods from another class. It is implemented using the extends keyword.

For example:

    public class Animal {
    public void makeSound() {
        System.out.println("Animal sound");
    }
}
 
public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Woof");
    }
}