Principles of OOP Introduction To Object-Oriented Programming ITF Courseware ELearn
Principles of OOP Introduction To Object-Oriented Programming ITF Courseware ELearn
IT Fundamentals
Home Course Basics of Programming Introduction to Object-Oriented Programming
Principles of OOP
COURSE DISCUSSION
Principles of OOP
Let us define each principle and give examples for better understanding.
Abstraction
Abstraction is a way of representing task elements from the real world as objects in a
program. Abstraction is always associated with the generalization of some information
about the properties of objects, so the main thing is to separate meaningful
information from insignificant in the context of the problem being solved. Moreover,
there can be several levels of abstraction. Abstraction and encapsulation are closely
related. Returning to the building example, we only need to know the number of floors
1 of 5 10/31/22, 10:53 PM
Principles of OOP | Introduction to Object-Oriented P... https://elearn.epam.com/courses/course-v1:RD_CIS+...
and the address. This is an abstraction. We are shown only the simplest interface, and
irrelevant information for this interface is hidden. If you recall the history, then you will
see that at first people lived in caves, and then they built small settlements (the
material was di�erent, the number of floors appeared over time, there was no address
for a long time). Now the variety of buildings and their design is amazing.
Let's try to apply the principle of abstraction to the building example. First, let us
highlight the most common classification of real estate. For example, it can be
represented in the form of the diagram shown in the figure below.
Now, with the help of abstraction, we can select general information in this hierarchy
of objects: the general abstract type of objects - Building, general characteristics - the
year of its construction, the material of the walls, the number of floors. This is how it
looks in Java:
2 of 5 10/31/22, 10:53 PM
Principles of OOP | Introduction to Object-Oriented P... https://elearn.epam.com/courses/course-v1:RD_CIS+...
from unnecessary details) in relation to a real subject allows you to make it formal, so
that during development, you can clearly identify all the dependencies and operations
on them in the created so�ware system. This simplifies the study (analysis) and
development of models, as well as their implementation on a computer.
Encapsulation
Encapsulation means restricting access to data and the ability to change it.
Encapsulation allows an object to define an interface with which to interact, hiding
implementation details, and thus, leaving the freedom to change the internal
structure. Think of some building. Most likely you thought of something that resembles
the picture below on the le�, not on the right.
We keep all the inside (rough) trim of a building and communications for the interior
design. For example, we protect the electrical wiring from ordinary users (not
electricians) to prevent the possibility of a short circuit. The same can be applied to
the code, we do not want someone outside to interfere with the organization of the
code or important data, and we hide data members and some methods inside the
class, o�en using access modifiers; an open interface is provided to work with the
object. In the topic "Classes and Objects in OOP", we examined the Java program
example that illustrated what access modifiers there are and how the access to private
data is organized. The task of a programmer is to determine which attributes and
methods will be available for public access, and which are the internal implementation
of the object and should be inaccessible for changes.
3 of 5 10/31/22, 10:53 PM
Principles of OOP | Introduction to Object-Oriented P... https://elearn.epam.com/courses/course-v1:RD_CIS+...
Inheritance
Inheritance allows you to create new classes based on the original class by adding
special data fields and additional methods to the existing classes. At the same time,
the properties and functionality of the parent class are borrowed by the new class and
can be reused. The original class is called a superclass or a parent, new classes are
called its subclasses or descendants. Let us take another look at the building diagram
above. You can see that it is a hierarchy in which the building located below has all the
features located up in the branch, plus its own features. For example, a cottage refers
to residential buildings and buildings in general. In this case, we can talk about the
inheritance of object properties. For example, in Java, creating a new subclass looks as
follows:
When creating a new subclass, you need to add quite a bit of new code, and we get a
new class with new functionality. Using the principle of OOP, inheritance allows you to
significantly reduce the amount of code, and therefore, to facilitate the work of a
programmer.
Polymorphism
Polymorphism is an OOP concept, which means that objects that belong to the same
branch of the hierarchy and receive the same method can act in di�erent ways.
4 of 5 10/31/22, 10:53 PM
Principles of OOP | Introduction to Object-Oriented P... https://elearn.epam.com/courses/course-v1:RD_CIS+...
Polymorphism is about the fact that it is possible to use descendant classes in the
context that was intended for a parent class, that is, there is a guarantee of preserving
the interface (the properties and functionality) of the parent class. Polymorphism
allows us to completely abstract in the client code from what kind of implementation
class we are using by referring to it through the use of the interface defined in the
parent class. This reduces the dependency of code modules. In this case, the execution
of each specific action will be determined by a subclass, if necessary. O�en, a method
inherited by a subclass is directly used by it. However, it is sometimes necessary for the
operations in a subclass to be overridden according to the semantic variations of the
subclass. For example, in the previous Java block of code, we overrode the
int getFloors_number(); method, which in the base class returns the number of
floors, and in the subclass, it also adds the number of parking floors.
You will study in more detail the implementation of each OOP concept for a specific
programming
Navigationlanguage during an in-depth study of the selected programming language.
5 of 5 10/31/22, 10:53 PM