IMT304: Object Oriented Programming
Lecture 4: Inheritance and Polymorphism
A.S. Bunu
Recap of Previous lecture
Review of attributes, methods, constructors, and access
modifiers.
Discussion on the assignment and common issues
encountered.
1-2
Introduction to Inheritance
Definition: inheritance refers to a process by which one class
inherits properties (attributes and methods) from another. A key
feature of inheritance in this context is the extends keyword
Advantages of using Inheritance
Code Reusability: Avoid redundant code by inheriting
functionalities from the parent class.
Enhanced Readability: Hierarchical structures give a clearer,
more intuitive view of related classes.
Improved Maintainability: Change the parent class, and the
child classes get updated accordingly
1-3
When a class inherits from another, two main roles are defined:
Superclass (or Parent Class or Base class): This
class acts as the source for inheritance. Its blueprint
forms the basis from which subsequent classes
inherit attributes or methods from.
Subclass (or Child class or Derived class): This
is the class that does the inheriting. It will naturally
incorporate all non-private properties and methods
from its superclass and can also have additional
properties and methods of its own.
1-4
Syntax (Java):
public class Animal {
String name;
int age;
public void eat() {
System.out.println("This animal is eating.");
}
}
public class Dog extends Animal {
public void bark() {
System.out.println("The dog is barking.");
}
}
1-5
Method Overloading vs. Method Overriding
Method overloading lets a class have several methods with
the same name but different parameters, allowing varied
actions based on parameters.
In contrast, method overriding enables a subclass to offer a
distinct behavior for an inherited method.
The @Override Annotation: The @Override annota
tion in Java indicates that a method is meant to override one
in its superclass. It's a safeguard, ensuring that the overriding
is intentional and correctly done, helping catch errors during
compile time.
1-6
Example of Method Overloading and Method Overriding:
class Calculator {
// Method overloading - same method name with different
parameters
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
class ScientificCalculator extends Calculator {
// Overriding the method to modify its behavior in the subclass
@Override
int add(int a, int b) {
return a + b + 10; // Just for illustration: adding 10 to
the result
}
}
1-7
Polymorphism
Definition:
Ability of a single function or method to work in different
ways based on the input or the object that calls it.
Two types:
Compile-time (method overloading) and
Runtime (method overriding).
1-8
a) Compile-time Polymorphism (Static
Polymorphism):
This type of polymorphism is achieved when we overload a
method.
void print(int a) { ... }
void print(double b) { ... }
Here, the method's name remains the same, but the parameter lists vary – this
distinction in parameters is known as method signatures.
b) Run-time Polymorphism (Dynamic Polymorphism):
This involves overriding methods from a superclass in its subclass.
class Animal {
void sound() { ... }
}
class Dog extends Animal {
void sound() { ... }
}
At runtime, Java uses the object's actual class (like Dog) to determine which version
of an overridden method should execute.
1-9
Benefits of Polymorphism
•Reusability: With Polymorphism, code components can
be leveraged across multiple classes, curtailing
redundancy.
•Extensibility: As business needs evolve, Polymorphism
ensures minimal disruptions when expanding
functionalities.
•Flexibility: Modules remain distinct, making systems
more manageable.
•Simplified Design: Systems designed with
Polymorphism are inherently organized and intuitive.
•Interchangeability: With Polymorphism, varying
implementations can be switched seamlessly.
•Enhanced Maintainability: With standardized
structures, tasks like debugging and updates become less
cumbersome.
1 - 10
Class Work
Create a Base Class and Derived Classes:
Write a class Vehicle with attributes make and model, and a
method displayInfo().
Create a derived class Car that extends Vehicle and adds an
attribute numDoors, and overrides displayInfo().
1 - 11
public class Vehicle {
String make;
String model;
public void displayInfo() {
System.out.println("Make: " + make);
System.out.println("Model: " + model);
}
}
public class Car extends Vehicle {
int numDoors;
@Override
public void displayInfo() {
super.displayInfo();
System.out.println("Number of doors: " + numDoors);
}
public static void main(String[] args) {
Car myCar = new Car();
myCar.make = "Toyota";
myCar.model = "Corolla";
myCar.numDoors = 4;
myCar.displayInfo();
}
} 1 - 12
Project02
Implement a Program Demonstrating Inheritance and
Polymorphism:
Create a class Shape with a method draw().
Create two derived classes Circle and Rectangle that extend
Shape and override draw().
Write a program that creates instances of Circle and Rectangle
and calls their draw() methods.
1 - 13