Saturday, May 12, 2018

What is Inheritance in Java? Here is Detail Explanation of Inheritance

What  Inheritance is? Perhaps for those who learning about object oriented programming (OOP) have heard about this concept. Maybe there are some people have understood about Inheritance or there are still confuse about how Inheritance really work.

This article will guide you who learning object oriented programming and still don't understand the concept of Inheritance.

What Inheritance is?

Inheritance is the process of inheriting data/method from super class to its sub class. What does it means? It means what is owned by super class (read : method/process) is also owned by its sub class.

What is the characteristic of class that use Inheritance concept?

The main characteristic that you can find is that class will use extends to its super class. For example :

From the snippet above, Class Tree as Super Class and Class Pine as Sub Class. Whatever data/method/process that owned by Class Tree is also owned by Class Pine.

Is all extends means that class has implement Inheritance concept?

Basically yes, but we have to get the concept of Inheritance, why developers created Inheritance model? Have you thinking about it?

Inheritance concept created in order to make easy developers to access the same method/function in various classes by extends the super class.

But there are some things that must be thought out before you implement Inheritance in your project, here are they :
  1. Make sure you have understood the flow of your program and its characteristic (is that related with the Car, People or the thing that have much entities inside it)
  2. If you create a class that contains function/method, thought first, is that method will used much time in different classes or not
  3. How often we will call the same method or function
  4. How important the method or function in class where will use it.
If you requirements like the 4 point above, you should use the Inheritance concept. But if now, there is no problem with that, here we talk about the best practice implementation of Inheritance.

Example :

If you create super class called Class Tree, the create sub class called Class Yamaha, and then the Class Yamaha extends Tree{}. it totally wrong! We should extends that much related with the Super Class, not just do extends like what you will. haha

Until here, understood?

So, what is the right characteristic that can implement Inheritance concept?

There are much example that can implement the Inheritance concept, here I will give you one :

Characteristic of Hospital

Super Class :
  • Class name : Hospital{...}
  • Method : hospitalName(...), hospitalAddress(....)
Sub Class :
  • Class name : Employee{...}
  • Method : employeeName(...), employeeAge(....)

Perhaps one of you have a question and asking "What the relation of Class Employee and Class Hospital?"

Well, the sure thing is the hospital must have employees, right? either it doctor, back office, front office or other staff. Those employees must be have an identity (looks like idcard) where are they work, so Class Employee has to extends Class Hospital as its Super Class.

Project Example

In order make you clear understand the concept of Inheritance, let implement the characteristic of the Hospital above

Class Hospital

Class Employee

Main Program

Take a loot at the codes above, method setHospitalName and setHospitalAddress called directly from its Super Class. Why? Because those methods will call repeated, so that methods wrote in its Super Class (Class Hospital).

The output of the program above is :

Okay that is the explanation about how Inheritance works, hope you'll understand. Thank you.

Related Posts