Inheritance



Inheritance :
 
Definition: 
           Inheritance allows a class (subclass) to inherit properties and behaviors from another class (superclass).

Explanation:
                 Subclasses extend the functionality of the superclass by inheriting its attributes and methods. Method overriding lets subclasses provide specific implementations for inherited methods.

Example:

class Animal {

    void sound() {

        System.out.println("Animal makes a sound.");

    }

}

 

class Dog extends Animal {

    @Override

    void sound() {

        System.out.println("Dog barks.");

    }

}

 

public class Main {

    public static void main(String[] args) {

        Animal myDog = new Dog();

        myDog.sound(); // Output: Dog barks.

    }

}

Comments

Popular posts from this blog

Installing MySQL and MySQL Workbench

Java Program to Check Palindrome Number

Scenario : 1