Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

INHERITANCE

Inheritance : Inheritance is a mechanism in which object of one class acquires some or all the
properties and behavior of object of another class .
The idea behind inheritance in java is that it enables creation of new classes that are built upon
existing classes . when a new class inherits from another class then the new class gets some or
all the properties and behavior of the old class. if required any number of methods and fields can
be added that are specific to the new class The new class is called as super class/base
class/parent class and the old one is called as sub class/derived class/child class. The advantage
of inheritance is code reusability. inheritance represents IS-A relationship.

Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the mechanism in


java by which one class is allowed to inherit the features(fields and methods) of another class.

Important terminology:

Super Class: The class whose features are inherited is known as superclass(or a base class or a
parent class).
Sub Class: The class that inherits the other class is known as a subclass(or a derived class,
extended class, or child class). The subclass can add its own fields and methods in addition to the
superclass fields and methods.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new
class and there is already a class that includes some of the code that we want, we can derive our
new class from the existing class. By doing this, we are reusing the fields and methods of the
existing class.
How to use inheritance in Java

Syntax : 
The keyword used for inheritance is extends.

class derived-class extends base-class


{
//methods and fields
}
Types of inheritance :
Single inheritance : one base class and one derived class
A
l

B
Example:
Employee

SalariedEmployee
Multilevel inheritance:
one base class, one derived class and one or more intermediate base classes
A

C
In the above inheritance path, B is the base class for Derived class C , and B is a derived class of
base class A . B acts as a base class for some derived class and derived class for some other base
class hence B is called as an intermediate base class
Hierarchical inheritance: one base class with one or more child classes
A

B C
Example:
Employee

SalariedEmployee HourlyPaidEmployee
Multiple inheritance: one derived class with more than one base class. Java does not support
Multiple inheritance.
A B

C
A java class can not extend from more than one class at a time. Hence java does not provide
support for multiple inheritance.
class A extends B, C
{
}
X
Hybrid inheritance: combination of one or more inheritance is called as Hybrid inheritance.in
this combination there should not be any multiple inheritance.

