Friday, March 24, 2023

Top 10 Object Oriented Programming Concepts Every Programmer Should Learn

Object-oriented programming (OOP) is a programming style that is used to solve the problem by thinking in terms of real-world objects like Car, Bag, Animal, etc. It also helps you to organize code, which makes it easier to understand and solve complex programs. Objects hold information about state and behavior. States are the characteristics of the object, or the words you would use to describe it, and usually, take the form of is or has descriptors. A computer is either on or off, a chair has four legs, and you have a name. Behaviors are the things the object can do, or the actions the purpose can perform and are usually verbs that end in ing like You are sitting, using a computer, and reading this article.

To make full use of object-oriented programming, you should be familiar with the following basic OOP concepts and fundamentals:
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
  5. Class
  6. Object
  7. Coupling
  8. Cohesion
  9. Composition
  10. Delegation

The tough part of object-oriented programming (which tends to be the tough part of learning most anything about programming) was understanding the "why" aspect. Why does OOP exist; what purpose does it serve?

Thankfully, that is more easily understood when we learn and grasp the four major principles of OOP like Abstraction, Encapsulation, Inheritance, and Polymorphism.

That said, of course, these terms themselves can be somewhat confusing and seemingly abstract, and really the best way to fully understand them is to dig in and start messing around with some code. What follows, though, are some brief definitions of these terms/concepts:






10 Essential OOP Concepts Every Programmer Should Learn 

Now, let's go through each of these concepts and try to understand them better:

1. Abstraction 

This is the process of hiding the details and exposing only the essential features of a particular concept or object without including the background details or explanation. As a simple example, consider a Vending machine.

All you care about is that you enter money and get the item you want, you are not bothered about how it dispatches, how it stocked up, calculate cash, or give change, does it work like similar to a shop or other vending places.

The important thing for a Vending machine is to vend, so when you create a class called Vending Machine, you can abstract details and just present the vend() method. There are different levels of abstraction; as you move high, the level of abstraction goes up, and a number of details go down like you get the 10,000 feet view.

As you lower the Abstraction level, you start focusing on details. Abstraction is also related to generalization, which means an increased level of Abstraction.

For example, at a high level, Aeroplane, Bus, Train, Ship all are means of transportation; they carry passengers, but when you come down, you will find that Aeroplane flies, Bus travel on the road, Train runs on a railway track and Ship sail on water. If you want to learn more, you can check out Python Beyond the Basics - Object-Oriented Programming course on Udemy.


Top 10 Object Oriented Programming Concepts Every Programmer Should Learn





2. Encapsulation

This is closely related to abstraction because it also hides implementation details, but the intent is different. It hides implementation details from the outside world so that you can change it later. Encapsulation is the practice of hiding the internal workings of classes and objects from outside forces, making them accessible only through methods commonly called "getters" and "setters."

This helps prevent the accidental change of data and functions within the given class/object from external sources, which could cause severe damage in a number of locations throughout your program.

By only allowing data to be accessed via methods, those classes and objects can better manage change and have a knowledge of which external sources are able to use or alter the data contained within, based on what those available methods do.

This frees you from the worry of breaking other code in our program that is using our particular class or object, even if we make changes in another location. If you want to learn more check out Absolute Introduction to Object-Oriented Programming in Java Course by Imtiaz Ahmed on Udemy

best udemy course to learn OOP



3. Inheritance

This refers to the ability for classes or objects to inherit the interface and possibly the implementation of a parent class or object, making them "sub" and "super" classes, respectively. An easy way to think about inheritance is to think of a quick example using a generic Animal superclass.

Say we have a Plant superclass, which contains some basic information that all plants will likely share, such as a property for the type of plant, and the height of the plant. All plants will have a type, and all animals will have a height.

That said, not all Plants will be able to give a flower, so it is here that we can create a subclass of Plant, let's call it FlowerPlant, which needs the ability to give a flower.

A FlowerPlant will also still need to know its type and its height, like all Plants, so it will first INHERIT from the superclass Plant, and then it will extend its functionality to include a property or perhaps a method that defines its ability as a flowering plant. The superclass is generic, the subclasses that inherit from it can get more specific.


4. Polymorphism 

This is a Greek term that means "one name, many forms," which is precisely what the concept refers to. Like abstraction and encapsulation are closely related, so too are inheritance and polymorphism (and encapsulation).

In OOP, polymorphism allows for the same action to be performed in different ways, depending on which object is performing it. It describes a pattern in which classes have different functionality while sharing the same interface.

Let's take an example of the Animal superclass and the Bird subclass: let's create another subclass called Dog. Dogs and Birds both make sounds, but dogs bark whereas birds caw. Each subclass will have a method named makeSound(), and while they are named the same thing, they will produce different results. Dog's makeSound() method will bark, whereas Bird's makeSound() method will tweet.



5. Class

