0% found this document useful (0 votes)
31 views41 pages

Encapsulation, Inheritance and Polymorphism

Uploaded by

Doyin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views41 pages

Encapsulation, Inheritance and Polymorphism

Uploaded by

Doyin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

COMPUTER PROGRAMMING 1

Encapsulation,
Inheritance and
Polymorphism
COMPUTER PROGRAMMING 1

Learning objectives

At the end of this lesson, you should be able to:

● Discuss the concepts of abstraction and encapsulation and their


importance in object-oriented programming

● Illustrate the concept of inheritance and its benefits in


code organisation and reuse.

● Describe and apply the principles of polymorphism to


write flexible and extensible code.

● Justify the role of interfaces in Java and how


they facilitate multiple inheritance.
COMPUTER PROGRAMMING 1

Abstraction and Encapsulation

Abstraction and encapsulation are fundamental concepts in


Object-Oriented Programming (OOP).
They facilitate code organisation, data protection, and modelling of
real-world entities.
COMPUTER PROGRAMMING 1

Abstraction

Abstraction involves simplifying complex reality by modelling classes that


represent essential aributes and behaviours.
It focuses on what an object does rather than how it does it.
Abstraction allows developers to hide unnecessary details and focus on
high-level functionalities.
COMPUTER PROGRAMMING 1

Encapsulation

Encapsulation is the concept of bundling data (aributes) and methods


(behaviours) that operate on the data into a single unit (class).
It promotes data protection by controlling access to the class’s internal
state.
Encapsulation ensures that the internal representation of an object is
hidden from the outside.
COMPUTER PROGRAMMING 1

Advantages of Encapsulation

Data Protection: Aributes can be made private, preventing direct


access and unauthorized modifications.
Code Organization: Methods related to data are encapsulated within the
class, enhancing code readability and maintainability.
Abstraction: The class interface abstracts away the internal
implementation details, simplifying usage for other parts of the code.
COMPUTER PROGRAMMING 1

Accessors (Getters) and


Mutators (Setters)

Accessors (geers) provide controlled access to private aributes.


Mutators (seers) allow controlled modification of private aributes.
COMPUTER PROGRAMMING 1

Example: encapsulation
with Getters and Setters
COMPUTER PROGRAMMING 1

Example: encapsulation
with Getters and Setters
COMPUTER PROGRAMMING 1

Real-World Analogy: ATM


Machine

Think of a bank account class as an ATM machine.


Users can interact with the ATM using a limited set of actions (methods).
The ATM machine encapsulates the internal mechanisms
(implementation details) from the users.
COMPUTER PROGRAMMING 1

Mid-lesson question

You are tasked with designing a simplified online


shopping system in Java.
1. Create a class Product to represent products
available for purchase on the online platform.
Each product has the following aributes:
productId (int), productName (String), price
(double) and quantityInStock (int)
The class should have the following methods:
Constructor, getProductId(),
getProductName(), getPrice(),
getQuantityInStock(), updateQuantity(int
newQuantity) and displayProductInfo()
COMPUTER PROGRAMMING 1

Mid-lesson question

2. Create multiple instances of the


Product class to represent dierent
products available in the online store.
3. Demonstrate the encapsulation
principle by accessing the aributes
through methods and abstracting
the inner details of the Product class.
COMPUTER PROGRAMMING 1

Answer: Mid-lesson question


COMPUTER PROGRAMMING 1

Answer: Mid-lesson question


COMPUTER PROGRAMMING 1

Answer: Mid-lesson question


COMPUTER PROGRAMMING 1

Answer: Mid-lesson question


COMPUTER PROGRAMMING 1

Inheritance

It allows a class to inherit aributes and methods from another class, promoting
code reuse and organization.
The class that provides the aributes 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 aributes and methods.
COMPUTER PROGRAMMING 1

Inheritance

Use the extends keyword to create a subclass.


Example
COMPUTER PROGRAMMING 1

Inheritance

Example: Using Inheritance for Shape Hierarchy


COMPUTER PROGRAMMING 1

Inheritance

Example: Using Inheritance for Shape Hierarchy


COMPUTER PROGRAMMING 1

Benefits of Inheritance

Code Reuse: Subclasses can reuse aributes 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

Polymorphism is the ability of objects of dierent classes to be treated as


objects of a common superclass.
It promotes flexibility and extensibility in code design.
COMPUTER PROGRAMMING 1

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

An interface in Java defines a contract for a set of methods that classes


must implement.
Interfaces allow multiple inheritance of behaviours, promoting code
flexibility and modularity.
COMPUTER PROGRAMMING 1

Declaring Interfaces

Use the interface keyword to declare an interface.


Interface methods are declared without implementations.
Example
COMPUTER PROGRAMMING 1

Implementing Interfaces

A class implements an interface by providing implementations for its methods.


Use the implements keyword in the class declaration.
Example
COMPUTER PROGRAMMING 1

Implementing Interfaces
COMPUTER PROGRAMMING 1

Using Interfaces for


Multiple Inheritance

Java supports multiple interface implementation, allowing classes to


inherit multiple sets of behaviours.
Example
COMPUTER PROGRAMMING 1

Using Interfaces for


Multiple Inheritance
COMPUTER PROGRAMMING 1

Advantages of Interfaces

Multiple Inheritance: Classes can implement multiple interfaces to


inherit behaviors from various sources.
Contractual Obligation: Interfaces define a contract that implementing
classes must fulfill, promoting consistency.
Flexibility: Interfaces enable dynamic method binding and polymorphism.
COMPUTER PROGRAMMING 1

Example: Using Interfaces


for Shape Calculations
COMPUTER PROGRAMMING 1

Example: Using Interfaces


for Shape Calculations
COMPUTER PROGRAMMING 1

Example: Using Interfaces


for Shape Calculations
COMPUTER PROGRAMMING 1

Summary

• Abstraction focuses on what an object does.


• Encapsulation bundles data and methods into a
single unit, promoting data protection and code
organization.
• Understanding abstraction and encapsulation is
crucial for designing maintainable and secure
object-oriented programs.
• Inheritance allows subclasses to inherit aributes
and methods from super classes.
• Polymorphism enables objects of dierent classes
to be treated as objects of a common superclass.
COMPUTER PROGRAMMING 1

Summary

• Interfaces define contracts for classes to


implement a set of methods.
• Implementing classes provide method
implementations for the interface’s contract.
• Interfaces support multiple inheritance,
enabling classes to inherit behaviours from
various sources.
COMPUTER PROGRAMMING 1

FURTHER READING RESOURCES

● Poo, D. C. C., Kiong, D., Ashok, S. (2008). Object-Ori


ented Programming and Java. Singapore: Springer.
● Java inheritance FAQ. (n.d.). (n.p.): One Percent
Beer.
● Java Tutorial. (n.d.). www.w3schools.com. Retrieved
August 12, 2023, hps://www.w3schools.com/java
● tutorialspoint.com. (2019). Java Tutorial.
www.tutorialspoint.com. Retrieved August 12, 2023,
hps://www.tutorialspoint.com/java/index.htm
● Burd, B. (2017). Beginning programming with Java
for Dummies. John Wiley & Sons.
COMPUTER PROGRAMMING 1

FURTHER READING RESOURCES

● Eckel, B. (2003). Thinking in JAVA. Prentice Hall


Professional.
● Ţigănoaia, B. (2019). Computer Programming:
Practical Applications. Romania: Editura
Niculescu.
COMPUTER PROGRAMMING 1

Thank
You

You might also like