0% found this document useful (0 votes)
2 views33 pages

Lecture 8-Inheritance and Polymorphism in Java

The lecture covers inheritance and polymorphism in Java, explaining how inheritance allows one class to acquire properties from another using the 'extends' keyword. It discusses method overriding as a form of runtime polymorphism and provides examples to illustrate these concepts. Additionally, it highlights the benefits of inheritance, such as code reusability and the ability to provide specific implementations of methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views33 pages

Lecture 8-Inheritance and Polymorphism in Java

The lecture covers inheritance and polymorphism in Java, explaining how inheritance allows one class to acquire properties from another using the 'extends' keyword. It discusses method overriding as a form of runtime polymorphism and provides examples to illustrate these concepts. Additionally, it highlights the benefits of inheritance, such as code reusability and the ability to provide specific implementations of methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Lecture – 8

Inheritance and Polymorphism in Java

Md. Hasanuzzaman
Lecturer
Department of CSE
Cox’s Bazar International University
Contents
• Inheritance in Java

• Method Overriding in Inheritance

• 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.

• It is an important feature of OOPs.

• Inherited class is called as parent class or super class or base class

• Class that inherits a parent class is called as child class or sub class or derived class

• ‘extends’ keyword is used to inherite a class

• Inheritance represents the IS-A relationship which is also known as a parent- 4


IS-A relationship

Super Class/Parent Class/Base Class

Sub Class/Child Class/Derived Class

Student is a Person
Car is a Vehicle
Truck is a Vehicle

5
extends Keyword

extends is the keyword used to inherit the properties of a class.

class SuperClass{ SuperClass


.....
.....
} extends

class SubClass extends SuperClass{


..... SubClass
}

6
extends Keyword

class Person{ Try by Yourself


……
}

class Student extends Person{


………
}

7
Example – 1
class Employee{ Employee
salary: float
float salary=40000;
}

class Programmer extends Employee{ extends


float bonus=10000;
Programmer
public static void main(String args[]){ bonus: float
Programmer p=new Programmer();
+main(String[]):void
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
8
}
Example – 1: Output
class Employee{
float salary=40000; Output:
}
Programmer salary is: 40000.0
Bonus of programmer is: 10000.0

class Programmer extends Employee{


float bonus=10000;

public static void main(String args[]){


Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
9
}
public class Teacher {
Example – 2 String designation = “Lecturer";
String uniName = “CBIU";
public void job()
Teacher Output:
{
designation: String
uniName: String System.out.println("Teaching"); CBIU
+job() : void } Lecturer
} CSE
Teaching
public class CseTeacher extends Teacher{
String mainSubject = “CSE";

CseTeacher public static void main(String[] args){


mainSubject: String CseTeacher obj = new CseTeacher();
System.out.println(obj.uniName);
+main(String[]):void System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.job();
}
10
}
Example
Example–-33

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

public static void main(String[] args) {


public void subtraction(int x, int y) {
int a = 20, b = 10;
z = x - y;
MyCalculation demo = new MyCalculation();
System.out.println("The difference :"+z);
demo.addition(a, b);
} demo.Subtraction(a, b);
} demo.multiplication(a, b);
demo.division (a, b);
}
13
}
Types of
inheritance

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.

• In other words, if a subclass provides the specific implementation of the


method that has been declared by one of its parent class, it is known as
method overriding.

• Method overriding is used for runtime polymorphism

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.

• There must be an IS-A relationship (inheritance)

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

class Bike extends Vehicle{


public static void main(String args[]){
Bike bike = new Bike();
bike.run();
}
}

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

public static void main(String args[]){


Bike bike = new Bike (); //creating object
bike.run(); //calling method
}
}

Output
Bike is running safely 20
Example – 6

Human

+eat() : void

Boy Girl

+eat() : void +eat() : void

+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.

Usage of super Keyword


1. super is used to refer immediate parent class instance variable.
super.variable;

2. super is used to invoke immediate parent class method.


super.method();

3. super() is used to invoke immediate parent class constructor.


super(); 22
1) super is used to refer immediate parent class instance variable
If a class is inheriting the properties of another class and if the members of the superclass have the
names same as the sub class, to differentiate these variables we use super keyword as shown below.
Problem without super keyword Solution by super keyword
public class Vehicle{ class Vehicle{
int speed=50; int speed = 50;
} }

public class Bike extends Vehicle{ class Bike extends Vehicle{


int speed=100; int speed =100;
void display() void display(){
{ System.out.println(super.speed);
System.out.println(speed); }
} public static void main(String args[]){
public static void main(String args[]){ Bike b = new Bike();
Bike b=new Bike(); b.display();
b.display(); }
} }
}
Output: ?
Output: ? 23
public class Vehicle {
int speed = 50;
2) super is used to refer
public void display() {
immediate parent class method System.out.println("\nVehicle Speed = " +speed);
}
}

The super keyword can also be used to


invoke parent class method. It should be public class Car extends Vehicle {
public void display() {
used in case subclass contains the same super.display();
method as parent class as in the example System.out.println("Car Speed = " + super.speed);
}
given below
public static void main(String[] args) {
Car c1 = new Car();
c1.display();
}
}

Output:
This is Sub Class
This is Super Class
24
public class Vehicle {
int speed = 50;

3) super is used to invoke parent public Vehicle(int speed) {


System.out.println("Vehicle is created");
class constructor }
this.speed = speed;

If a class is inheriting the properties of public class Car extends Vehicle {


public Car() {
another class, the subclass automatically super(100);
acquires the default constructor of the System.out.println("Car is created");
}
super class. But if you want to call a
parametrized constructor of the super public void display() {
System.out.println("\nVehicle Speed = " + super.speed);
class, you need to use the super keyword System.out.println("Car Speed = " +speed);
as shown below. }

public static void main(String[] args) {


Car c1 = new Car();
c1.display();
}
}
Output:
Vehicle is created
Car is created
Vehicle Speed = 100
Car Speed = 100

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) {

Car c1 = new Car();


c1.display();
}
}
27
Example – 8
Person

# 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

Two types of Polymorphism

1. Compile-time Polymorphism : Method Overloading

2. Run-time Polymorphism : Method Overriding

32
Thank you!

33

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy