0% found this document useful (0 votes)
14 views47 pages

Oop 2

The document provides an overview of Object Oriented Programming (OOP) concepts including its pillars: Encapsulation, Inheritance, Abstraction, and Polymorphism. It discusses the benefits and challenges of OOP, various types of inheritance, and the differences between abstraction and encapsulation. Additionally, it explains runtime and compile-time polymorphism in Java.

Uploaded by

duyanjeo
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)
14 views47 pages

Oop 2

The document provides an overview of Object Oriented Programming (OOP) concepts including its pillars: Encapsulation, Inheritance, Abstraction, and Polymorphism. It discusses the benefits and challenges of OOP, various types of inheritance, and the differences between abstraction and encapsulation. Additionally, it explains runtime and compile-time polymorphism in Java.

Uploaded by

duyanjeo
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/ 47

COMPROG 2 - OOP

Part 2
Outline

▰ OOP Pillars

2
Introduction

Object Oriented programming is a programming style


which is associated with the concepts like class,
object, Inheritance, Encapsulation, Abstraction,
Polymorphism. Most popular programming languages
like Java, C++, C#, Ruby, etc. follow an
object-oriented programming paradigm.

3
Introduction

4
5
Benefits of OOP

1. Modularity for easier troubleshooting


2. Reuse of code through inheritance
3. Flexibility through polymorphism
4. Effective problem solving

6
Challenges of OOP

▰ Steep learning curve


▰ Larger program size
▰ Slower program execution
▰ It's not a one-size fits all solution

7
Encapsulation

Encapsulation is a mechanism where you bind your data and


code together as a single unit. It also means to hide your data
in order to make it safe from any modification.

8
Encapsulation

9
Encapsulation

Encapsulation is achieved by declaring the variables as


private and providing public setter and getter methods to
modify and view the variable values. In encapsulation, the
fields of a class are made read-only or write-only. This
method also improves the re-usability. Encapsulated code
is also easy to test for unit testing.
10
Encapsulation

11
Inheritance

In OOP, computer programs are


designed in such a way where
everything is an object that interact
with one another. Inheritance is one
such concept where the properties
of one class can be inherited by the
other. It helps to reuse the code and
establish a relationship between
different classes.
12
Inheritance

13
Inheritance

14
Inheritance - 5 types

▰ Single
▰ Multi level
▰ Hierarchical
▰ Multiple
▰ Hybrid

15
Inheritance - 5 Types

1. Single Inheritance
In single inheritance, one class
inherits the properties of another. It
enables a derived class to inherit
the properties and behavior from a
single parent class. This will in turn
enable code reusability as well as
add new features to the existing
code.
16
Inheritance - 5 Types

1. Single Inheritance
In single inheritance, one class
inherits the properties of another.
It enables a derived class to inherit
the properties and behavior from a
single parent class. This will in turn
enable code reusability as well as
add new features to the existing
code.
17
Inheritance - 5 Types

1. Single Inheritance
In single inheritance, one class
inherits the properties of another.
It enables a derived class to inherit
the properties and behavior from a
single parent class. This will in turn
enable code reusability as well as
add new features to the existing
code.
18
Inheritance - 5 Types

2. Multi Level Inheritance


When a class is derived from a
class which is also derived from
another class, i.e. a class having
more than one parent class but at
different levels, such type of
inheritance is called Multilevel
Inheritance.
19
Inheritance - 5 Types

2. Multi Level Inheritance


When a class is derived from a
class which is also derived from
another class, i.e. a class having
more than one parent class but at
different levels, such type of
inheritance is called Multilevel
Inheritance.
20
Inheritance - 5 Types

2. Multi Level Inheritance

21
Inheritance - 5 Types

3. Hierarchical Inheritance
When a class has more than one
child classes (sub classes) or in
other words, more than one child
classes have the same parent
class, then such kind of
inheritance is known as
hierarchical.
22
Inheritance - 5 Types

3. Hierarchical Inheritance
When a class has more than one
child classes (sub classes) or in
other words, more than one child
classes have the same parent
class, then such kind of
inheritance is known as
hierarchical.
23
Inheritance - 5 Types