Syntax :
class derived-class name extends base-class name
{
//methods and fields.
}
Extends is a keyword used for inheriting the features of base class into derived class
class P
{

void m1()
{
System.out.println("parent");
}

class C extends P
{

void m2()
{
System.out.println("child");
}

class Test
{
public static void main(String[] args)
{
P p=new P();
p.m1();
//Error-p.m2();

C c=new C();
c.m1();
c.m2();

P p1=new C();
p1.m1();
//Error- p1.m2();

//Error-C c1=new P();

}
Conclusions:
1. The methods in the parent class by default available to the child and hence on the child
reference we can call both parent and child class methods

2. The methods in the child class by default not available to the parent and hence on parent
reference we can not call child specific methods

3. Parent reference can be used to hold child object but using that reference we can not call
child specific methods, but we can call the methods present in parent class

4. Parent reference can be used to hold child object, but child reference can not be used to
hold parent object.
Without inheritance:
class VLoan class HLoan class PLoan
{ { {
300 methods 300 mehtods 300 methods
} } }
900 methods
90 Hours
With inheritance:
class Loan
{
250 methods
}
Class VLoan extends Loan class HLoan extends Loan class PLoan extends Loan
{ { {
50 specific methods 50 specific methods 50 specific methods
} } }
400 methods
40 Hours
Note: the most common methods which are applicable for any type of child, we have to define in
parent class. The specific methods which are applicable for a particular child we have to define
in child class.
A class can extend from only one class. When ever we develop any inheritance application it is
highly recommended for the java programmer to create an object of bottom most child class
because it contains all the features of top most and intermediate parent classes.
Whenever we create an object of bottom most child class first we get the memory allocation for
data members of the topmost parent class then we get memory allocation for data members of
intermediate parent classes and at the last we get memory allocation for data members of bottom
most child class.
Example:
class P
{
int i=10;
}

class C extends P
{
int j=20;
}

public class Test


{
public static void main(String[] args)
{
C c1=new C();
System.out.println("i="+c1.i+"....."+"j="+c1.j)
}

}
Output:
i=10…j=20

If we do not want to give the features of parent to the child classes, then the definition of
the parent class must be made as final hence final class never participate in inheritance
process.
Example:
final class P
{
}
class C extends P
{
}
Compilation error: can not inherit from P

If we do not want to give some of the features of parent class to the child class then those
features of the parent class must be made as private, hence private features of the parent
class never participate in the inheritance . in other words the private features of parent
class can not be accessed in the child class context

The constructors of the parent class will not be inherited to child class, because
constructors are meant for initializing its own class data members, but not other class data
members
If our class is not extending from any other class then only Object class is the super class of our
class
class A Object
{
} A
If our class is extending any other class then Object class is indirect super class of our class.
class B extends A
{
}
A Object Object

A
B
X B
Multiple inheritance Multilevel inheritance

super key word: super key word is used to refer immediate parent class instance variable. We
can use super keyword to access the data member or field of parent class. It is used if parent class
and child class have same fields.

Example:
class P
{
int i=10;
int j=20;
}
class C extends P
{
int i=100;
int j=200;
void show()
{
System.out.println(“child class data members:”);
System.out.println("i="+this.i+"...."+"j="+this.j);
System.out.println(“parent class data members:”);
System.out.println("i="+super.i+"...."+"j="+super.j);
}
}

class Test
{
public static void main(String[] args)
{
C c1=new C();
c1.show();
}
}
Output:
Child class data members:
i=100….j=200
Parent class data members:
i=10…j=20

super can be used to invoke parent class members. The super keyword can also be used to invoke
parent class method. It should be used if subclass contains the same method as parent class. In
other words, it is used if method is overridden.

//accessing parent class methods using super

class p
{
int i=888;
void printmessage()
{
System.out.println("inside printmessage() of class P");
}

class C extends P
{
int i=999;

void printmessage()
{
System.out.println("inside printmessage() of class C");
}

vod show()
{
this.printmessage();
super.printmessage();
System.out.println("i value in parent class="+super.i);
System.out.println("i vlaue in child class="+this.i);
}

class Test
{
public static void main(String[] args)
{
C c1=new C();
c1.show();
//super.printmessage() ;//error
}

}
OUTPUT
inside printmessage() of class C
inside printmessage() of class P
i value in parent class=888
i vlaue in child class=999

Method overriding: parent class methods are by default available to the child through
inheritance. If child class is not satisfied with parent class implementation then child is allowed
to re define that method based on its requirement this process is called overriding.
class P
{
void m1()
{
System.out.println("inside m1() method in P");
}
}

class C extends P
{
void m1()
{
System.out.println("inside m1() method in C");
}
}

class Test
{
public static void main(String[] args)
{
P p1=new P();
p1.m1();

C c1=new C();
c1.m1();

P p2=new C();
p2.m1();

}
}

OUTPU

inside m1() method in P


inside m1() method in C
inside m1() method in C

In over riding method resolution always takes care by java virtual machine based on runtime
object and hence overriding is also considered as runtime polymorphism or dynamic
polymorphism or late binding.
super key words is used to call the parent class constructor from child class constructor
explicitly.
//constructor flow in inheritance

class P
{
int i;

P(int i)
{
this.i=i;
System.out.println("class P 1-arg constructor");
System.out.println("value of i="+this.i);
}

class C extends P
{
int j;
C()
{
super(10);
j=20;
System.out.println("value of j="+j);
}

}
class Test
{
public static void main(String[] args)
{
C c1=new C();
}
}
ABSTRCT CLASSES
In java methods are categorized into two types.
1. Concrete methods
2. Abstract methods
Concrete methods: methods with implementation are called concrete methods.
Abstract method: method having only declaration and no implementation is called abstract
method. Abstract method declaration should end with a semicolon.
Example:
public abstract void m1();
public abstract void m1() { } X
child class is responsible to provide implementation for parent class abstract methods.
Example:
abstract class Vechile
{
abstract public int getNoOfWheels();
}
class Bus extends Vechile class Auto extends Vechile
{ {
public int getNoOfWheels() public int getNoOfWheels()
{ {
return 7; return 3;
} }
} }

class AbsDemo
{
public static void main(String[] args)
{

Vechile v=new Bus();


System.out.println(“Number of Wheels of Bus is:”+v.getNoOfWheels());
Vechile v=new Auto();
System.out.println(“Number of Wheels an Auto is:”+v.getNoOfWheels());

}
}
By declaring abstract method in the parent class, we can set up the rules to the child class .
every child class must provide the implementation for all the abstract methods of its parent
class.

Abstract class: In a class if there is at least one abstract method then that class must be declared
as an abstract class. Object creation is not allowed for abstract class. The purpose of declaring an
abstract class is to prevent the object creation. Even though there are no abstract methods in a
class still we can declare that class as abstract class.
An abstract class can have zero or more abstract methods.
Example:
Account

CurrentAccount SavingsAccount

The common properties and behavior of CurrentAccoutn and SavingsAccount are taken in
Account class . we wish to prevent the object creation for Account class then we can declare
Account class as abstract even though there is no abstract method in it.

Example: HttpServlet class is an abstract class but it does not have any abstract methods
Example 1:
class P
{
public void m1();
}

CE: missing method body , or declare abstract

Example 2:
class P
{
public abstract void m1() { }
}

CE: abstract methods can not have a body

Example 3:
class P
{
public abstract void m1();
}

CE: P is not abstract and does not override abstract method m1() in P

If a class is extending from an abstract class then for each and every abstract method of the
super class implementation must be provided in the sub class other wise the sub class becomes
abstract class.
Example: class P
{
public abstract void m1();
public abstract void m2();
}
class C extends P
{
public void m1() { }
}

CE: C is not abstract and does not override abstract method m2() in P

You might also like