Lecture 8-Inheritance and Polymorphism in Java
Lecture 8-Inheritance and Polymorphism in Java
Md. Hasanuzzaman
Lecturer
Department of CSE
Cox’s Bazar International University
Contents
• Inheritance in Java
• Polymorphism in Java
2
Topic - 1 : Inheritance in Java
3
Introduction to Inheritance
• Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object.
• Class that inherits a parent class is called as child class or sub class or derived class
Student is a Person
Car is a Vehicle
Truck is a Vehicle
5
extends Keyword
6
extends Keyword
7
Example – 1
class Employee{ Employee
salary: float
float salary=40000;
}
11
Example – 4
12
public class MyCalculation extends Calculation {
Example – 4: Solution
public void multiplication(int x, int y) {
z = x * y;
public class Calculation {
System.out.println("The product :"+z);
int z;
}
public void division(int x, int y) {
public void addition(int x, int y) {
z = x / y;
z = x + y;
System.out.println("The Division :"+z);
System.out.println("The sum :"+z);
}
}
14
Types of
inheritance
15
Topic - 2 : Method Overriding in Java
16
Method Overriding used in Inheritance
• If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in Java.
17
Rules for Method Overriding
• The method must have the same name as in the parent class
• The method must have the same parameter as in the parent class.
18
class Vehicle{
Let's understand the problem that we void run(){
may face in the program if we don't System.out.println("Vehicle is running");
use method overriding
}
}
Output
Vehicle is running
19
class Vehicle{
Example – 5 void run(){
Method overriding System.out.println("Vehicle is running");
}
}
Java Program to illustrate the
class Bike extends Vehicle{
use of Method Overriding void run(){
System.out.println("Bike is running safely");
}
Output
Bike is running safely 20
Example – 6
Human
+eat() : void
Boy Girl
+main(String[]):void +main(String[]):void
21
‘super’ keyword
• The super keyword is similar to this keyword.
• It is used to differentiate the members of super_class from the members of sub_class if
they have same name.
• It is used to invoke the constructor of super_class from the sub_class.
Output:
This is Sub Class
This is Super Class
24
public class Vehicle {
int speed = 50;
25
UML of Example - 7
Vehicle
# speed : int
+ Vehicle(int)
+ display(): void
Car
- speed : int
+ Car()
+ display(): void
+ main(String) : void
26
public class Car extends Vehicle {
Example – 7 int speed = 100;
public Car()
public class Vehicle { {
super("Red");
int speed = 50;
System.out.println("Car is Created\n");
}
public Vehicle(String color) {
System.out.println("Vehicle is created with color:" + color); public void display()
} {
super.display();
public void display() { System.out.println("Vehicle Speed is: "+super.speed);
System.out.println("The vehicle Speed is: " + speed); System.out.println("Car Speed is: "+speed);
} }
}
public static void main(String[] args) {
# name : String
# age: int
+ Person(String, int)
+ display(): void
Student
- id: int
- cgpa : double
+Student(String, int , int, double)
+display(): void
+main(String) : void
28
Why use inheritance in java
• Code Reusability
• 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.
• Method Overriding
• To provide the specific implementation of a method which is already provided by its
superclass.
29
Inheritance in Java
30
Topic - 3 : Polymorphism in Java
31
Introduction to Polymorphism
32
Thank you!
33