Inheritance in Java - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts


In object-oriented programming, inheritance is a very important feature. Inheritance is a technique by which properties of one class is inherited in another class. A class which inherits the property is known as sub-class and the class from which the property is inherited is known as super-class. Let us discuss with a sample example.

In this example, we have defined two classes, A and B. in class A, two variables x and y are declared. Here, class B inherits A. In class B variable z is declared. In class B we can access x and y because the variables are inherited from class A.

A.java
public class A { int x; int y; public A(int x, int y) { this.x = x; this.y = y; } }
B.java
public class B extends A { int z; public B(int x, int y, int z) { super(x, y); this.z = z; } }
TestMain.java
public class TestMain { public static void main(String[] args) { A a = new A(10, 5); System.out.println(a.x + " " + a.y); B b = new B(10, 4, 7); System.out.println(b.x + " " + b.y + " " + b.z); } }

In the above class TestMain, object of B class is created and the instance variables, x, y and z are printed by the reference b. Though, x and y are declared in class A, the variables are being used in a similar way as instance variables are used.

Look at the constructor of B class carefully. Within this constructor, super is written as the first line.

When we initiate object creation of a class, first the constructor of the sub-class calls the constructor of the super class. If super class contains a parameterized constructor, the required parameters are to be passed in the super-class constructor. The super is used to call the super-class constructor. If the super-class does not have any constructor or it has a constructor with no parameters, then use of super is not mandatory.


Accessing object of one class by reference of other class


In Java, an object of one class can be accessed if there exists a sub-class and super-class relationship (inheritance).

Object of sub-class can be accessed by super-class reference, but not vice-versa, as shown in the following example.

Here, the object of class B is accessed using a reference of class A, but remember that all the methods and instance variables of class B can not be accessed by reference to class A. Only the variables and methods, which are declared and inherited in class A, are accessible by the reference of class A

A.java
public class A { int x = 10; void showA() { System.out.println(x); } }
B.java
public class B extends A { int y = 5; void showB() { System.out.println(x + " " + y); } }
TestMain.java
public class TestMain { public static void main(String[] args) { A a = new A(); a.showA(); // own method B b = new B(); b.showB(); // own method b.showA(); // inherited method A a1 = new B(); // object of sub-class can be accessed by super-class reference a1.showA(); // a1.showB(); // not accessible // B b1 = new A(); // object of the super class can not be accessed by sub-class reference } }

Case Study


Let us explain with an example.

We want to design a banking system. So, we have to manage different types of accounts of the customers, such as savings, salary, term deposits, loans, etc. If you define the classes, you will see that there are many common attributes in the classes. But those are added in every class.

In this context, the solution is inheritance.

If you create a class Account, in which all the common attributes are written, the attributes of the class can be inherited in other classes. Thus the Account class will be known as the super (parent) class and the classes which will inherit this class will be known as sub-classes.

Here, the advantage is you don't need to write a common set of attributes as well as methods in all the sub-classes.

Let us define the class as follow.

Account.java
public class Account { private int _accountNo; private String _customerName; private String _customerAddress; private String _branchName; private String _branchAddress; private float _amount; private float _rateOfInterest; public int get_accountNo() { return _accountNo; } public void set_accountNo(int _accountNo) { this._accountNo = _accountNo; } public String get_customerName() { return _customerName; } public void set_customerName(String _customerName) { this._customerName = _customerName; } public String get_customerAddress() { return _customerAddress; } public void set_customerAddress(String _customerAddress) { this._customerAddress = _customerAddress; } public String get_branchName() { return _branchName; } public void set_branchName(String _branchName) { this._branchName = _branchName; } public String get_branchAddress() { return _branchAddress; } public void set_branchAddress(String _branchAddress) { this._branchAddress = _branchAddress; } public float get_amount() { return _amount; } public void set_amount(float _amount) { this._amount = _amount; } public float get_rateOfInterest() { return _rateOfInterest; } public void set_rateOfInterest(float _rateOfInterest) { this._rateOfInterest = _rateOfInterest; } }

Now we will design another class SavingsAccount, which will inherit the properties of Account class. There are some properties in SavingsAccount class which are not the properties of other classes like loan accounts. Thus those methods are defined in SavingsAccount class as shown below.

SavingsAccount.java
public class SavingsAccount extends Account { public void payAnnualInterest() { set_amount(get_amount() + get_amount() * get_rateOfInterest() / 100); } public void deposit(float amount) { set_amount(get_amount() + amount); } public void withdrawl(float amount) { set_amount(get_amount() - amount); } public float getAccountBalance() { return get_amount(); } }

In this context, you will understand that every type of loan account should inherit the properties of the Account class. Now, try to think in detail. All the loan accounts have some common properties. Thus, here we want to define another class LoanAccount, which inherits the properties of the Account class. So, LoanAccount class is defined as below.

LoanAccount.java
public class LoanAccount extends Account { public float getRemainingLoanAmount() { return get_amount(); } public void rePayment(float amount) { set_amount(get_amount() - amount); } }

Using the same concept, HomeLoanAccount and EducationLoanAccount classes are designed. Two different classes are defined because ligic of interest calculation is defferent.

LoanAccount.java
public class HomeLoanAccount extends LoanAccount { // update loan amount based on compound interest public void updateLoanAmountOnInterest(int noOfYear) { set_amount(get_amount() + get_amount() * (float) Math.pow(get_rateOfInterest() / 100, noOfYear)); } }
EducationLoanAccount.java
public class EducationLoanAccount extends LoanAccount { // update loan amount based on simple interest public void updateLoanAmountOnInterest(int noOfYear) { set_amount(get_amount() + get_amount() * get_rateOfInterest() * noOfYear / 100); } }

Happy Exploring!

No comments:

Post a Comment