Inheritence
Inheritence
programming problem.
given example.
accessing package, import statement, static import, adding class and interfaces
to a package.
Inheritance in Java
Reusability is yet another aspect of OOP paradigm
It is always nice if we could reuse something that already exists
rather than creating the same class all over again.
This is basically done by creating new classes, reusing the properties
of existing ones.
Mechanism of deriving new class from old class is called
inheritance
The old class is known as-
Base Class / Super class / Parent Class
The new class is known as-
Derived class/ Sub Class / Child class
Inheritance in Java
Inheritance allows subclasses to inherit all the variables and
methods of their parent classes.
Types:
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple inheritance
4. Hierarchical Inheritance
Java does not directly implement multiple inheritance.
However this concept is implemented using a concept of
interface
Defining Subclass
A subclass is defied as follows:
class sub_class extends super_class
{
variable declarations;
method declarations;
}
The keyword extends signifies that the properties of the
super_class are extended to the sub_class.
The sub_class will now containts its own variable and methods
as well as those of the super_class.
Inheritance In Java
Single inheritance -
class Vehicle
{
Vehicle()
{
System.out.println("Vehicle is created");
}
}
class Bike extends Vehicle{
Bike()
{
super();//will invoke parent class constructor
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike b=new Bike();
}
}
Multiple Inheritance
“Multiple Inheritance” refers to the concept of one class
extending (Or inherits) more than one base class.
The problem with “multiple inheritance” is that the derived class
will have to manage the dependency on two base classes.
A B
C
Multilevel Inheritance
Multilevel inheritance refers to a mechanism in OO
A
technology where one can inherit from a derived class,
thereby making this derived class the base class for the new class.
B
As you can see in below flow diagram C is subclass or child class
The class A serves as a base class for the derived class B which in
turn serves as base class for the derived class C. The chain ABC is
known as inheritance path
Class X
{
public void methodX()
{
System.out.println("Class X method");
}
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling local method
}
}
class Car{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Maruti extends Car{
public Maruti()
{
System.out.println("Class Maruti");
}
public void brand()
{
System.out.println("Brand: Maruti");
}
public void speed()
{
System.out.println("Max: 90Kmph");
}
}
public class Maruti800 extends Maruti{
public Maruti800()
{
System.out.println("Maruti Model: 800");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
class Design {
Design(){
System.out.println("Design()...");
} public class TestCallOrder {
} public static void main(String[] args)
class Coding extends Design { {
Coding(){ //Create object of bottom most
System.out.println("coding()..."); class object
} System.out.println("Constructor
} call order...");
class Testing extends Coding { new Testing();
Testing() }
{ }
System.out.println("Testing()...");
}
}
class X class Z extends Y
{ {
int i; int k;
X(int i) Z(int i, int j, int k)
{ {
this.i = i; super(i, j);
System.out.println("Created X"); this.k = k;
} System.out.println("Created Z");
} }
}
class Y extends X
{ class MultiLevelConstructors
int j; {
Y(int i, int j) public static void main(String arg[])
{ {
super(i); Z z = new Z(12, 22, 32);
this.j = j; System.out.println("---------------");
System.out.println("CreatedY");
} }
} }
Hierarchical Inheritance
In such kind of inheritance one class is
B,C & D.
B C D
Hierarchical Inheritance
Many programming problems can be cast into a hierarchy where
Fixed-
Saving deposit
Current
class Test{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
}
Method Overloading Method Overriding
Java run time is automatic garbage collecting system. It automatically frees up the
memory resources used by the objects.