Encapsulation, Inheritance and Polymorphism
Encapsulation, Inheritance and Polymorphism
Encapsulation,
Inheritance and
Polymorphism
COMPUTER PROGRAMMING 1
Learning objectives
Abstraction
Encapsulation
Advantages of Encapsulation
Example: encapsulation
with Getters and Setters
COMPUTER PROGRAMMING 1
Example: encapsulation
with Getters and Setters
COMPUTER PROGRAMMING 1
Mid-lesson question
Mid-lesson question
Inheritance
It allows a class to inherit aributes and methods from another class, promoting
code reuse and organization.
The class that provides the aributes and methods is called the superclass (or
parent class).
The class that inherits from the superclass is called the subclass (or child class).
The subclass extends the superclass, inheriting its aributes and methods.
COMPUTER PROGRAMMING 1
Inheritance
Inheritance
Inheritance
Benefits of Inheritance
Code Reuse: Subclasses can reuse aributes and methods from super
classes, reducing redundancy.
Extensibility: New functionality can be added to subclasses without
modifying the superclass.
Modularity: Inheritance promotes organized and modular code structure.
COMPUTER PROGRAMMING 1
Polymorphism
Method Overriding
Subclasses can provide their own implementation for a method inherited from a
superclass.
Method overriding allows customization of behaviour in subclasses.
Example
COMPUTER PROGRAMMING 1
Dynamic Method
During runtime, the appropriate method implementation is called based on the actual object’s type.
This allows for polymorphic behaviour based on the object’s dynamic type.
Example
COMPUTER PROGRAMMING 1
Example: Polymorphism in
Shape Hierarchy
COMPUTER PROGRAMMING 1
Example: Polymorphism in
Shape Hierarchy
COMPUTER PROGRAMMING 1
Interfaces
Declaring Interfaces
Implementing Interfaces
Implementing Interfaces
COMPUTER PROGRAMMING 1
Advantages of Interfaces
Summary
Summary
Thank
You