Inheritance

Inheritance is a process where a class can acquire the method and properties of another class.
The class which inherits the properties of other is known as subclass and the class whose properties are inherited is known as parent class.

Example:

class Main {
    public static void main(String[] args) {
        Animal animal=new Animal(); //Animal referenced and animal object
        animal.run();
        Animal chitah=new Chitah(); //Animal referenced but chitah object
        chitah.run();
        Animal human=new Human(); //Animal referenced but human object
        human.run();

    }

}
class Animal{
    public void run(){
        System.out.println("Animal can run");
    }
}
class Chitah extends Animal{
    public void run(){
        System.out.println("Chitah can run fast");
    }
}
class Human extends Animal{
    public void run(){
        System.out.println("Human can run slower");
    }
}

Output:

Animal can run
Chitah can run fast
Human can run slower
about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *