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 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class Tree{ | |
..... | |
} | |
Class Pine extends Tree { | |
..... | |
} |
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 :
- 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)
- If you create a class that contains function/method, thought first, is that method will used much time in different classes or not
- How often we will call the same method or function
- 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package hs; | |
/** | |
* | |
* @author androidbie.com | |
*/ | |
public class Hospital { | |
private String hospitalName; | |
private String hospitalAddress; | |
public Hospital(String hospitalName, String hospitalAddress) { | |
this.hospitalName = hospitalName; | |
this.hospitalAddress = hospitalAddress; | |
} | |
public Hospital() { | |
} | |
public String getHospitalName() { | |
return hospitalName; | |
} | |
public void setHospitalName(String hospitalName) { | |
this.hospitalName = hospitalName; | |
} | |
public String getHospitalAddress() { | |
return hospitalAddress; | |
} | |
public void setHospitalAddress(String hospitalAddress) { | |
this.hospitalAddress = hospitalAddress; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package hs; | |
/** | |
* | |
* @author androidbie.com | |
*/ | |
public class Employee extends Hospital{ | |
private String employeeName; | |
private String employeeAge; | |
private String employeeTitle; | |
public Employee(String employeeName, String employeeAge, String employeeTitle) { | |
this.employeeName = employeeName; | |
this.employeeAge = employeeAge; | |
this.employeeTitle = employeeTitle; | |
} | |
public Employee() { | |
} | |
public String getEmployeeName() { | |
return employeeName; | |
} | |
public void setEmployeeName(String employeeName) { | |
this.employeeName = employeeName; | |
} | |
public String getEmployeeAge() { | |
return employeeAge; | |
} | |
public void setEmployeeAge(String employeeAge) { | |
this.employeeAge = employeeAge; | |
} | |
public String getEmployeeTitle() { | |
return employeeTitle; | |
} | |
public void setEmployeeTitle(String employeeTitle) { | |
this.employeeTitle = employeeTitle; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package hs; | |
/** | |
* | |
* @author androidbie.com | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
//kita panggi si sub classnya | |
// #1 | |
Employee pg = new Employee("Anree", "21", "Front Office"); | |
pg.setHospitalName("Hospital ABC"); //method super class | |
pg.setHospitalAddress("Victoria Street No 78"); //method superclass | |
//#2 | |
Employee pg1 = new Employee("John A", "26", "Doctor Specialist"); | |
pg1.setHospitalName("Hospital GGG"); //method super class | |
pg1.setHospitalAddress("Roma, Italy"); //method superclass | |
//Show ID Card #1 | |
System.out.println("ID Card"); | |
System.out.println(""); | |
System.out.println("Employee Name : \t\t" + pg.getEmployeeName()); | |
System.out.println("Title : \t\t" + pg.getEmployeeTitle()); | |
System.out.println("Hospital Name :\t" + pg.getHospitalName());//method superclass | |
System.out.println("Hospital Address :\t"+ pg.getHospitalAddress());//method superclass | |
System.out.println(""); | |
System.out.println(""); | |
//Show ID Card #2 | |
System.out.println("ID Card"); | |
System.out.println(""); | |
System.out.println("Employee Name : \t\t" + pg1.getEmployeeName()); | |
System.out.println("Title : \t\t" + pg1.getEmployeeTitle()); | |
System.out.println("Hospital Name :\t" + pg1.getHospitalName());//method superclass | |
System.out.println("Hospital Address :\t"+ pg1.getHospitalAddress());//method superclass | |
} | |
} |
The output of the program above is :
Okay that is the explanation about how Inheritance works, hope you'll understand. Thank you.
EmoticonEmoticon