Four fundamental ideas of OOP you need to know

Abstraction

Abstraction means we focus on the essential qualities of something to create a class. It’s the general idea of Object-Oriented Programming (OOP). In creating a class, we will focus on the essentials and discard what’s unimportant.

For example, we write a Person class. We have the abstract idea of what a person is. We easily consider both Andy and Mike as a person to instantiate andy and mike.

It might be true that every person has a specific age. But if our application doesn’t care about that piece of information, we don’t need that attribute in our class. Discard what’s unimportant.

Encapsulation

To encapsulate is to keep the contents of an object together. And also to separate the object from other objects, and to protect those contents. The principle is that an object should not reveal anything about itself except what is necessary for other part of application to work. Encapsulation helps to reduce dependencies between different parts of the application. So that a change in one place won’t cascade down and need many changes elsewhere.

If we have this person object andy with all its attributes. we don’t want some other part of the application to be able to reach its properties and change them. We would want to provide some methods that could be used by others to change the object properties if needed. For example, to get that person’s full name we provide getFullName() method. And to change that person’s hobby, we provide setHobby() method.

Inheritance

We could create a class, but instead of writing it from scratch, we could use another class as a base class.

For example, later on you need another class called Customer. But you realize that this new Customer class is exactly the same as the Person class. The only difference is it also has a customerNumber.

You don’t want to add that customer number to your Person class, because we’re trying to use abstraction. We’re trying to only focus on the essentials, and not all your Person objects will be customers.

So to create this new class, we are going to inherit from the Person class. Customer class inherits from Person class. It means Customer class automatically has everything that the Person class has. It happens without us having to write any code. And finally we could add the customerNumber attribute to the new class. The terms used in defining the relationship are superclass and subclass. The Person is the superclass / parent class of Customer. The new Customer class is the subclass / child class of Person .

Polymorphism

Polymorphism means that a class could have many forms. So, its behavior will depend on as what class we treat it.

For example, we have Customer class that inherits from Person class. Let’s say the Person class has a getDescription() method that prints the person’s full name. Then, we override that method in the Customer class to specify its own description behavior. If it’s called, the method will print the customer’s customerNumber.

If we create a new Customer object youngCustomer and call its getDescription() method, it will print its customerNumber. But, if we treat it as a Person class object, the getDescription() method will print the customer’s full name. The youngCustomer is still an object of Customer class nonetheless.


Please like and share if you like this post.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.