Inheritance in Java is a mechanism in which one object acquires all the properties and behavior of parent class. It allows a software developer to derive a new class from an existing class.This is one of the core feature of java programming language,we already discussed java polymorphisam,java abstraction in detail.
Inheritance allows us to define general class and then more specialized classes simply by adding new details to the more general class definition. The subclass is a specialization of the superclass
- A class which is derived from an existing class is known as Derived class or subclass.
- The class from which other classes are derived is called Base class or super class
Inheritance allows us to define certain behviors once and then to reuse those behaviors over and over again in the subclasses. This is called reusability.
a)For method overriding ( runtime polymorphism can be achieved)
b) code re-usability
Syntax for java Inheritance:
class subclass_name extends superclass_name{
//methods and fields
}
Note:
Here extends keyword indicates that you are making a new class that derives from an existing class
Here extends keyword indicates that you are making a new class that derives from an existing class
Understanding the simple example of inheritance:
As the display in the above figure programmer is subclass and Employee is the superclass
Relationship Between two classes is called IS-A Relationship. That means a programmer is a type of Employee.
For Example :
class Employee{
Float salary=40000;
}
class Programmer extends Employee{
int bonus=1000;
public static void main(String args){
Programmer p=new Programmer();
System.out.printn("programmer salary is="+p.salary);
System.out.printn("Bonus of programmer is="+p.bonus);
}
}
Output:
Programmer salary is=40000
Programmer salary is=40000
Bonus of programmer is= 1000
In the above Example Programmer object can access the fields of own class as well as of employee class i.e. code re-usability
Note:- parent class reference can be used to hold child class object but by using that reference we r not allowed to call child class specific methods.
Ex:-
class test
{
public static void main(String arg[])
{
p p1 = new p();
p1.m1();
p1.m2();
p1.m3(); ->C.E: Child class methods are not available to the parent class.
c c1 = new c();
c1.m1();
c1.m2();
c2.m3();
p p2 = new c();
p2.m1();
p2.m2();
p2.m3(); ->C.E: By using parent class reference we can’t call child class specific
method
c c4 = new p() ->C.E: Child class reference can’t be used to hold parent class object.
}
}
Example 2:
Ex:-
class test
{
public static void main(String arg[])
{
p p1 = new p();
p1.m1();
p1.m2();
p1.m3(); ->C.E: Child class methods are not available to the parent class.
c c1 = new c();
c1.m1();
c1.m2();
c2.m3();
p p2 = new c();
p2.m1();
p2.m2();
p2.m3(); ->C.E: By using parent class reference we can’t call child class specific
method
c c4 = new p() ->C.E: Child class reference can’t be used to hold parent class object.
}
}
Example 2:
class Person{
String name;
String getName();
{
.......
.....
}
class Employee extends Person
{
int salary;
int getSalary();
{
..........
.......
}
class Student extends Person
{
int marks;
int getMarks();
{
.............
............
}
Types of Inheritance:
Single Inheritance:
New class is derived from single base class
Multilevel Inheritance:
New class ID derived or inherited from another derived class
Multiple Inheritance:
New class is derived from several base classes
Hybrid Inheritance:
Features of one class may be inherited by more than one class.
No comments:
Post a Comment