You are on page 1of 20

OBJECT ORIENTED

PROGRAMMING
LECTURE # 7: TYPES OF
INHERITANCE

Lecturer Muhammad Khalid


Muhammad.khalid@hitecuni.edu.pk

Department of CS
1 BSSE-2
Hitec University Taxila
TYPES OF INHERITANCE (1/4)
2

• On the basis of class, there can be following types of


inheritance in java:
TYPES OF INHERITANCE (2/4)
3

• When one class inherits multiple classes, it is known


as multiple inheritance. For Example:

• Java doesn’t allow multiple


inheritance.
• Through Java interfaces we can achieve multiple
inheritance.
SINGLE INHERITANCE (CONCEPT)
4

• Refers to a child and parent class relationship where


a class extends the another class.
• In single inheritance, the features and methods of
the superclass are inherited by a single subclass.
• Class Mango extends Fruit, we can say
Mango IS-A Fruit
• Tightly coupled (disadvantage)
Class Fruit

Class Mango
SINGLE INHERITANCE (SYNTAX)
5

class Fruit
{
//methods and fields
}
class Mango extends Fruit
{
//methods and fields
}
SINGLE INHERITANCE (EXAMPLE)
6

//Base-class
class Fruit
{
void fruitname()
//Main Program
{
System.out.println("I am a Fruit");
} public class SinglelevelInheritance
} {
public static void main(String args[])
{
//Derived-class Mango obj = new Mango();
class Mango extends Fruit
obj.fruitname();
{
obj.taste();
void taste()
}
{
System.out.println("My taste is sweet"); }
}
}
MULTILEVEL INHERITANCE (CONCEPT)
7

• Multilevel inheritance refers to a


mechanism in OO technology where Class A
one can inherit from a derived class,
thereby making this derived class
the base class for the new class.
Class B
• We can see from the diagram C is
subclass or child class of B and B is
a child class of A.
Class C
MULTILEVEL INHERITANCE (SYNTAX)
8

class X
{
//methods and fields
}
class Y extends X
{
//methods and fields
}
class Z extends Y
{
//methods and fields
}
MULTILEVEL INHERITANCE (EXAMPLE)
9

Class X
{
public void methodX()
{ //Main Program
System.out.println("Class X method"); public static void main(String args[])
}
{
}
Class Y extends X Z obj = new Z();
{ //calling grand parent class method
public void methodY() obj.methodX();
{ //calling parent class method
System.out.println("class Y method"); obj.methodY();
}
}
//calling local method
Class Z extends Y obj.methodZ();
{ }
public void methodZ()
{
System.out.println("class Z method");
}
HIERARCHICAL INHERITANCE (CONCEPT)
10

• Hierarchical
inheritance When two or
Class X
more classes inherits a
single class, it is known
as hierarchical
inheritance.
Class Y Class Z
• In the example given
below, Y and Z classes
inherits the X class, so
there is hierarchical
inheritance.
HIERARCHICAL INHERITANCE (SYNTAX)
11

class X
{
//methods and fields
}
class Y extends X
{
//methods and fields
}
class Z extends X
{
//methods and fields
}
HIERARCHICAL INHERITANCE (EXAMPLE)
12

class X //Base Class


{
void eat() //Main Program
{System.out.println("eating...");} public static void main(String ar
} gs[]){
class Y extends X // First Child Class
Z c=new Z();
{
void bark() c.meow();
{System.out.println("barking...");} c.eat();
} //c.bark();//Error
class Z extends X //Second Child Class }}
{
void meow()
{System.out.println("meowing...");}
}
OTHER TYPES OF INHERITANCE
13

Multiple Inheritance:
When one class extends more than one classes then
this is called multiple inheritance. For example:
Class C extends class A and B then this type of
inheritance is known as multiple inheritance.
Java doesn’t allow multiple inheritance. We will discuss
later why java doesn’t allow multiple inheritance and
how we can use interfaces instead of classes to achieve
the same purpose.
Hybrid Inheritance:
Do yourself?
THIS AND SUPER KEYWORD IN JAVA

In Java, this and super are both keywords used to


reference objects and access their members
(variables and methods), but they serve different
purposes.

14
THIS KEYWORD

This refers to the current instance of the class in


which it appears.
It is primarily used to differentiate between
instance variables and parameters with the same
name, particularly in constructors and methods.
It can also be used to invoke other constructors of
the same class using this().

15
CODE
class Main
class Test {
{ public static void main (String[] args)
int i; {
void setValue(int j) Test obj = new Test();
{ obj.setValue(10);
i=j; obj.show()
} }
void show()
{
System.out.println(i);
}
16
THIS KEYWORD (CODE)

public class MyClass {


private int num;

public MyClass(int num) {


this.num = num; // Here, "this.num" refers to the
instance variable num, and "num" refers to the
parameter num.
}

public void display() {


System.out.println("Value of num: " + this.num); //
Accessing instance variable using "this"
}
} 17
SUPER KEYWORD

Super keyword is used to refer to the superclass


(parent class) of the current object.
It is used to access superclass methods and
constructors, particularly when they are
overridden or to differentiate between superclass
and subclass members.

18
SUPER KEYWORD (CODE)

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}}
class Dog extends Animal {
void sound() {
super.sound(); // Calling superclass method
System.out.println("Dog barks");
}}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}} 19
USES OF "THIS" KEYWORD
1. This keyword can be used to to refer current
class instance variable.
2. This keyword can be used to invoke current class
method (implicitly).
3. This() can be used to invoke current class
constructor.
4. This can be used to pass as an argument in the
method call.
5. This can be used to pass as an argument in the
constructor call.
6. This can be used to return the current class 20
instance from the method.

You might also like