Most programming languages are object oriented today which have OOP concept like abstraction, polymorphism, encapsulation and inheritance, But there are other ways to program. Procedural programming language like C. In procedural programming, programs are written as a long series of operations to execute. Some of that might be written in methods to make the program more modular. But the common goal is to execute the instructions from start to end. The example I can think of is a recipe book. You need to follow the instructions to make any recipe. The recipe to make tea, would include the steps you need to follow. Put the jar on the stove, add water, tea leaves, sugar and boil it. Just do those steps in order and you will get tea. I usually see programmers love to code in procedural patterns because it’s easy right?
So look at the above recipe in object oriented style. You could split into several self contained objects and each object will contain it’s own data and logic to describe how it behaves and interacts with other objects. I can put water, tea leaves and sugar into a jar and use a microwave oven to boil it. Result is the same, we will get the tea. The difference is how we approach making tea. So I have defined the functionality to boil things on the stove or in the oven. Now If I need to make coffee I can put coffee powder and water into the jar to make coffee. What I need is just another Jar, which is an object.
Object Oriented approach similar to real world. You want to know what’s an object in a computer program?, ask yourself, what’s an object in the real world?
So the tea is an object, the cup is an object. But two separate mugs are not the same object. Every object has inherent properties/attributes that describe its current state. For example a cup can be full, empty or partially full. Most objects will have multiple attributes. Those attributes can be the size and color of the cup and they can remain constant. If we think about more complex objects like aircraft, there is nothing about complexity in terms of objects. Any object can contain other objects. Each object in a program is self contained. Three things that describe the object
- Identity – which differentiate the object from other objects.
- Attributes – which defines its current state.
- Behaviour – which defines what the object can do.
Sometimes it’s difficult to know which should be defined as an object in your application. For this just ask yourself if it’s a noun. Noun is not just a tangible thing, but also the idea, the event. In a simple way, if you can put the word “the” in front of it, it’s an object. You can not say The driving, The chatting. Those are verbs that describe behaviour of an object.
For creating objects, we need a class. A class is the detailed definition of an object. But it’s not the object itself. Once we define a class, we can create as many objects based on that class. There are three components that make a class.
- Name – like Cookies
- Attributes – like weight, size and color
- Behaviour – like consume() and decorate()
Let’s think about the car which is a real world example of the object. It has identity, every car is different from another car. It has attributes like tyres, steering and dashboard. It has behaviour, you can drive the car.
There are four fundamental concepts in object oriented programming. Abstraction, Polymorphism, Inheritance and Encapsulation
Abstraction is something we use in our daily life. Let me give you an example, If I say, ‘’person” you know what I mean. I didn’t specify who I was thinking of, not even male or female. But you know what person means. Abstraction means we focus on essential qualities rather than one specific quality. You can refer to a person by gender, height etc. but not with taste and smell. Abstraction means the idea or concept of a person is completely separate from any specific instance. It’s what we do all of the time in conversation. That’s what we do when we make a class. We focus on essential qualities like name, gender and height.
Encapsulation means protecting the content of the object. We bundle an object’s attributes or data along with the methods that operate on that data within the same unit or the same class. It will restrict the access of some or all of the object’s components. You need to call a specific method which is assigned to interact with internal methods to access protected components. In the future if I want to change something in the protected components, I don’t need to write code again and again. I will just update the code in components and it will work.
When we create a new claas, It may not always be necessary to build it from scratch. Inheritance enables a new class to inherit the attributes and methods of existing classes using the same implementation. For example a person class can be inherited by customer class, employee class. In this case person class can be super class and customer class can be sub class. Sometimes it is described as Parent and child class or base and derived class.
Final object oriented concept is polymorphism. Polymorphism means having many forms. There are multiple forms of polymorphism. One form, called dynamic or run-time polymorphism, allows us to access methods using the same interface on different types of objects that may implement those methods in different ways. Think of making a team using a stove or microwave oven. We input water, tea, sugar and get the tea. The process was different but the input and output was the same. The other form is called static or compile-time polymorphism. it uses a feature of many object-oriented programming languages called method overloading. Overriding and overloading both are different. Overloading allows you to implement multiple methods within a class that have the same name, but a different set of input parameters. Overriding enables a child class to provide different implementations for a method that is already defined and/or implemented in its parent class or one of its parent classes.
Those are the basic object oriented concepts which will work in any language.