Revolutionizing B.
Tech
Course Name: Object
Oriented Programming
using Java
Total Hours : 45
Module 3: Inheritance and
Polymorphism
Course Name: Object Oriented Programming [25ESCS03]
Total Hours : 6 + 6
Table of Content
• Aim – Understanding the purpose and • Did You Know*
efficacy of object oriented programming • Summary
• Introduction • Terminal Questions
• Objective – unit wise split into 3 to 5 • Reference Links*
• Sections/Topics • Q&A*
• Self Assessments – 3 per section • Thank You
• Activities – 1 per section
*not mandatory
Aim
To equip students in the fundamentals and understanding of
object oriented paradigm and its efficacy in programming large
projects
a. Explain the fundamentals of software engineering to
understand the need of object oriented
programming
b. Understanding the java language fundamentals to
implement object oriented programming
Objectiv
e c. Understanding the components of Java language
and its various keywords to create an object
oriented paradigm
Course Outcome (Cos)
On completion of the course the student will be able to:
CO1: Understand Java syntax, object-oriented principles, and control
structures.
CO2: Implement object-oriented concepts using classes, objects, and
constructors.
CO3: Apply inheritance, polymorphism, and dynamic method dispatch in
Java.
CO4: Design modular Java applications using interfaces, abstract classes,
and packages.
CO5: Handle exceptions, file I/O, and apply collections in Java
applications.
CO6: Analyze and implement advanced Java programming features such
as multi-threading, interfaces, wrapper classes, and package structuring for
Module 3
Inheritance and Polymorphism – Inheritance - Single,
multilevel, hierarchical inheritance, Syntax and method
reuse, Method Overriding - Redefining methods, Access
modifiers and annotations, Polymorphism - Compile-time vs
Runtime, Method overloading vs overriding, super, final -
Accessing parent class constructors/methods, Restricting
inheritance/overriding.
Module 2
Lab Practice: (6 Hours)
1. Lab exercise 1: Implement single and multilevel
inheritance (Employee) (Inheritance hierarchy)
2. Lab exercise 2: Demonstrate method overriding in
subclass (Runtime polymorphism)
3. Lab exercise 3: Use super to access parent class
members (Keyword usage)
4. Lab exercise 4: Write a program that uses dynamic
method dispatch (Late binding and reference
polymorphism)
What is Inheritance?
• Definition:
• Inheritance is a mechanism where one class acquires the properties
(variables) and behaviors (methods) of another class.
• The "IS-A" Relationship: Inheritance represents an "IS-A" relationship.
• Example: A Car is a Vehicle. A Surgeon is a Doctor.
• Key Terminology:
• Super Class (Parent/Base Class): The class whose features are
inherited.
• Sub Class (Child/Derived Class): The class that inherits the other
class.
Why Use Inheritance?
1. Code Reusability (Method Reuse):
This is the primary reason for [Link] can reuse fields and
methods of the existing class without having to rewrite the same code in
the new class.
2. Extensibility:
You can add new features to a child class without modifying the parent
class.
3. Method Overriding:
Inheritance is strictly required to achieve Runtime Polymorphism
(Method Overriding).
Inheritance
class Parent
{
// properties and methods
}
class Child extends Parent
{
// additional properties and methods
}
Single Inheritance
class Employee
{
float salary = 40000;
}
class Programmer extends Employee
{
int bonus = 10000;
}
public class MainClass
{
public static void main(String[] args)
{
Programmer p = new Programmer();
[Link]("Programmer salary is: " + [Link]);
[Link]("Bonus of Programmer is: " + [Link]);
}
}
Types of inheritance
There are five types of inheritance. Java Single Inheritance
1. Single Inheritance Class A
2. Multilevel Inheritance
extends
3. Hierarchical Inheritance
4. Multiple Inheritance Class B
5. Hybrid Inheritance
In single inheritance, a single
subclass extends from a single
superclass.
Multilevel Inheritance Hierachical Inheritance
Class A Class A
extends
extends
Class B Class B Class C
extends In hierarchical inheritance, multiple
subclasses extend from a single
Class C superclass
In multilevel inheritance, a subclass
extends from a superclass and
then the same subclass acts as a
superclass for another class
Multiple Inheritance Hybrid Inheritance
Class A
extends
Class A Class B Class B Class C
extends extends
Class C Class D
• In multiple inheritance, a single • Hybrid inheritance is a combination of two
subclass extends from multiple or more types of inheritance
superclasses
• Here, we have combined hierarchical and
• Java doesn't support multiple
inheritance. However, we can multiple inheritance to form a hybrid
achieve multiple inheritance inheritance
using interfaces
Single Inheritance
class Animal public class MainClass
{ {
void eat() public static void main(String[] args)
{ {
[Link]("Animal is eating"); Dog d = new Dog();
} [Link]();
} [Link]();
}
class Dog extends Animal }
{
void bark()
{
[Link]("Dog is barking");
}
}
Multilevel Inheritance
class Animal {
void eat() public class MainClass
{ {
[Link]("Animal is eating"); public static void main(String[] args)
} {
} Puppy p = new Puppy();
class Dog extends Animal [Link]();
{ [Link]();
void bark() [Link]();
{ }
[Link]("Dog is barking"); }
}
}
class Puppy extends Dog
{
void weep()
{
[Link]("Puppy is weeping");
}
}
Hierarchical Inheritance
class Animal { public class MainClass
void eat() {
{ public static void main(String[] args)
[Link]("Animal is eating"); {
} Dog d = new Dog();
} Cat c = new Cat();
class Dog extends Animal
{ [Link]();
void bark() [Link]();
{
[Link]("Dog is barking"); [Link]();
} [Link]();
} }
class Cat extends Animal }
{
void meow()
{
[Link]("Cat is meowing");
}
}
Method Reuse
Concept:
• A subclass automatically inherits the non-private methods of its
superclass.
Benefit:
• You write the method once in the parent class and call it using the
child class object, eliminating duplicate code.
Method Overriding (Runtime Polymorphism)
• Method Overriding is when a child class provides its own specific
implementation of a method that is already defined in its parent class.
• Rules for Method Overriding:
• The method name must be the same.
• The parameter list must be the same.
• There must be an IS-A relationship (inheritance).The return type
must be the same or a subtype.
Method Overriding (Runtime Polymorphism)
class Animal public class MainClass
{ {
void sound() public static void main(String[] args)
{ {
[Link]("Animal makes a sound"); Dog d = new Dog();
} [Link]();
}
Animal a = new Animal();
class Dog extends Animal [Link]();
{
@Override }
void sound() }
{
[Link]("Dog barks");
} Output:
}
Dog barks
Animal makes a sound
Method Overriding (Runtime Polymorphism)
• What is an Annotation?
• An annotation is a special label/tag attached to a method, class, or
variable that gives extra information to the compiler.
• @Override Annotation:
• Tells the compiler that this method is intentionally overriding a parent
class method.
• If the method signature does NOT match the parent, the compiler
throws an error immediately.
• Prevents accidental mistakes like typos in method names.
• Best Practice: Always use @Override when overriding methods.
Access Modifiers
• Access modifiers determine the visibility of variables and methods from
the parent class to the child class.
• public: Accessible everywhere. Inherited by subclasses.
• protected: Accessible within the same package, and in subclasses
(even in different packages). Ideal for inheritance.
• default (no modifier): Accessible only within the same package.
Inherited only if the subclass is in the same package.
• private: Accessible only within the class it is declared. Never
inherited by subclasses.
Access Modifiers
• Access Modifiers control the visibility and accessibility of classes,
methods, and variables.
Access Modifiers
class Employee { public class MainClass {
public String name = "John"; public static void main(String[] args) {
// accessible everywhere
private String company = "TechCorp"; Programmer p = new Programmer();
// only inside Employee [Link]();
protected double salary = 50000; }
// accessible in subclass }
public String getCompany() {
return company; Output:
}
} Name: John
class Programmer extends Employee { Company: TechCorp
Salary: 50000.0
void display() {
[Link]("Name: " + name);
[Link]("Company: " + getCompany());
[Link]("Salary: " + salary);
}
}
Polymorphism
Polymorphism
• Definition:
• Polymorphism means "many forms". It is the ability of a single method
or object to behave differently based on the context.
• Real-life Analogy:
• A person behaves differently as a Son, Employee, and Friend — same
person, different behavior.
• A Dog sounds differently from a Cat, but both are Animals.
• Two Types of Polymorphism in Java:
• Compile-time Polymorphism (Static) → Method Overloading
• Runtime Polymorphism (Dynamic) → Method Overriding
1. Compile-time Polymorphism
• Definition:
• When the method to be called is determined at compile time by the
Java compiler, it is called Compile-time Polymorphism.
• Also Known As: Static Polymorphism / Early BindingAchieved By:
Method Overloading
• How it works: The compiler looks at the method name + parameters and
decides which method to call before the program runs.
• Key Point: Same method name, but different number or type of
parameters in the same class.
1. Compile-time Polymorphism
class Calculator public class MainClass
{ {
int add(int a, int b) public static void main(String[] args)
{ {
return a + b; Calculator calc = new Calculator();
} [Link]("Sum (2 ints): " + [Link](10, 20));
int add(int a, int b, int c) [Link]("Sum (3 ints): " + [Link](10, 20, 30));
{ [Link]("Sum (doubles): " + [Link](1.5, 2.5));
return a + b + c; }
} }
double add(double a, double b)
{
return a + b; Output:
}
} Sum (2 ints): 30
Sum (3 ints): 60
Sum (doubles): 4.0
2. Runtime Polymorphism
• Definition:
• When the method to be called is determined at runtime based on the
actual object, it is called Runtime Polymorphism.
• Also Known As: Dynamic Polymorphism / Late Binding / Dynamic
Method Dispatch
• Achieved By: Method Overriding
• How it works: A parent class reference variable holds a child class
object. The JVM decides at runtime which overridden method to execute.
2. Runtime Polymorphism
class Animal { public class MainClass {
void sound() { public static void main(String[] args) {
[Link]("Animal makes a sound"); Animal a1 = new Animal();
} [Link]();
}
class Dog extends Animal { Animal a2 = new Dog(); // Parent ref, Child object
@Override [Link](); // Dog's method called at runtime
void sound() { }
[Link]("Dog barks"); }
}
}
Output:
Animal makes a sound
Dog barks
Compile-time vs Runtime Polymorphism
Feature Compile-time Polymorphism Runtime Polymorphism
Dynamic Binding / Late
Also Known As Static Binding / Early Binding
Binding
Mechanism Method Overloading Method Overriding
During Compilation (by the
When is it resolved? During Execution (by the JVM)
Compiler)
Less flexible, execution is Highly flexible, execution is
Flexibility
faster. slightly slower.
Yes (Strictly requires IS-A
Inheritance Required? No
relationship)
Method Overloading vs Method Overriding
Feature Method Overloading Method Overriding
Happens within the same Happens between two
Location
class. classes (Parent & Child).
Method Name Must be the SAME. Must be the SAME.
Must be DIFFERENT
Parameters Must be exactly the SAME.
(number or type).
Must be the SAME (or
Return Type Can be same or different.
covariant type).
To provide a specific
To increase readability of the
Use Case implementation of a parent's
program.
method.
The super Keyword
• The super keyword in Java is a reference variable used to refer to the
immediate parent class object.
• Primary Uses of super:
1. Access Parent Variables: Used to access parent class variables if
the child class has a variable with the exact same name (resolves
variable hiding).
2. Invoke Parent Methods: Used to invoke parent class methods if the
child class has overridden them.
3. Invoke Parent Constructors: super() is used to call the parent
class's constructor from the child class's constructor.
super - Accessing Variables and Methods
• The child class overrides the parent's method and hides the parent's
variable. We use super to reach back to the parent's version.
super - Accessing Variables and Methods
class Animal { public class MainClass
String type = "Animal"; {
void sound() { public static void main(String[] args) {
[Link]("Animal makes a sound"); Dog d = new Dog();
} [Link]();
} [Link]();
class Dog extends Animal { }
String type = "Dog"; }
@Override
void sound()
{ Output:
[Link]("Dog barks");
[Link](); // calls Animal's sound() Dog barks
} Animal makes a sound
void display() Parent type: Animal
{ Child type: Dog
[Link]("Parent type: " + [Link]); // Animal
[Link]("Child type: " + [Link]); // Dog
}
}
super() - Accessing Parent Constructors
class Person public class SuperConstructorDemo
{ {
Person() public static void main(String args[])
{ {
[Link]("Person (Parent) Constructor created"); Student s = new Student();
} }
} }
class Student extends Person Output:
{
Student() Person (Parent) Constructor created
{ Student (Child) Constructor created
super(); // Invokes immediate parent class constructor
[Link]("Student (Child) Constructor created");
}
}
The final Keyword
Definition:
• The final keyword is used to restrict the user from modifying something.
• Real-life Analogy:
• A final variable is like a fixed rule that cannot be changed.
• A final method is like a sealed process no one can modify.
• A final class is like a locked door — no one can extend it.
final Variable & Method – Program
class Vehicle public class MainClass
{ {
final int speed = 120; // constant, cannot change public static void main(String[] args)
final void fuelType() {
{ Car c = new Car();
// cannot be overridden [Link]();
[Link]("Runs on Petrol"); }
} }
}
class Car extends Vehicle Output:
{
void display() Max Speed: 120
{ Runs on Petrol
// speed = 150; ERROR! Cannot assign to final variable
[Link]("Max Speed: " + speed);
fuelType();
}
}
final Variable & Method – Program
final Class – Restricting Inheritance – Program
final class Vehicle
{
void display()
{
[Link]("This is a Vehicle");
}
} Output:
// class Car extends Vehicle { }
// ERROR! Cannot inherit from a final class This is a Vehicle
public class MainClass
{
public static void main(String[] args)
{
Vehicle v = new Vehicle();
[Link]();
}
}
Reference Links
1. Java 2 Complete Reference 5th Edition -
[Link]
[Link]
2. Java 2 Complete Reference 7th Edition
[Link]
ew?usp=sharing
Thank you