Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views24 pages

PSOOP - Lecture 05-Inheritance in Java-2024-25

The document provides an overview of inheritance in Java, detailing its types, such as single, multi-level, multiple, hierarchical, and hybrid inheritance. It explains how inheritance allows a class to utilize properties and methods from another class, enhancing code reusability, and includes examples of class structures and constructors. Additionally, it discusses the use of the 'super' keyword to access superclass members and constructors in inheritance scenarios.

Uploaded by

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

PSOOP - Lecture 05-Inheritance in Java-2024-25

The document provides an overview of inheritance in Java, detailing its types, such as single, multi-level, multiple, hierarchical, and hybrid inheritance. It explains how inheritance allows a class to utilize properties and methods from another class, enhancing code reusability, and includes examples of class structures and constructors. Additionally, it discusses the use of the 'super' keyword to access superclass members and constructors in inheritance scenarios.

Uploaded by

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

INHERITANCE IN JAVA

-Compiled by Nikahat Mulla


Agenda

• Inheritance
• Introduction
• Types of Inheritance
• Example and Syntax
• Inheritance without constructors
• Inheritance with constructors
• super keyword uses

2
Inheritance
• Capability of a class to use the properties & methods of another class
while adding its own functionality

• A class derived from another class is called as subclass / derived


class / extended class / child class

• The class from which the subclass is derived is called as superclass /


base class / parent class

• Each class is allowed to have one direct superclass

• Each superclass can have unlimited number of subclasses


Inheritance (Contd…)
Super Class

Sub Class

Sub Class
Types of inheritance
• Single
• Multi-level
• Multiple
• Hybrid
Single Inheritance
Single Parent and single child class derived
from the parent
Multilevel Inheritance
• Class B is derived from class A and
• class C is derived from class B

Multilevel Inheritance
Multiple Inheritance

Phone Camera

Smart Phone
Hierarchical Inheritance

Person

Student Teacher

Full-Time Part-Time
Hybrid Inheritance
• Hybrid inheritance is a combination of two
or more types of inheritance discussed earlier
What inheritance does Java support?
• Java supports single, multi-level, hierarchical
inheritance through classes
• Java doesn’t support Multiple Inheritance (One
sub class deriving from more than one super
classes) through classes but through interfaces
• Hybrid Inheritance is only supported if it’s a
combination of single/multi-level/hierarchical
• Each class is allowed to have one direct
superclass
• Each superclass can have unlimited number
of subclasses
Example 1
• Consider a Mobile Phone Model: Example:
Redmi 9
• If I want to add more features to Redmi 9 to
develop my new model, Redmi 9 Pro, I reuse
the basic design of Redmi 9 and add the
additional features, rather than creating
Redmi 9 Pro from scratch.
• This concept is called Inheritance
• Inheritance facilitates reusability.
How is inheritance achieved in Java?
• Using the extends keyword
class Parent{
….
}

class Child extends Parent{


}
Problem Statement 1:
• Consider a class called Shape with member
color of the shape. Consider a class called
Rectangle, derived from the Shape class, which
adds features length and breadth, add a
method to print the rectangle’s details and find
the area of the rectangle. Use appropriate
access modifiers.
Scenario 1: Without Constructors
Shape

private String color

public void setColor(String c); Rectangle


public String getColor();
private float length
private float breadth
color

IS A public void setLength(float l));


public float getLength();
public void setBreadth(float b);
public float getBreadth();
public void setColor()
public String getColo()
Scenario 2: With Constructors
Shape

Rectangle
private String color

private float length


public Shape(); private float breadth
public Shape(String color); color
public void setColor(String c);
public String getColor(); public Rectangle();
public Rectangle(float l,float b);
public void setLength(float l));
IS A public float getLength();
public void setBreadth(float b);
public float getBreadth();
public void setColor()
public String getColor()
Example 2: Constructing a Table
• Consider a class called TableTop. It has
features: color, dimensions of table.
• Let’s say you want to now create Table
objects.
• One way to do this is, since, we already have
TableTop class we can reuse it using
inheritance, add features to it to make the
complete Table.
• Let’s Design it.
Class TableTop  Class Table
class TableTop{ class Table extends TableTop{
private float length,breadth; int no_legs;
private String color; float height;
TableTop(){ Table(){
no_legs=4;
length=breadth=1;
height=2.5f;
color="Black"; }
} Table(int nl,float h,float l,float b,String c){
TableTop(float l,float b,String super(l,b,c);
c){ no_legs=nl;
color=c; height=h;
length=l; }
breadth=b; public void print(){
System.out.printf("Area=%.2f\n",area());
}
System.out.println("No. of legs="+no_legs);
public float area(){ System.out.printf("Height=%.2f\n",height);
return length*breadth; System.out.println("Color="+getColor());
} }
public String getColor(){ }
return color;
}
}
Main Class for testing
class TestTable{
public static void main(String []args){
Table t1=new Table(3,4,5,6,"Gray");
t1.print();
Table t2=new Table();
t2.print();
}
}
Constructors in Inheritance
• Constructors are invoked in the order of hierarchy

• While instantiating a sub class, its super class default constructor will
be invoked first, followed by the sub class constructor

• The keyword super can be used to invoke the super class


parameterized constructor instead of the default

• Remember that:
• super() call must occur as the first statement in constructor
• super() call can only be used in a constructor definition
Using super
• The keyword super refers to members of the
super class

• Used when member names of the subclass


hide members by the same name in the super
class

• super.member
• member can be either a method or an instance variable
Thank You

You might also like