0% found this document useful (0 votes)
4 views

Module 2 Lesson 1-5

Module 2 focuses on Object-Oriented Programming (OOP) paradigms in Java, covering key concepts such as encapsulation, inheritance, polymorphism, and abstraction. It explains how encapsulation helps in data hiding, inheritance allows for code reusability, and polymorphism enables methods to take different forms. The module also details the use of abstract classes and methods to achieve abstraction in software design.

Uploaded by

Patricia Garcia
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Module 2 Lesson 1-5

Module 2 focuses on Object-Oriented Programming (OOP) paradigms in Java, covering key concepts such as encapsulation, inheritance, polymorphism, and abstraction. It explains how encapsulation helps in data hiding, inheritance allows for code reusability, and polymorphism enables methods to take different forms. The module also details the use of abstract classes and methods to achieve abstraction in software design.

Uploaded by

Patricia Garcia
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 73

MODULE 2:

OOP
PARADIGMS

Prepared by: Kres-Ann Oclarit


27/02/2023 2

PRESENTATION TITLE

OBJECTIVES
:
• Create and call overloaded methods
• Classify the suitability of overridden
methods
• Apply the principles of inheritance,
polymorphism, abstract classes and
inheritance using java classes
• Respond keenly to the details of a real-
world object
JAVA
ENCAPSULATION
MODULE 2: OOP PARADIGMS

Java Encapsulation
Encapsulation is one of the key features of
object-oriented programming. Encapsulation
refers to the bundling of fields and methods
inside a single class.

It prevents outer classes from accessing and


changing fields and methods of a class. This
also helps to achieve data hiding.

27/02/2023 4
PRESENTATION TITLE
Example 1: Java
Encapsulation

5
In the above example, we have created a class named Area. The
main purpose of this class is to calculate the area.

To calculate an area, we need two variables: length and breadth


and a method: getArea(). Hence, we bundled these fields and
methods inside a single class.

Here, the fields and methods can be accessed from other classes
as well.

Hence, this is not data hiding.

This is only encapsulation. We are just keeping similar codes


together.
6
7
• In Java, encapsulation helps us to keep related fields and methods
together, which makes our code cleaner and easy to read.

• It helps to control the values of our data fields. For example,

• Here, we are making the age variable private and applying logic inside
the setAge() method. Now, age cannot be negative.
8
• The getter and setter methods provide read-only or write-only
access to our class fields. For example,

• It helps to decouple components of a system. For example, we can


encapsulate code into multiple bundles.
• These decoupled components (bundle) can be developed, tested, and
debugged independently and concurrently. And, any changes in a
particular component do not have any effect on other components.
• We can also achieve data hiding using encapsulation. In the above
example, if we change the length and breadth variable into private, then
the access to these fields is restricted.
• And, they are kept hidden from outer classes. This is called data hiding.
9
MODULE 2: OOP PARADIGMS

Data Hiding

In the above example, we have a private field


age. Since it is private, it cannot be accessed
from outside the class.
In order to access age, we have used public
methods: getAge() and setAge().
These methods are called getter and setter
methods.
Making age private allowed us to restrict
unauthorized access from outside the class. This
is data hiding.

27/02/2023 10
JAVA INHERITANCE
MODULE 2: OOP PARADIGMS

Java Inheritance
Inheritance is one of the key features of OOP
that allows us to create a new class from an
existing class.

The new class that is created is known as


subclass (child or derived class) and the
existing class from where the child class is
derived is known as superclass (parent or base
class).

27/02/2023 12
The extends keyword is used In the above example, the Dog

PRESENTATION TITLE
to perform inheritance in Java. class is created by inheriting
For example, the methods and fields from
the Animal class.

Here, Dog is the subclass and


Animal is the superclass.

13
14

PRESENTATION TITLE
• In the above example, we have derived a subclass Dog from
superclass Animal. Notice the statements,

• Here, labrador is an object of Dog. However, name and eat() are the
members of the Animal class.

• Since Dog inherits the field and method from Animal, we are able to
access the field and method using the object of the Dog.

15
16
MODULE 2: OOP PARADIGMS

