Inheritance (OOP - JAVA)

 Inheritance


Let's consider the first BMW car and a BMW 740E car. There are lots of differences and similarities in   those cars. 



See the similar properties for those cars,

  • tires 
  • engine
  • light
  • door 
  • break liners
  • seats
  • steering wheel   
See the similar behaviors for those cars,
  • drive
  • reverse
  • break
  • horn
  • signal lights on/off
See the newly properties for BMW 740E,
  • cameras
  • sensors
  • digital dashboard
  • video player
  • GPS
  • air bags
See the newly behaviors for BMW 740E,
  • auto pilot
  • voice command
When we think about the above properties and behaviors, we can see each an every property and behavior which is related to the first BMW car have been joined to the BMW 740E car. In other words, the first BMW car properties and behaviors have been inherited to the BMW 740E car.

The things which are related to a class goes to another class is called Inheritance. We need to make a joint between two classes to do it. The first class is called as super class(parent class) and the second class is called as sub class(child class)

Look at the example given below.

class OldCar{

    String color;
    String name;

    void drive(){
    }

    void break(){
    }

}

class NewCar extends OldCar{

    String camera;
    String wifi;

    void flying(){
    }

}

In this example code segment, we can see the NewCar has been extended by the OldCar.




Take a few minutes to understand the inheritance concept by following the pictures I have provided.

🤔

I think now you can understand the concept well. 







Comments

Popular posts from this blog

Object Creation, Data Types (OOP - JAVA)

Upcasting and Downcasting (OOP - JAVA)

Method Overloading (OOP - JAVA)