A class is a holding structure in OOP, which is used to create an object. Some people also called it a blueprint for an object. It holds attributes that make the state of the object and methods which can be called upon those objects.  A class can hold both static and non-static methods.

 A static method is something that doesn't alter the state of the object, while the non-static method can alter the state of the object. Both attributes and methods are optional in a Class, and different object-oriented programming languages like Python, C++, or Java have different rules, but they are mostly the same. For example, in Java, you can use the keyword "class" to create a class, and a new Class() will create an object.

Creating a class is easy, but designing a class is difficult because you need skills to identify objects and their relationships in your program. For example, in a game, your Player can be a class, and then all the weapons it uses can also be separate classes. A player can hold multiple weapons, and that defines the relationship between two classes.


6. Object

An object is what you create from Class. You can create multiple instances of objects from a single class. Each object has values for the attributes which define its state. For example, suppose you have a Player class which has int age as one attribute, then different players will be different objects, where they may have the same or different age.

The object is the hero of Object-Oriented programming, you can guess it right, the word itself has the Object in the first place and says, programming is object-oriented. This means your program will revolve around objects, and that would be the entity that will interact with each other and drive your program forward.

Through your application life-cycle, different objects will be created and destroyed. Over the period of your application software development, you will create new Classes and modify existing Classes to create more powerful objects, which better suit your application need.

Again, creating an object is easy, but thinking which objects will be needed by your application and their relationship is a skill and only comes by writing and designing object-oriented programs. A good way to master this skill is by developing games.



7. Coupling

Coupling is the principle of "separation of concerns." This means that one object doesn't directly change or modify the state or behavior of another object. This is a side-effect of the relationship between the object. When one object is related to another, change in one object affects other objects.

The relationship is essential. You cannot avoid that because that's what makes your program, but the key is establishing a relationship between the object which is close to each other. For example, if an object at the backend is related to the frontend, then it would be messy because, for every backend change, you need to modify the frontend.

This is called coupling, you need to reduce coupling between different layers of your application so that you can modify them separately.



8. Cohesion

Cohesion is the principle of grouping like code together and ensures each function performs only a single task. Cohesion helps to create code that is maintainable and reusable. Objects that are independent of one another and do not directly modify the state of other objects are said to be loosely coupled.

Loose coupling lets the code be more flexible, more changeable, and easier to work with. A common phrase you'll hear is "strive for low coupling and high cohesion." This phrase is a helpful reminder that we should strive for code that separates tasks and doesn't rely heavily on each other. Thus, the low (or loose) coupling is generally good, while the high (or tight) coupling is generally bad.


9. Composition

It provides an alternative way to solve a problem than Inheritance. In composition, an object is viewed as a composition of many other objects. For example, you can say that a Computer is composed


10. Delegation

It's not advisable for a class to do all the things. Instead, one class should focus on just one thing and delegate responsibility to other classes.  A good example of delegation is the equals() method in Java, which delegates the equality check to the Object class.

These can be intimidating concepts to understand, even once you become more familiar with how to write your programs to utilize them. OOP is just like learning any other facet of programming: it is going to take a long time to master. 

That said, you will be able to start picking up on it and using it fairly quickly. With time and practice, however, you will become more familiar with it and will be able to write more efficient programs through the use of these principles.




How to learn Object-Oriented Programming or OOP?

The best way to learn OOP is just to write object-oriented code. Try to re-write programs and applications you have already done and see how you could refactor it to use OOP concepts and principles, like Abstraction, Encapsulation, Inheritance, Composition, etc. As well as this, think of some real-life examples to practice things like objects and inheritance.

For example, why not create a Car class and have it inherited from a class called vehicles which contain some general vehicle methods and properties. 

You can also read some good books on Object-oriented, like., Head First Programming and Head First Java, to boost your learning. These books are a good resource for object-oriented programming with Python and Java.

Some good books and courses to learn OOP
  • Absolute Introduction to Object-Oriented Programming in Java (Udemy)
  • Elegant Objects
  • The Object-Oriented Thought Process
  • Head First Object-Oriented Analysis and Design
  • Object-Oriented Programming in Java (Coursera)
  • Byte Size Chunks: Java Object-Oriented Programming & Design 


Here is an excellent example of how you can visualize an object-oriented world:

10 Object Oriented Programming Concepts & Fundamentals Every Java Programmer Should Learn


That's all about some essential Object-Oriented Programming Concepts and Principles, every programmer should know. The biggest benefit of OOP is that it gives you a way to think and organize code. I also help to create maintainable code - code that is understandable, adaptable, and extendable.

It also helps create reusable code by following the DRY (Don't Repeat Yourself) method: write the code once and then reuse it, rather than copying and pasting. The OOP way of thinking also lends itself well to directly translating real-world objects and interactions into code.

And, if you like this article, you may like these Java and Programming articles as well:

Thanks for reading this article. If you find these object-oriented design principles useful, then please share them with your friends and colleagues. If you have any questions or feedback, then please drop a note.

P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this FREE Object Oriented Programming (OOPs) for JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course. 

2 comments:

Feel free to comment, ask questions if you have any doubt.