UNIT III Inheritance
UNIT III Inheritance
Inheritance
Inheritance in java is a mechanism in which one class acquires all the properties and
methods of parent class
The idea behind inheritance in java is that you can create new classes that are built upon
existing classes.
When you inherit from an existing class, you can reuse methods and fields of parent
class, and you can add new methods and fields also.
The extends keyword indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality.
In the terminology of Java, a class which is inherited is called parent or super class and
the new class is called child or subclass.
Syntax:
class superclass-name
{
// body of class
}
classsubclass-name extends superclass-name
{
// body of class
}
Example:
Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}
Types of Inheritance
Que:
1. List different types of inheritance and explain any one with example.
2. Describe different forms of inheritance with example.
3. List types of Inheritance in Java.
4. Explain multilevel inheritance with example.
5. List four types of Inheritance.
Ans:
Below are the different types of inheritance which is supported by Java.
1. Single Inheritance
2. Multiple Inheritance (Through Interface)
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance (Through Interface)
Class A
{
public void methodA()
Page 2 of 12
{
System.out.println("Base class method");
}
}
class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}
Page 3 of 12
MultiLevel Inheritance Example
Class A
{
public void methodA()
{
System.out.println("Class A method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("class B method");
}
}
Class C extends B
{
public void methodC()
{
System.out.println("class C method");
}
Page 4 of 12
obj.methodB(); //calling parent class method
obj.methodC(); //calling local method
}
}
In Hierarchical inheritance one parent class will be inherited by many sub classes.
As per the below example ClassA will be inherited by ClassB,
ClassC and ClassD. ClassA will be acting as a parent class for ClassB,
ClassC and ClassD.
publicclassA
{
publicvoid methodA()
{
System.out.println("ClassAmethod ");
}
}
publicclassBextendsA
{
publicvoidmethodB()
{
System.out.println("Class B method ");
}
}
publicclassCextendsA
{
publicvoidmethodC()
{
System.out.println("Class C method ");
}
}
publicclassDextendsA
Page 5 of 12
{
publicvoidmethodD()
{
System.out.println("Class D method ");
}
}
publicclassHInheritance
{
publicstaticvoid main(String args[])
{
B b =newB();
b.methodB();
b.methodA();
C c =newC();
c.methodC();
c.methodA();
D d =newD();
d.methodD();
d.methodA();
}
}
5. Hybrid Inheritance in Java
Page 6 of 12
Method Overriding
When a method in a sub class has same name and type signature as a method in its super
class, then the method in the sub class is said to overriding the method in the super class.
When an overridden method is called from within a sub class, it will always refer to
method defined by the sub class.
The method defined by the super class will be hidden.
class A
{
int i, j;
A(int a, int b)
{
i = a;
j = b;
}
void show()
{
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A
{
int k;
void show()
{
System.out.println("k: " + k);
}
}
Output:
k: 10
Super Keyword
Que:
1. Write a Java Program to explain super keyword.
2. Explain Super keyword in inheritance.
3. State the importance of super keyword in Java.
Ans:
Super keyword is used by subclass to refer to its immediate superclass.
Super class also known as Base class.
Super keyword has two uses:
1.To call the super class constructor
Using Super keyword, a subclass can call a constructor defined by its superclass.
Constructors are called in order of derivation from super class to sub class.
Syntax:
Super(arg-list);
Here,arg-list specifies any arguments needed by the constructor in the superclass.
Super() must always be the first statement executed inside a subclass constructor.
Example
class Box
{
double width;
double height;
double depth;
Page 8 of 12
weight = m;
}
}
OUTPUT
i in superclass:6
i in subclass:4
Page 9 of 12
Dynamic method dispatch is the mechanism by which a call to an overridden method
is resolved at runtime,rather than comile time.This is the way, java implements
runtime polymorphism.
Dynamic method dispatch uses the prinicpal: “Super class reference variable refers to a
sub class object”.
When an overridden method is called by a super class reference, java determines which
version of that method to execute based on the type of object it refer to.
In simple words, the type of object which it referred determines which version of
overridden method will be called.
Example:
classA
{
voidm1()
{
System.out.println("Inside A's m1 method");
}
}
classB extendsA
{
// overriding m1()
voidm1()
{
System.out.println("Inside B's m1 method");
}
}
classC extendsA
{
// overriding m1()
voidm1()
{
System.out.println("Inside C's m1 method");
}
}
classDispatch
{
publicstaticvoidmain(String args[])
{
A a = newA();
B b = newB();
Page 10 of 12
C c = newC();
r = a; // r refers to an A object
}
}
Output:
Inside A's m1 method
Inside B's m1 method
Inside C's m1 method
The Object class is the parent class of all the classes in java by default. In other words,
it is the superclass of all other class.
A reference variable of type Object can refer to an object of any other class.
Object class provides following methods:
Method Purpose
getClass() Obtains the class of an object at run time.
clone() Creates and returns a copy of this object.
equals() Indicates whether some other object is "equal to" this one.
finalize() Called by the garbage collector on an object when garbage
collection determines that there are no more references to the
object.
getClass() Returns the runtime class of an object.
hashCode() Returns a hash code value for the object.
notify() Wakes up a single thread that is waiting on this object's
monitor.
Page 11 of 12
notifyAll() Wakes up all threads that are waiting on this object's monitor.
toString() Returns a string representation of the object.
wait() Causes current thread to wait until another thread invokes the
notify() methodor the notifyAll() method for this object.
Page 12 of 12