is-a relationship
In Java, inheritance is an is-a relationship. That
is, we use inheritance only if there exists an is-a
relationship between two classes. For example,
• Car is a Vehicle
• Orange is a Fruit
• Surgeon is a Doctor
• Dog is an Animal
Here, Car can inherit from Vehicle, Orange can
inherit from Fruit, and so on.

27/02/2023 17
MODULE 2: OOP PARADIGMS

Method Overriding
in Java Inheritance
In Example 1, we see the object of the subclass
can access the method of the superclass.

However, if the same method is present in


both the superclass and subclass, what
will happen?

In this case, the method in the subclass


overrides the method in the superclass. This
concept is known as method overriding in Java.
27/02/2023 18
19

PRESENTATION TITLE
• In the above example, the eat() method is present in both the
superclass Animal and the subclass Dog.

• Here, we have created an object labrador of Dog.

• Now when we call eat() using the object labrador, the method inside
Dog is called. This is because the method inside the derived class
overrides the method inside the base class.

• This is called method overriding.

20
MODULE 2: OOP PARADIGMS

super Keyword in
Java Inheritance
Previously we saw that the same method in the
subclass overrides the method in superclass.

In such a situation, the super keyword is used to


call the method of the parent class from the
method of the child class.

27/02/2023 21
22

PRESENTATION TITLE
MODULE 2: OOP PARADIGMS

Why use
inheritance?
The most important use of inheritance in Java is
code reusability. The code that is present in the
parent class can be directly used by the child
class.

Method overriding is also known as runtime


polymorphism. Hence, we can achieve
Polymorphism in Java with the help of
inheritance.

27/02/2023 23
MODULE 2: OOP PARADIGMS

Types of Inheritance
There are five types of inheritance.

1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance

27/02/2023 24
MODULE 2: OOP PARADIGMS

Single Inheritance
In single inheritance, a single subclass extends from a single superclass. For example,

27/02/2023 25
MODULE 2: OOP PARADIGMS

Multilevel
Inheritance
In multilevel inheritance, a subclass
extends from a superclass and then the
same subclass acts as a superclass for
another class. For example,

27/02/2023 26
MODULE 2: OOP PARADIGMS

Hierarchical
Inheritance
In hierarchical inheritance, multiple subclasses extend from a single
superclass. For example,

27/02/2023 27
MODULE 2: OOP PARADIGMS

Multiple Inheritance
In multiple inheritance, a single subclass extends from multiple superclasses.
For example,

27/02/2023 28
MODULE 2: OOP PARADIGMS

Multiple Inheritance
Java does not support multiple
inheritance of classes, but it does
support multiple inheritance of
interfaces.

27/02/2023 29
MODULE 2: OOP PARADIGMS

Hybrid Inheritance
Hybrid inheritance is a combination of
hierarchical and multiple inheritance,
where a class inherits from multiple
classes, some of which are derived
from a common base class. Java does
not support hybrid inheritance of
classes, but it does support hybrid
inheritance of interfaces. For
example,

27/02/2023 30
MODULE 2: OOP PARADIGMS

Extends vs.
Implements
Extends:
• extends is used to create a subclass (child class) that inherits properties
and behaviors (methods) from a superclass (parent class).
• The subclass can access all the public and protected members (methods and
variables) of its superclass.
• Java supports single inheritance, meaning a subclass can only extend one
superclass, though it can be part of an inheritance hierarchy where each
class extends another, forming a chain of inheritance.

27/02/2023 31
MODULE 2: OOP PARADIGMS

Extends vs.
Implements
Implements:
• implements is used to declare that a class implements a particular
interface.
• An interface in Java is a contract that specifies a set of methods that a class
implementing that interface must provide.
• When a class implements an interface, it agrees to provide concrete
implementations for all the methods declared in the interface.
• A class can implement multiple interfaces.

27/02/2023 32
MODULE 2: OOP PARADIGMS

Output:

27/02/2023 33
JAVA
POLYMORPHISM
MODULE 2: OOP PARADIGMS

Java Polymorphism
Polymorphism is an important concept of object-
oriented programming. It simply means more
than one form.

“Poly” means MANY and “Morph” means “Take


Different Forms”

That is, the same entity (method or operator or


object) can perform different operations in
different scenarios.

27/02/2023 35
36

PRESENTATION TITLE
MODULE 2: OOP PARADIGMS

