Você está na página 1de 7

Object Oriented Programming CS2024L Lab 8

Topics for todays lab

Inheritance
o Single Inheritance
o Multilevel Inheritance
o Hierarchal inheritance
Super keyword

What is Inheritance?

Inheritance is one of the feature of Object-Oriented Programming (OOPs).


Inheritance allows a class to use the properties and methods of another class. In
other words, the derived class inherits the states and behaviors from the base
class. The derived class is also called subclass and the base class is also known as
super-class. The derived class can add its own additional variables and methods.
These additional variable and methods differentiates the derived class from the
base class.

Inheritance is a compile-time mechanism. A super-class can have any number of


subclasses. But a subclass can have only one superclass. This is because Java does
not support multiple 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{
.....
.....
}

class Sub extends Super{


.....
.....
}

public class ChildClass extends BaseClass {


// derived class methods extend and possibly override
}

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);
}
}

class Car extends Vehicle {


int CC;
int gears;
void attributescar() {
// The subclass refers to the members of the superclass
System.out.println("Color of Car : " + color);
System.out.println("Speed of Car : " + speed);
System.out.println("Size of Car : " + size);
System.out.println("CC of Car : " + CC);
System.out.println("No of gears of Car : " + gears);
}
}

public class Test {


public static void main(String args[]) {
Car b1 = new Car();
b1.color = "Blue";
b1.speed = 200 ;
b1.size = 22;
b1.CC = 1000;
b1.gears = 5;
b1.attributescar();
}
}

The output is

Page 2 of 7
Object Oriented Programming CS2024L Lab 8

Color of Car : Blue


Speed of Car : 200
Size of Car : 22
CC of Car : 1000
No of gears of Car : 5

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

B obj = new B();


obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}

Multilevel Inheritance

Multilevel inheritance refers to a mechanism in OO technology where one can inherit


from a derived class, thereby making this derived class the base class for the new class.
As you can see in below flow diagram C is subclass or child class of B and B is a child
class of A. For more details and example refer Multilevel inheritance in Java.

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

Z obj = new Z();


obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling local method
}
}

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.

It is used to differentiate the members of superclass from the members of


subclass, if they have same names.
It is used to invoke the superclass constructor from subclass.

Exercise

Q1 Write a parent class Account that extends class name saving account and saving
account extends account details class.

Member variables of parent and derived classes are:


Account
Customer Name (String)
Account Number (Integer)

Saving account
Minimum bill (int)
Saving Bill (int)

Account Details
Deposits (int)
Widthdrawls(int)

In each class make parameterize constructor and display function.


Drive classes invoke super class constructor and method using super keyword

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

Você também pode gostar