CodeGym /Courses /Java Core /OOP: basic principles

OOP: basic principles

Java Core
Level 1 , Lesson 1
Available

"Hello, Amigo! Today I'm going to open a new and interesting world to your view. I'm talking about Object-Oriented Programming (OOP). You've already gotten to know classes and objects. Today you're going to learn more about them, much more."

We'll start with the four pillars of OOP. They are abstraction, encapsulation, inheritance, and polymorphism. (There used to be three, but abstraction was added later)

1) Abstraction.

A good example of abstraction in real life is job descriptions at a company. A job title is one thing, but its duties are an entirely different matter.

Imagine you're creating the org chart for your future company. You might divide up the duties of a secretary, spreading them out over several other positions. You might split the CEO's job into several separate positions: chief financial officer, chief technology officer, chief marketing officer, chief human resources officer. Or you could combine the positions of office manager and recruiter into one.

Suppose you come up with names for positions at your company, and then you "dish out" the responsibilities for these positions. That's abstraction - splitting something large and monolithic into several small parts.

OOP: basic principles - 1

From a programmer's perspective, abstraction is properly dividing a program into objects.

A large program can usually be represented as interacting objects in any of a dozen different ways. Abstraction lets you pick out an object's main characteristics and omit anything less important.

Abstraction is like a military strategy. If you choose the wrong strategy, no ingenious tactics will save the day.

2) Encapsulation.

Encapsulation is intended to improve the interaction between objects by simplifying them.

OOP: basic principles - 2

The best way to simplify something is to hide anything complicated from those who don't need to know about it. For example, if you sat down behind the pilot controls of a Boeing jet, it would take a long time to understand how they work:

OOP: basic principles - 3

On the other hand, everything looks simpler to the passengers on the plane: they buy a ticket and board the plane, which then takes off and lands. You can easily fly from continent to continent, knowing only how to «buy a ticket» and «board a plane». We do not see any of the complexity related to preparing the plane for flight, take off, landing, and various potential emergency situations. And we've made no mention of satellite navigation, autopilot, and air traffic control centers. This simplifies our life.

In terms of programming, encapsulation is «hiding the implementation». I like that definition. Our class can contain hundreds of methods and implement highly complex behavior in various situations. But we can hide all its methods from prying eyes (by marking them «private»), leaving only two or three methods for interaction with other classes (by marking them «public»). Then all the other classes in our program will only see—and call—these few methods on this class. All of the complexity of the class will be hidden inside, just as the cockpit is kept from the view of happy passengers.

3) Inheritance.

Inheritance is a concept in programming and real life. In programming, inheritance is a special relationship between two classes. But inheritance in real life is far more interesting.

If we need to create something in real life, we have two options:

1) make what we need from scratch, and spend a lot of time and effort doing it.

2) make what we need using things that already exists.

The best strategy is this: We take an existing good solution, rework and tweak it to meet our needs, and then use it.

Consider, human evolution. If we trace their beginning back to the beginning of life on the planet, we see that billions of years have passed. But if we think of humans as starting from monkeys, then only a couple million years have passed. Creating something from scratch takes longer. Much longer.

Similarly, in programming we can create one class based on another. The new class becomes a descendant (heir) of an existing class. This is super helpful when you already have a class that contains 80-90% of the needed data and methods. We simply declare a suitable class as the parent of our new class. All of the parent class's data and methods automatically become part of the new class. Convenient, right?

4) Polymorphism.

Polymorphism is programming concept that describes the situation where different implementations are hidden behind the same interface. To find an analog in real life, we can look to the process of driving a car.

If a person can drive a truck, then he or she could also be put behind the wheel of an ambulance or a sports car. A person can drive a car regardless of what kind of car it is, because they all have the same control interface: a steering wheel, pedals, and a gearshif. Cars are organized differently inside, but they all share the same control interface.

Return to programming, polymorphism lets us interact with objects of different classes (usually with a common ancestor) in the same way. The significance of this can't be overemphasized. It becomes more important as a program grows larger.

OOP is principles. Programming laws. Each of them restricts us in some way, but in return provides huge advantages as programs grow large. The four principles of OOP are like the four legs of a chair. If you take even one of them away, the entire system becomes unstable.

Comments (25)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hoist Level 17, San Diego, United States
4 January 2023
Best of luck and hard work in 2023 ! // all the user comments are worthwhile in here
Anonymous #11008702 Level 27, Lewisburg, United States
20 July 2022
Abstraction - splitting something large and monolithic into several small parts (in a hotel - front desk, hospitality, maintenance, housekeeping, restaurant) Encapsulation - intended to improve the interaction between objects by simplifying them (Ipod had one button, search engine has one field) Inheritance - making what we need using things that already exist (stand on the shoulders of giants, tap into the existing infrastructure, clone what works) Polymorphism - describes the situation where different implementations are hidden behind the same interface (AirBNB - book a stay at thousands of unique getaway locations, Uber - get a driver by pressing a button, concert venue - buy a ticket and show up, the herculean production of the show by the stage crew is none of your concern)
Mary Khan Level 2, Russia, Russian Federation
14 May 2022
Absolutely bad explanations of OOP principles.
Sinisa Level 11, Banja Luka, Bosnia and Herzegovina
31 March 2021
It is 2021. so there are different views to the OOP. Why OOP inheritance sucks by Stefan Mischook https://youtu.be/da_Rvn0au-g
Jonaskinny Level 25, Redondo Beach, United States
24 February 2022
I'd say you have to understand oop to work for most companies that have legacy systems ... those will be oop if in java/j2ee.
null Level 0
30 December 2020
briefly: - inheritance subclassing visible members of superclass - encapsulation hiding object's data - abstraction hiding the implementation details from the user - polymorphism ability to substitute an object with other certain objects during runtime
Agent Smith Level 38
22 August 2020
1. Encapsulation is a fundamental principle of object-oriented programming, which involves hiding internal state and requiring all interaction to be performed through an object's methods. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. 2. Inheritance is the process by which one object can acquire the properties of another object. It supports the concept of hierarchical classification. Using inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. Thus, it is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case. 3. Polymorphism (from Greek, meaning “many forms”) is the quality that allows one interface to access a general class of actions. The specific action is determined by the exact nature of the situation. More generally, the concept of polymorphism is often expressed by the phrase “one interface, multiple methods.” Example: method overloading. 4. Abstraction means extracting the most important and significant characteristics of an item, and conversely, casting aside those that are minor or insignificant.
Sela Level 20, Poland
11 July 2020
briefly: - inheritance subclassing visible members of superclass - encapsulation hiding object's data - abstraction hiding the implementation details from the user - polymorphism ability to substitute an object with other certain objects during runtime
Jakub Góźdź Level 31, Katowice, Poland
12 March 2020
Q: “Whats the object-oriented way to become wealthy?” A: Inheritance
Ivan Level 22, Nope, Bulgaria
25 November 2019
POLYMORPHISM - System.out.println(); - pretty good example for polymorphism. You can use it in many ways, yet you call only that function. Behind this function, there is so much code for different occasions. It can do basic operations multiple, divide, etc. It can concatenate Strings, it handles autoboxing and unboxing, and so many more (newb here). So this thing can do wonders, POLY - Many and Morph - many forms. Many forms under one function :) Someone correct me if i'm wrong.
Alexandre Lalancette Level 41, Quebec, Canada
10 November 2019
Supa clear!