Escolar Documentos
Profissional Documentos
Cultura Documentos
Inheritance
o Single Inheritance
o Multilevel Inheritance
o Hierarchal inheritance
Super keyword
What is Inheritance?
Extends Keywords
Extends is the keyword used to inherit the properties of a class. Below given is the
syntax of extends keyword.
class Super{
.....
.....
}
Example
Lets consider a superclass Vehicle. Different vehicles have different features and
properties however there few of them are common to all. Speed, color, fuel used, size are
Page 1 of 7
Object Oriented Programming CS2024L Lab 8
few which are common to all. Hence we can create a class Vehicle with states and
actions that are common to all vehicles. The subclass of this superclass can be any type of
vehicle. Example: Class Car A has all the features of a vehicle. But it has its own
attributes which makes it different from other subclasses. By using inheritance we need
not rewrite the code that weve already used with the Vehicle. The subclass can also be
extended. We can make a class Sports Car which extends Car. It inherits the features
of both Vehicle and Car.
class Vehicle {
String color;
int speed;
int size;
void attributes() {
System.out.println("Color : " + color);
System.out.println("Speed : " + speed);
System.out.println("Size : " + size);
}
}
The output is
Page 2 of 7
Object Oriented Programming CS2024L Lab 8
Note:
The derived class inherits all the members and methods that are declared as public or
protected. If declared as private it cannot be inherited by the derived classes. The private
members can be accessed only in its own class. The private members can be accessed
through assessor methods as shown in the example below. The derived class cannot
inherit a member of the base class if the derived class declares another member with the
same name.
Single Inheritance
Single inheritance is easy to understand. When a class extends another one class only
then we call it a single inheritance. The below flow diagram shows that class B extends
only one class which is A. Here A is a parent class of B and B would be a child class of
A.
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[])
{
Page 3 of 7
Object Oriented Programming CS2024L Lab 8
Multilevel Inheritance
Class X
{
public void methodX()
{
System.out.println("Class X method");
}
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Page 4 of 7
Object Oriented Programming CS2024L Lab 8
Hierarchical Inheritance
In such kind of inheritance one class is inherited by many sub classes. In below example
class B,C and D inherits the same class A. A is parent class (or base class) of B,C & D.
Read More at Hierarchical Inheritance in java with example program.
Class A
{
public void methodA()
{
System.out.println("method of Class A");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
Class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
Class D extends A
{
public void methodD()
{
System.out.println("method of Class D");
}
}
Class MyClass
{
public void methodB()
{
System.out.println("method of Class B");
Page 5 of 7
Object Oriented Programming CS2024L Lab 8
}
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}
Super keyword
The super keyword is similar to this keyword following are the scenarios where the super
keyword is used.
Exercise
Q1 Write a parent class Account that extends class name saving account and saving
account extends account details class.
Saving account
Minimum bill (int)
Saving Bill (int)
Account Details
Deposits (int)
Widthdrawls(int)
In the main make the object of account detail and display the details of the account
Page 6 of 7
Object Oriented Programming CS2024L Lab 8
Q#2:
Create a class named Trigon inherited from the class GeometricShape (containing a
single data member named shapeName (a string) and a member function named
setShapeName(shapeName). The class Trigon is required to hold:
Three data members i.e. base, perpendicular, and hypotenuse of type double
A no-arg constructor that initializes all data members with value 1.0
A parameterized constructor to initialize all data fields with user-defined values
The setter functions for all three data fields
The accessor functions for all three data fields
A function named displayArea() that shows the area of a certain Trigon object
In the main() function make three objects of class Trigon while considering the
shapeName as threeangle for each object. In addition, it is required to invoke all the
functions of Trigon class.
Instructor:
Qazi Shuja Ud Din,
Faculty of Computing, Riphah International University at Islamabad
Email:qazi.shujauddin@riphah.edu.pk
Page 7 of 7