Why Polymorphism?
Polymorphism allows us to create consistent
code. In the previous example, we can also
create different methods: renderSquare() and
renderCircle() to render Square and Circle,
respectively.
This will work perfectly. However, for every
shape, we need to create different methods. It
will make our code inconsistent.

27/02/2023 37
MODULE 2: OOP PARADIGMS

To solve this, polymorphism in Java allows us to create a single method


render() that will behave differently for different shapes.

We can achieve polymorphism in Java using the following ways:


1. Method Overriding
2. Method Overloading
3. Operator Overloading

27/02/2023 38
MODULE 2: OOP PARADIGMS

Method Overriding
During inheritance in Java, if the same method is present in both
the superclass and the subclass. Then, the method in the
subclass overrides the same method in the superclass.

This is called method overriding. In this case, the same method


will perform one operation in the superclass and another
operation in the subclass. For example,

27/02/2023 39
40

PRESENTATION TITLE
41

PRESENTATION TITLE
MODULE 2: OOP PARADIGMS

Method Overloading
In a Java class, we can create methods with the same name if they differ in
parameters. For example,

27/02/2023 42
43

PRESENTATION TITLE
MODULE 2: OOP PARADIGMS

Java Operator Overloading


Some operators in Java behave differently with different operands. For
example,
• + operator is overloaded to perform numeric addition as well as string
concatenation, and
• operators like &, |, and ! are overloaded for logical and bitwise operations.

• The + operator is used to add two entities. However, in Java, the +


operator performs two operations (Mathematical addition and
Concatenation).

27/02/2023 44
MODULE 2: OOP PARADIGMS

Operator Overloading: +
(Mathematical addition)

1. When + is used with numbers (integers and floating-point numbers), it


performs mathematical addition. For example,

27/02/2023 45
MODULE 2: OOP PARADIGMS

Operator Overloading: +
(Concatenation)

2. When we use the + operator with strings, it will perform string


concatenation (join two strings). For example,

27/02/2023 46
MODULE 2: OOP PARADIGMS

Polymorphic Variables

A variable is called polymorphic if it refers to different values


under different conditions.

Object variables (instance variables) represent the behavior of


polymorphic variables in Java. It is because object variables of a
class can refer to objects of its class as well as objects of its
subclasses

27/02/2023 47
48

PRESENTATION TITLE
JAVA ABSTRACT CLASS
AND
ABSTRACT METHODS
MODULE 2: OOP PARADIGMS

Java Abstract Class


Abstraction in Java refers to the concept of hiding complex
implementation details and exposing only the essential
features of a software component to the user. Abstraction is
achieved in Java through the use of abstract classes and
interfaces.

An abstract class is a class that cannot be instantiated and


can only be subclassed. It is used to provide a common
interface for a group of related classes. An abstract class
may contain both abstract and non-abstract methods. An
abstract method is a method without a body, and it is
declared using the "abstract" keyword. The implementation
of abstract methods is left to the subclasses that extend the
abstract class.
27/02/2023 50
MODULE 2: OOP PARADIGMS

27/02/2023 51
MODULE 2: OOP PARADIGMS

27/02/2023 52
MODULE 2: OOP PARADIGMS

• A method that doesn't have its body is known as an abstract method. We


use the same abstract keyword to create abstract methods. For example,

• Here, display() is an abstract method. The body of display() is replaced


by ";".

• If a class contains an abstract method, then the class should be declared


abstract. Otherwise, it will generate an error.

27/02/2023 53
MODULE 2: OOP PARADIGMS

Example: Java Abstract Class and Method


Though abstract classes cannot be instantiated, we can create
subclasses from it. We can then access members of the abstract
class using the object of the subclass. For example,

27/02/2023 54
MODULE 2: OOP PARADIGMS

27/02/2023 55
MODULE 2: OOP PARADIGMS

Implementing Abstract
Methods
If the abstract class includes any abstract method, then all the
child classes inherited from the abstract superclass must provide
the implementation of the abstract method. For example,

27/02/2023 56
MODULE 2: OOP PARADIGMS

