X Tutup
Skip to content

Latest commit

 

History

History
52 lines (36 loc) · 1.34 KB

File metadata and controls

52 lines (36 loc) · 1.34 KB

Introduction

Inheritance is a core concept in OOP (Object Oriented Programming). It donates IS-A relationship. It literally means in programming as it means in english, inheriting features from parent(in programming features is normally functions and variables).

Consider a class, Animal as shown,

//Creating an Animal class with bark() as a member function.
public class Animal {

    public void bark() {
        System.out.println("This is a animal");
    }

}

Animal is a parent class, because the properties this class has can be extended to all the animals in general.

Consider an animal named Lion, having a class like,

//Lion class is a child class of Animal.
public class Lion extends Animal {

    public void bark() {
        System.out.println("Lion here!!");
    }

}

Now whenever we do,

Animal animal = new Lion(); //creating instance of Animal, of type Lion
animal.bark();

Note: Initialising the Animal class with Lion. The output will look like

Lion here!!

According to OOP, there are many types of inheritance, but Java supports only some of them(Multi-level and Hierarchical). To read more about it, please read this.

X Tutup