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
B.java
TestMain.java
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
B.java
TestMain.java
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
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
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
Using the same concept, HomeLoanAccount and EducationLoanAccount classes are designed. Two different classes are defined because logic of interest calculation is different.
LoanAccount.java
EducationLoanAccount.java
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.