Java Inheritance

Inheritance is a mechanism in java which makes the properties of a class available to it subs classes.

Why Inheritance?

1. We can modify the existing implementation of a class in your sub class, in technical terms ro achieve run time Polymorphism. (Method overriding).

2. Code Re-usability.

How many Inheritance does java supports?

There are three kinds of inheritances supported in java.

1. Single inheritance.
2. Multilevel inheritance
3. Hierarchical inheritance. 

Inheritance in Java has two subtopics

1. Is-A Relationship
2. Has-A Relationship.

Is-A RelationShip :

Is-A Relationship is concept of providing, the properties of a class inherited another class. this can be done using extends keyword.

Lets do a simple POC on this concept. Lets create a parent class  ParentClass.java

package com.learning.inheritance;
public class ParentClass {
 public String getName() {
  return "My Name is Ravi kanth";
 }
}

Now lets extend the parent class to a class ChildClass.java and lets try to access getName() of the parent class in child class.


package com.learning.inheritance;

public class ChildClass extends ParentClass {
 public static void main(String[] args) {
  ChildClass childClass = new ChildClass();
  System.out.println(childClass.getName());
 }
}


Lets run the ChildClass.java and see the output

Output:

My Name is Ravi Kanth


You can clearly see the output, that child class object can use parents properties.

Now lets create a method which same and return type in ChildClass.java


package com.learning.inheritance;

public class ChildClass extends ParentClass {
 public static void main(String[] args) {
  ChildClass childClass = new ChildClass();
  System.out.println(childClass.getName());
 }

 public String getName() {
  return "My Name is John";
 }
}


Lets run the ChildClass.java and see the result:

Output:

My Name is John


you can see the it is printing the method of the childClass but not parent class, so you are overriding the methods of the parent class. This is called Method Overriding.

Points to be noted in a Is-A  Relationship.

1. By default all the Public and protected properties of the parent class is available to the sub classes.
2. We can always override the methods which are provided by the parent class to the subclasses until and unless if the methods are not declared as final
3.  You can hold parents reference in the child Object.


Has-A Relationship :

If a class have entity relationship of another class is called Aggregation or Composition. So Aggregation in java represents the Has-A relationship.

Lets do a simple POC on this concept.

Lets create a employee class Employee.java


package com.learning.inheritance;

public class Employee {
 
 private String firstName;
 private String lastName;
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 } 
}

But Employee information is incomplete, Employee working in a company has may other parameters like Employee Address, Employee Bank Details.

I have already  two classes which holds that information, which is as below.
  
EmployeeAddressInformation.java

package com.learning.inheritance;

public class EmployeeAddressInformation {

 private String houseNumber;
 private String streetName;
 private String city;
 private String state;
 private String country;
 private String pinCode;

 public String getHouseNumber() {
  return houseNumber;
 }

 public void setHouseNumber(String houseNumber) {
  this.houseNumber = houseNumber;
 }

 public String getStreetName() {
  return streetName;
 }

 public void setStreetName(String streetName) {
  this.streetName = streetName;
 }

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

 public String getState() {
  return state;
 }

 public void setState(String state) {
  this.state = state;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 public String getPinCode() {
  return pinCode;
 }

 public void setPinCode(String pinCode) {
  this.pinCode = pinCode;
 }

}

EmployeeBankInformation.java


package com.learning.inheritance;

public class EmployeeBankInformation {

 private String bankName;
 private String employeeNameinBankAccount;
 private String bankAccountNo;
 private String bankAddress;
 private String bankBranch;
 private String bankIfscNumber;

 public String getBankName() {
  return bankName;
 }

 public void setBankName(String bankName) {
  this.bankName = bankName;
 }

 public String getEmployeeNameinBankAccount() {
  return employeeNameinBankAccount;
 }

 public void setEmployeeNameinBankAccount(String employeeNameinBankAccount) {
  this.employeeNameinBankAccount = employeeNameinBankAccount;
 }

 public String getBankAccountNo() {
  return bankAccountNo;
 }

 public void setBankAccountNo(String bankAccountNo) {
  this.bankAccountNo = bankAccountNo;
 }

 public String getBankAddress() {
  return bankAddress;
 }

 public void setBankAddress(String bankAddress) {
  this.bankAddress = bankAddress;
 }

 public String getBankBranch() {
  return bankBranch;
 }

 public void setBankBranch(String bankBranch) {
  this.bankBranch = bankBranch;
 }

 public String getBankIfscNumber() {
  return bankIfscNumber;
 }

 public void setBankIfscNumber(String bankIfscNumber) {
  this.bankIfscNumber = bankIfscNumber;
 }

}

In the Employee Class. Instead of again adding the parameters which holds the details of the employee information, we can use the existing classes references  in your Employee class.  which is below.

package com.learning.inheritance;

public class Employee {

 private String firstName;
 private String lastName;
 private EmployeeAddressInformation addressInformation;
 private EmployeeBankInformation employeeBankInformation;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public EmployeeAddressInformation getAddressInformation() {
  return addressInformation;
 }

 public void setAddressInformation(EmployeeAddressInformation addressInformation) {
  this.addressInformation = addressInformation;
 }

 public EmployeeBankInformation getEmployeeBankInformation() {
  return employeeBankInformation;
 }

 public void setEmployeeBankInformation(EmployeeBankInformation employeeBankInformation) {
  this.employeeBankInformation = employeeBankInformation;
 }

}

Now Employee Class Has -A Relationship with EmployeeAddressInformation and EmployeeBankInformation classes.

The Major Advantage of Has-A Relationship is code re-usablility.

I am an open source enthusiast. Currently having 6 years of experience and part of Ericsson Global. I am a traveler, Coding manic, Foodie, Gaming Freak and an Amazing Cook.

Share this

Pages
Previous
Next Post »