JavaExplain Inheritance in Java?

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.sound(); // Output: Dog barks
    }
}

Related Snippets

Cover Image for Generate a random number

Generate a random number

Loading...
Cover Image for How does Exception Handling work in Java?

How does Exception Handling work in Java?

Loading...
Cover Image for What is a List in Java?

What is a List in Java?

Loading...
Cover Image for What is Method Overloading?

What is Method Overloading?

Loading...
Cover Image for What is the main method in Java?

What is the main method in Java?

Loading...
Cover Image for What is the Singleton Pattern?

What is the Singleton Pattern?

Loading...