0% found this document useful (0 votes)
34 views

UNIT III Inheritance

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

UNIT III Inheritance

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT-III

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:

// A simple example of inheritance.

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)

1. Single Inheritance in Java

 Single Inheritance is the simple inheritance of all.


 When a class extends another class(Only one class) then we call it as Single
inheritance.
 The below diagram represents the single inheritance in java where Class Bextends
only one class Class A.
Here Class Bwill be the Sub class and Class A will be one and only Super class.

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
}
}

2. Multiple Inheritance in Java

 In Multiple Inheritance, one class extending more than one class.


 Multiple Inheritance is basically not supported by many Object Oriented
Programming languages such as Java, Small Talk, C# etc.
 Java does not support multiple inheritance because the Child class has to manage the
dependency of more than one Parent class.
 But you can achieve multiple inheritance in Java using Interfaces.

3. Multilevel Inheritance in Java

 In Multilevel Inheritance, a derived class will be inheriting a parent class and as


well as the derived class act as the parent class to other class.
 As seen in the below diagram. ClassBinherits the property of ClassA and
again ClassB act as a parent for ClassC. In Short ClassAparent
for ClassB and ClassB parent for ClassC.

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");
}

public static void main(String args[])


{
C obj = new C();
obj.methodA(); //calling grandparent class method

Page 4 of 12
obj.methodB(); //calling parent class method
obj.methodC(); //calling local method
}
}

4. Hierarchical Inheritance in Java

 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.

 Hierarchical Inheritance Example

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

 Hybrid Inheritance is the combination of both Single and Multiple Inheritance.


 Again Hybrid inheritance is also not directly supported in Java only through interface
we can achieve this. Flow diagram of the Hybrid inheritance will look like below.
 As you can ClassA will be acting as the Parent class for ClassB &
ClassC and ClassB & ClassC will be acting as Parent for ClassD.

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;

B(int a, int b, int c)


{
super(a, b);
k = c;
}

void show()
{
System.out.println("k: " + k);
}
}

Public class MultiOverride


{
Public static void main(String args[])
{
B b=new B(10,20,30);

b.show(); //calls show method of class B by overriding show method in class A


}
Page 7 of 12
}

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;

Box (double w, double h, double d)


{
width=w;
height=h;
depth=d;
}

class BoxWeight extends Box


{
double weight;

BoxWeight(double w, double h, double d, double m)


{
super(w, h, d); // call superclass constructor

Page 8 of 12
weight = m;
}
}

2. To access a member of the superclass


 When member names of a subclass and superclass are same then member names of a
sub class hide same members of the superclass.
 In this situation, Super keyword is used to access the super class member.
 Syntax:
Super.member
 member can be either a method or an instance variable.
 Example
Class A
{
int i;
}
Class B extends A
{
int i;
B(int a,int b)
{
Super.i=a;
i=b;
}
Void show()
{
System.out.println(“i in superclass:”+super.i);
System.out.println(“i in subclass:”+i);
}
}
Class usesuper
{
Public static void main(String args[])
{
B ob=new B(6,4);
ob.show();
}
}

OUTPUT
i in superclass:6
i in subclass:4

Dynamic Method Dispatch

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();

A r; // obtain a reference of type A

r = a; // r refers to an A object

r.m1(); // calling A's version of m1()

r= b;// r refers to a B object

r.m1(); // calling B's version of m1()

r = c;// r refers to a C object

r.m1(); // calling C's version of m1()

}
}

Output:
Inside A's m1 method
Inside B's m1 method
Inside C's m1 method

Object class in Java

 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

You might also like