3. Hierarchical Inheritance
When a class has more than one
child classes (sub classes) or in
other words, more than one child
classes have the same parent
class, then such kind of
inheritance is known as
hierarchical.
24
Inheritance - 5 Types

Other than these types of inheritance in Java, there are


other types known as multiple inheritances and hybrid
inheritance. Both types are not supported through classes
and can be achieved only through the use of interfaces.

25
Inheritance - 5 Types

4. Multiple Inheritance
Multiple inheritances is a type of
inheritance where a subclass can
inherit features from more than
one parent class.

26
Inheritance - 5 Types

5. Hybrid Inheritance
Hybrid inheritance is a
combination of multiple inheritance
and multilevel inheritance. Since
multiple inheritance is not
supported in Java as it leads to
ambiguity, so this type of
inheritance can only be achieved
through the use of the interfaces.
27
OOP: Abstraction

▰ Abstraction refers to the quality of


dealing with ideas rather than events. It
basically deals with hiding the details
and showing the essential things to the
user.

28
OOP: Abstraction

29
OOP: Abstraction

Two ways to achieve abstraction:


1. Abstract Class
2. Interface

30
OOP: Abstraction

Abstract class: Abstract class in Java contains the ‘abstract’


keyword. Now what does the abstract keyword mean? If a class
is declared abstract, it cannot be instantiated, which means you
cannot create an object of an abstract class. Also, an abstract
class can contain abstract as well as concrete methods.
Note: You can achieve 0-100% abstraction using abstract class.

31
OOP: Abstraction

32
OOP: Abstraction

Interface: Interface in Java is a blueprint of a class or you can


say it is a collection of abstract methods and static constants.
In an interface, each method is public and abstract but it does
not contain any constructor. Along with abstraction, interface
also helps to achieve multiple inheritance in Java.
Note: You can achieve 100% abstraction using interfaces.

33
OOP: Abstraction

34
OOP: Abstraction

35
OOP: Abstraction

What is the difference between


Abstraction and Encapsulation?

36
OOP: Abstraction

37
OOP: Abstraction

1. Abstraction focuses on elements that are necessary to build a system


whereas, the encapsulation focuses on hiding the complexity of the
system.
2. The abstraction is performed during the design level of a system. On
the other hand, encapsulation is performed the system is being
implemented.
3. Abstractions main motive is, what is to be done to build a system.
Encapsulations main motive is, how it should be done to build a system.
4. Abstraction is achieved by encapsulation whereas, the encapsulation is
achieved by making the elements of the system private.
38
OOP: Abstraction

Abstraction and encapsulation both are the


essential features of OOP. A good encapsulation
can achieve a good abstraction.

39
OOP: Polymorphism

Polymorphism means taking many forms, where ‘poly’ means


many and ‘morph’ means forms. It is the ability of a variable,
function or object to take on multiple forms. In other words,
polymorphism allows you define one interface or method and
have multiple implementations.

40
OOP: Polymorphism

41
OOP: Polymorphism

42
OOP: Polymorphism

Polymorphism in Java is of two types:


1. Run time polymorphism (Dynamic polymorphism)
2. Compile time polymorphism (Static polymorphism)

43
OOP: Polymorphism

Run time polymorphism: In Java, runtime polymorphism


refers to a process in which a call to an overridden method
is resolved at runtime rather than at compile-time. In this, a
reference variable is used to call an overridden method of a
superclass at run time. Method overriding is an example of
run time polymorphism.

44
OOP: Polymorphism

45
OOP: Polymorphism

Compile time polymorphism: In Java, compile time polymorphism refers to a


process in which a call to an overloaded method is resolved at compile time
rather than at run time. Method overloading is an example of compile time
polymorphism. Method Overloading is a feature that allows a class to have
two or more methods having the same name but the arguments passed to the
methods are different. Unlike method overriding, arguments can differ in:
1. Number of parameters passed to a method
2. Datatype of parameters
3. Sequence of datatypes when passed to a method.
46
OOP: Polymorphism

47

You might also like