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

Inheritance Part-1

The document provides an overview of inheritance in Java, explaining its definition, purpose, and key terms such as superclass and subclass. It discusses different types of inheritance including single, multilevel, and hierarchical inheritance, while also addressing the limitations of multiple inheritance in Java. Additionally, it highlights the importance of inheritance in achieving code reusability and reducing redundancy in object-oriented programming.

Uploaded by

shahmeasum007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Inheritance Part-1

The document provides an overview of inheritance in Java, explaining its definition, purpose, and key terms such as superclass and subclass. It discusses different types of inheritance including single, multilevel, and hierarchical inheritance, while also addressing the limitations of multiple inheritance in Java. Additionally, it highlights the importance of inheritance in achieving code reusability and reducing redundancy in object-oriented programming.

Uploaded by

shahmeasum007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Inheritance Part-1

JAVA
Contents
2

 Recap of previous lecture


 What is inheritance
 Why we use inheritance
 Terms used in inheritance
 Types of inheritance
Recap
3

 Private, public and static inner classes


What is Inheritance (1/2)
4

• Inheritance in Java is a mechanism in java by which one class


is allow to inherit the features(fields and methods) of
another class.

• It is a mechanism in which one object acquires all the


properties and behaviors of a parent object.

• Inheritance is an important pillar of OOP(Object Oriented


Programming). This concept represents the IS-A relationship,
also known as parent-child relationship.
What is Inheritance (2/2)
5
Why we use Inheritance
6

• For method overriding (so runtime polymorphism can be


achieved)

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

• Reduce redundancy using relationships b/w classes like


inheritance (IS-A) and association (HAS-A).
Terms used in Inheritance (1/2)
7

• Class: A class is a group of objects which have common


properties. It is a template or blueprint from which objects
are created.

• Super Class/Parent Class/Base Class: Superclass is the class


from where a subclass inherits the features. It is also called a
base class or a parent class.

• Sub Class/Child Class/Derived Class: Subclass is a class which


inherits the other class. It is also called a derived class,
extended class, or child class.
Terms used in Inheritance (2/2)
8

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

• The super keyword in Java is a reference variable which is


used to refer immediate parent class object. There are
different usage of super keyword and which will discuss later.

• The implements is a keyword which is used to implement an


interface in Java.
Types of Inheritance (1/4)
9

• On the basis of class, there can be three types of inheritance


in java:
Types of Inheritance (2/4)
10

• 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.
Important points (1/2)
11

• Inheritance in Java is done using:


• Extends: in case of Java class and abstract class.
• Implements: In case of Java interface.

• In Java when class is extended, sub-class inherits all the public,


protected and default (only if the sub-class is located in the same
package as the super-class) methods and data of the super class.

• The protected keyword is an access modifier used for attributes,


methods, and constructors, making them accessible in the same
package and sub-class.
Important points (2/2)
12

• Private data and methods of the super-class are not inherited by


the sub-class and can not accessed directly by the sub-class.

• Constructors of the super class are not inherited but constructor of


the superclass can be invoked from the subclass.
Single Inheritance (Concept)
13

• 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 Fruit
• Class Mango extends Fruit, we can say
Mango IS-A Fruit

• Tightly coupled (disadvantage) Class Mango


Single Inheritance (Syntax)
14

class Fruit
{
//methods and fields
}
class Mango extends Fruit
{
//methods and fields
}
Single Inheritance (Example)
15

//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)
16

• Multilevel inheritance refers to a


mechanism in OO technology where one Class A
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)
17

class X
{
//methods and fields
}
class Y extends X
{
//methods and fields
}
class Z extends Y
{
//methods and fields
}
Multilevel Inheritance (Example)
18

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)
19

• Hierarchical inheritance When two or


more classes inherits a single class, it is
known as hierarchical inheritance.

• In the example given below, Y and Z Class X


classes inherits the X class, so there is
hierarchical inheritance.

Class Y Class Z
Hierarchical Inheritance (Syntax)
20

class X
{
//methods and fields
}
class Y extends X
{
//methods and fields
}
class Z extends X
{
//methods and fields
}
Hierarchical Inheritance (Example)
21

class X //Base Class


{
void eat()
{System.out.println("eating...");}
} //Main Program
class Y extends X // First Child Class public static void main(String args[]){
{ 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
22

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?
If you feel any problem ask the questions regarding this concept during the online
discussion session. (MS Team)
References
23

• Absolute JAVA Fifth Edition by Walter Savitch (Chapter-7)


• https://www.javatpoint.com/inheritance-in-java
• https://www.geeksforgeeks.org/inheritance-in-java/
• https://beginnersbook.com/2013/03/inheritance-in-java/
• https://
www.slideshare.net/AdilAslam4/inheritance-and-its-type-in-ja
va
24

THANK YOU

You might also like