Module 2 Lesson 1-5
Module 2 Lesson 1-5
OOP
PARADIGMS
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.
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.
Here, the fields and methods can be accessed from other classes
as well.
• 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,
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.
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.
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.
PRESENTATION TITLE
• In the above example, the eat() method is present in both the
superclass Animal and the subclass 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.
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.
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.
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.
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
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.
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
27/02/2023 44
MODULE 2: OOP PARADIGMS
Operator Overloading: +
(Mathematical addition)
27/02/2023 45
MODULE 2: OOP PARADIGMS
Operator Overloading: +
(Concatenation)
27/02/2023 46
MODULE 2: OOP PARADIGMS
Polymorphic Variables
27/02/2023 47
48
PRESENTATION TITLE
JAVA ABSTRACT CLASS
AND
ABSTRACT METHODS
MODULE 2: OOP PARADIGMS
27/02/2023 51
MODULE 2: OOP PARADIGMS
27/02/2023 52
MODULE 2: OOP PARADIGMS
27/02/2023 53
MODULE 2: OOP PARADIGMS
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
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
27/02/2023 68
MODULE 2: OOP PARADIGMS
27/02/2023 69
MODULE 2: OOP PARADIGMS
27/02/2023 70
MODULE 2: OOP PARADIGMS
27/02/2023 71
MODULE 2: OOP PARADIGMS
27/02/2023 72
MODULE 2: OOP PARADIG
MS
27/02/2023
THANK YOU
73