Implementing Abstract
Methods
• In the example, we have created an abstract class Animal. The
class contains an abstract method makeSound() and a non-
abstract method eat().
• We have inherited a subclass Dog from the superclass Animal.
Here, the subclass Dog provides the implementation for the
abstract method makeSound().
• We then used the object d1 of the Dog class to call methods
makeSound() and eat().

27/02/2023 57
MODULE 2: OOP PARADIGMS

Implementing Abstract
Methods

27/02/2023 58
MODULE 2: OOP PARADIGMS
Accesses Constructor of Abstract
Classes
• An abstract class can have constructors like the regular class. And, we can
access the constructor of an abstract class from the subclass using the super
keyword. For example,

27/02/2023 59
MODULE 2: OOP PARADIGMS
Accesses Constructor of Abstract
Classes
• Here, we have used the super() inside the constructor of Dog to
access the constructor of the Animal.
• Note that the super should always be the first statement of the
subclass constructor.
• The major use of abstract classes and methods is to achieve abstraction in
Java.
• Abstraction is an important concept of object-oriented programming that
allows us to hide unnecessary details and only show the needed information.
• This allows us to manage complexity by omitting or hiding details with a
simpler, higher-level idea.

27/02/2023 60
MODULE 2: OOP PARADIGMS

Key Points to Remember


• We use the abstract keyword to create abstract classes and methods.
• An abstract method doesn't have any implementation (method body).
• A class containing abstract methods should also be abstract.
• We cannot create objects of an abstract class.
• To implement features of an abstract class, we inherit subclasses from it
and create objects of the subclass.
• A subclass must override all abstract methods of an abstract class.
However, if the subclass is declared abstract, it's not mandatory to override
abstract methods.
• We can access the static attributes and methods of an abstract class using
the reference of the abstract class.
27/02/2023 61
JAVA INTERFACE
MODULE 2: OOP PARADIGMS

Java Interface
An interface is a fully abstract class. It includes a group
of abstract methods (methods without a body). We use
the interface keyword to create an interface in Java. For
example,

27/02/2023 63
MODULE 2: OOP PARADIGMS

Implementing an
Interface
• Like abstract classes, we cannot create objects of
interfaces.
• To use an interface, other classes must implement it.
We use the implements keyword to implement an
interface.

Example…

27/02/2023 64
MODULE 2: OOP PARADIGMS

Implementing Multiple
Interfaces
• In Java, a class can also implement multiple interfaces. For
example,

27/02/2023 65
MODULE 2: OOP PARADIGMS

Extending an Interface
• Similar to classes, interfaces can extend other interfaces. The
extends keyword is used for extending interfaces. For
example,

27/02/2023 66
MODULE 2: OOP PARADIGMS

Advantages of Interface in
Java
Now that we know what interfaces are, let's learn about why
interfaces are used in Java.
• Similar to abstract classes, interfaces help us to achieve
abstraction in Java.
• Interfaces provide specifications that a class (which
implements it) must follow.
• Interfaces are also used to achieve multiple inheritance in
Java.

27/02/2023 67
MODULE 2: OOP PARADIGMS

"default" methods in Java


Interfaces
• With the release of Java 8, we can now add methods with
implementation inside an interface. These methods are called
default methods.
• To declare default methods inside interfaces, we use the
default keyword. For example,

27/02/2023 68
MODULE 2: OOP PARADIGMS

Why default methods?


• Let's take a scenario to understand why default methods are
introduced in Java.
• Suppose, we need to add a new method in an interface.
• We can add the method in our interface easily without
implementation. However, that's not the end of the story. All
our classes that implement that interface must provide an
implementation for the method.

27/02/2023 69
MODULE 2: OOP PARADIGMS

Why default methods?


• If a large number of classes were implementing this interface,
we need to track all these classes and make changes to them.
This is not only tedious but error-prone as well.

• To resolve this, Java introduced default methods. Default


methods are inherited like ordinary methods.

27/02/2023 70
MODULE 2: OOP PARADIGMS

"private" and "static" Methods in Interface


• The Java 8 also added another feature to include static
methods inside an interface.
• Similar to a class, we can access static methods of an
interface using its references. For example,

27/02/2023 71
MODULE 2: OOP PARADIGMS

"private" and "static" Methods in Interface

27/02/2023 72
MODULE 2: OOP PARADIG
MS
27/02/2023

THANK YOU
73

You might also like