The
process of creating a class to reuse exiting class members using our class name
or object is called inheritance. It can be defined as a process of obtaining
one object property to another object. The concept of getting the properties of
one class to another class is known as inheritance.
Super class – any class
which is getting extended by another class is known as super class.
Sub class – any class
which is extending another class is known as subclass.
We use
inheritance for method overriding and for code reusability.
Syntax –
class Subclass
extends Superclass
{
------
------
}
The extends keyword indicates that we are
making a new class that derives from an existing class.
Example –
class
Employee{
float salary=40000;
}
public class
Programmer extends Employee{
int bonus
= 10000;
public static void
main(String[] args) {
Programmer
p = new
Programmer();
System.out.println("Programmer salary is : "+p.salary);
System.out.println("Programmer bonus is : "+p.bonus);
}
}
Output –
Programmer salary is : 40000.0
Programmer bonus is : 10000
Important points to be remembered with respect of inheritance –
- · Always super class objects are created from the subclass constructors. It is the subclass constructor which would be creating object of super class in association with the subclass object, by calling the super class constructor.
- · Sub class objects or objects of any classes can’t be created without creating super class objects.
- · Always the super class objects are created first and sub class objects are created last.
Types of inheritance –
·
Single level
·
Multi level
·
Hierarchical
·
Hybrid
·
Multiple interface inheritance
Note – Java does not support
multiple inheritance, but it provides alternative method to support multiple
inheritance with interfaces.
1. Single level inheritance – If two
classes or two interfaces or a class and interface participating in an
inheritance is called single level.
·
Sub-class extends Super-class
·
Sub-interface extends Super-interface
·
Sub-class implements Super-interface
Example –
class
Add{
int num1,num2,result=0;
public void
add()
{
result=num1+num2;
}
}
class
Sub extends Add{
public void
sub()
{
result=num1-num2;
}
}
public class
Test2 {
public static void
main(String[] args) {
Sub s = new
Sub();
s.num1=50;
s.num2=30;
s.add();
System.out.println(s.result);
s.sub();
System.out.println(s.result);
s.sub();
}
}
Output –
80
20
Note – In the above example Sub
class extends all the properties of Add class. So we have created an object of
Subclass, but still we can directly acces the properties of its Super class.
2. Multi level inheritance – If more
than two classes are participating in inheritance relation vertically, we call
it as multilevel inheritance.
Example –
class
Add{
int num1,num2,result=0;
public void
add()
{
result=num1+num2;
}
}
class
AddSub extends Add{
public void
sub()
{
result=num1-num2;
}
}
class
AddSubMul extends AddSub
{
public void
mult()
{
result=num1*num2;
}
}
public class
Test2 {
public static void
main(String[] args) {
AddSubMul s = new
AddSubMul();
s.num1=50;
s.num2=30;
s.add();
System.out.println(s.result);
s.sub();
System.out.println(s.result);
s.mult();
System.out.println(s.result);
}
}
Output –
80
20
1500
Note – In the above example
AddSub extends Add and AddSubMul extends AddSub, so all the properties of above
parent class is being inherited by the sub class AddSubMul.
3. Hierarchical inheritance – If we
derive multiple sub classes from a single super class, we call it as
Hierarchical inheritance
Example –
class
Add{
int num1,num2,result=0;
public void
add()
{
result=num1+num2;
}
}
class
AddSub extends Add{
public void
sub()
{
result=num1-num2;
}
}
class
AddMul extends Add
{
public void
mult()
{
result=num1*num2;
}
}
public class
Test2 {
public static void
main(String[] args) {
AddSub a = new
AddSub();
a.num1=15;
a.num2=10;
a.add();
System.out.println(a.result);
a.sub();
System.out.println(a.result);
AddMul a1 = new
AddMul();
a1.num1=5;
a1.num2=3;
a1.add();
System.out.println(a1.result);
a1.mult();
System.out.println(a1.result);
}
}
Output –
25
5
8
15
4. Hybrid inheritance – Developing
an inheritance by combining other types of inheritances is called hybrid
inheritance.
For more details refer tutorials on types
of classes.
5. Multiple inheritance – Deriving
a sub class from multiple super class is called multiple inheritances. Java
does not support multiple inheritance with classes, but it supports with
interfaces.
For more details refer tutorials on types
of classes.
No comments:
Post a Comment