You are on page 1of 17

Object Oriented

Programming with JAVA


SSIT1071
Mrs. Hemangini Amit Patel
P P Savani University
School of Engineering
CE/IT
Unit 4

Class and Inheritance


Agenda/Topics to Be Covered
• Use and Benefits of Inheritance in OOP
• Types of Inheritance in Java
• Inheriting Data Members and Methods
• Role of Constructors in inheritance
• Overriding Super Class Methods
• Use of “super”
• Polymorphism in inheritance
• Type Compatibility and Conversion.
Inheritance
• Inheritance in Java is a mechanism in which one object acquires all
the properties and behaviors of a parent object.
• In short, it is possible to inherit attributes and methods from one class to
another class.
• It is an important part of OOPs (Object Oriented programming system).
• 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 the parent class.
• Moreover, you can add new methods and fields in your current class
also.
• Inheritance represents the IS-A relationship which is also known as
a parent-child relationship.
Inheritance
• The greatest advantage of inheritance is “reusability of code”.
• Base class:
– It is a class from which a new class inherits properties and methods.
– It is also known as base class or parent class.

• Sub Class:
– It is a class, which is derived from an existing class. It is class known as child
class or derived class.
Use and Benefits of Inheritance in
OOP
• In the above diagram data members and methods are
represented in broken line are inherited from faculty
class and they are visible in student class logically.

• Advantage of inheritance

• If we develop any application using concept of


Inheritance than that application have following
advantages,

• Application development time is less.


Note: In Inheritance the scope of access • Application take less memory.
modifier increasing is allow but decreasing is
not allow. • Application execution time is less.
 Suppose in parent class method access
modifier is default then it's present in child • Application performance is enhance (improved).
class with default or public or protected
access modifier but not private(it • Redundancy (repetition) of the code is reduced or
decreased scope). minimized so that we get consistence results and less
storage cost.
Types of Inheritance in Java
• Based on number of ways inheriting the feature of base class into
derived class we have five types of inheritance; they are:
– Single inheritance
– Multiple inheritance
– Hierarchical inheritance
– Multilevel inheritance
– Hybrid inheritance
Single Inheritance
• It is the Process of deriving a new class from a single base class
• Example:
Class A
{
//Body of the Class;
}
Class B extends A
{
//Body of the Class;
}
Single Inheritance
(Example)
Multiple Inheritance
• It is the process of deriving a new class from more than one base class
• Example:
Class A
{
//body of the class;
}
Class B
{
//body of the class;
}
Class C extends A, B Note: Java does not support
{ multiple inheritance with
//body of the class; class.
}  but it supports multiple
inheritance using interfaces.
Multi Level Inheritance
• It is the process of deriving a new class from another deriving class

• Example:  Single base class + single derived


Class A class + multiple intermediate base
{
classes.
//body of the class;
}
Class B extends A
{
//body of the class;
}
Class C extends B
{
//body of the class;
}
Multilevel Inheritance
(Example)
Hierarchical Inheritance
• It is the process of deriving more than one class from a single base
class.
• Example:
Class A
{
//body of the class;
}
Class B extends A
{
//body of the class;
}
Class C extends A
{
//body of the class;
}
Hierarchical Inheritance
Inheriting Data Members and
Methods
Constructor in Inheritance
• Constructor & Inheritance:
– When both super and sub classes are having a constructor, on creating an object for the subclass, base
class constructor is called first then subclasses constructors.

• Example:
Class Parent/Base
{
Parent/Base() {}
}
Class Child/Derived extends Parent/Base
{
Derived() {}
}
Class Constructor_Inheritance
{
public static void main(String[] args)
{
Derived d = new Derived();
}
}
Constructor in Inheritance

You might also like