How to explain Object-oriented programming(OOP) to a non-technical person?
Let’s see what is object-oriented programming, it’s concepts and how to explain OOP concepts to a non-technical person in less than 10 mins.
Object-oriented programming
To begin with, Programming — a way of designing, organizing/building, describing/modeling the executable computer program(code) to perform a specific task. Object-oriented programming — Programming is object-oriented (coding based on objects).
Object-oriented programming languages are Java, Python, JavaScript, C++, C#, Dart, Swift, Scala, Kotlin, PHP, Ruby, Perl, etc.
What is meant by objects?
Generally, the meaning of an object will differ based on the type we use.
Illustration: (Grammar)
Give me a pencil. Here, a pencil is an object.
He gave an apple to his friend. Here an apple(what) and to his friend(whom) are objects.
We know which receives an action from the subject is an object.
Eg: He ate a car.
Sentence is grammatically correct but universally not accepted. So, an object has some data that models the sentence and has an identity to make it meaningful.
In programming, the object has state, behavior, and identity. For illustration, consider variables. Variables are placeholder.
Illustration: variable_name = “car”;
Here, car is a value stored in variable_name (variable). We term it as key-value. Let’s understand the object about state, behavior and identity.
State- car has color, model, type, etc,. State tells about the characteristics/properties of the object.
Behavior- car is used for transportation. Behavior tells about the operation/predefined function of the object
Identity- car can be considered under vehicle, transportation or related to locomotive but can’t be considered as number system. Identity tells about the unique identification of the object.
But it’s creepy to set value to a value. Actually, car is a value and we are providing color, type and id to car (value) itself. So we create a template and all the values(related to car) can be added to that template.
Class
In programming, class is a template. It contains group of objects that has common properties. Now, Car becomes class(template) and all its related value are grouped as data member(variables) and member function(method).
car.name= “Suzuki”;
car.type=“Subcompact”;
car.model=“Swift”;
Here, Car is a class name and we access its data member using car as object name. So, we call Object is an instance of the class. Accessing data members and member functions of a class using (.)dot operator.
Creation of object is called instantiation. Object name is a variable but it holds the object reference of the class so it is called instance of a class. We already mentioned variable is a placeholder.
Why can’t we use class name instead of object name?
Because class is a template/blueprint or just a logical entity which contains only list of variables and methods. No memory is allocated for them. Memory allocation happens only when object of the class is created.
Class is declared once but object of that class can be created as much as possible or based on requirement. Like, Car is a class and it can have car1, car2, car3,..,carN objects. Due to this, objects are termed as physical entity.
For better clarification, Parents do name a child but why we need initials? We know it’s binding a child with parents by keeping Father/Mother’s first letter of their name + dot operator + child name. Consider this binding is actually an object reference. How?
Due to this behavior, Child is referred as instance variable. If the object “jhon” gets destroyed then we can’t get “jhon.Child” or “J.Clark”.
Physical Entity vs Logical Entity
We know, Class is a logical entity but object is both physical and logical entity. How?
Consider, Entity as a variables which holds whatever we dump.
Logical entity — Fruits, Vegetables. (blueprint)
Physical Entity — Apple, Carrot. (physical data)
Object is a logical entity when it is used with API, database and other object reference like cloning.
Mostly, object are termed as real-life entity due to distinct and independent existence. It means the class can have more than one object and each object are distinct to store, access, manipulate its references and independent of other object unless task need reference.
Features/Pillars of OOP
To learn about pillars/core of OOP, person should have some knowledge about programming.
If a programming language includes Identity, properties, behavior and attribute or capabilities of object then it is termed as object-based languages. Eg: Visual Basic.
If a programming language includes object based and polymorphism, encapsulation, inheritance, composition and abstraction then it is considered as object-oriented. Eg: R, C#, C++, Java, Python.
Encapsulation
Consider a net banking website, for an existing customer the bank has created a separate online account for them and secured it with their credentials (user_id, password). An existing customer was also categorized based on their type of account (Current/savings) and provided access security based on it (like says, savings account holder doesn’t have access to current account privileges and savings account has different withdrawal schemes and so).
So having an account in the bank won’t allow you to access another person’s account. This is like capsuling the customer based on some data (credentials, type of account and so) to make them secured.
How Encapsulation works in OOP?
Now, let’s see encapsulation in OOP. Encapsulation helps to keep the variables and methods of the class accessible within the class itself, and no other classes are allowed to access them. It sounds more technical.
Consider SBI_ Bank class and variables (User_id, password), methods (login()). Based on the credentials, the bank will check whether they are existing or not, then redirect to the home page if an existing customer or the sign-up page if a new user. So SBI_Bank (class) will take the decision to redirect while manipulating its variables.
When to use Encapsulation?
This achieves data hiding because user don’t know how the class is storing the variable since we accessing the variables passed with method.
Inheritance
We know Inheritance means derived genetically from our parents. In OOP, it derives features from the classes. Here, features means data member or member function. Broadly, there are 5 types of inheritance in OOP.
Technical terms : Super class can be termed as parent class, base class and sub class can be termed as derived class, child/children class.
- Single Inheritance- A subclass inherit the features of one superclass/parent class.
- Multiple Inheritance- one subclass can have more than one superclass and inherit features from all parent classes. Java doesn’t support this inheritance but can be achieved through Interface
- Multi-level Inheritance- A sub class is inherit from the base class and that sub class can be act as base class for another sub class.
- Hierarchical Inheritance- More than one sub class can inherit the features of one super class.
- Hybrid Inheritance- combination of two or more type of inheritance is used.
Inheritance represents IS-A relationship. Eg: Car IS-A Vehicle , Dog IS-A Animal/Pet, Surgeon IS-A Doctor. Above listed types are common but based on OOP language it might have other types of Inheritance.
When to use Inheritance?
Used when more than one class has the same logical structure and is different in data passing or object access. This isn’t preferred for code reusability, but it supports it. (You came to get this point at the end 👍). Inheritance used with Database in C#.
Read : Implement Inheritance with EF in an ASP.NET MVC 5 app
Polymorphism
Whenever we came to this word in programming, they simply expand this term as poly+morphism = many form. Eg: A man has many forms like Grandfather, Father, Son, Husband, Employee.
Types of polymorphism
- Compile-time polymorphism/Static Binding- Method/Operator Overloading.
2. Runtime polymorphism/ Dynamic Binding- Method Overriding.
Sound method in Animal class is called Overridden method and Sound method in Dog class is called Overriding method.
When to use Polymorphism?
Used when same method accessed within, derived and another class but calling is based on parameter, accessing by object. For real-time illustration, API coding mostly implemented in this type.
Abstraction
It means only the essential details are given to the user. Eg: The user should know how to operate the coffee vending machine rather than how it mechanically works.
Technically, it is a way of hiding the implementation details and showing only the functionality to the users. Encapsulation helps to hide the data into a single unit or data hiding, but abstraction hides the unwanted information or implementation hiding.
When to use Abstraction?
Used to avoid code duplication and ability to reduce complexity. Used to code focus on what it does and not on how it does. Design level implementation are done in Abstraction.
For illustration, Class is the best example for encapsulation since it wraps its members within it and abstraction will access the object to know what it can do to manipulate while calling and not concern about mapping and reference to other object.
Code Reusability in OOP
Inheritance supports code reusability. Because, Encapsulation and Abstraction hides data into it as per its definition. Polymorphism helps to do different forms of task by single action.
For the most part, Inheritance for code reusability is a bad practice. Because, Subclass inherits all the members from the super class which breaks encapsulation (subclass access all the methods in super class and able to overwrite).
Composition
Mostly preferred for code reusability.
How Composition works in OOP?
Simply, Composition means mixing of something or putting something together. Eg: Human body is a combination of skin, brain, heart, lungs and so. Heart won’t function without a human body. Like that, consider a class which contain another class as its member so we can hold the functionality of another class within the class .
Composition represent HAS-A relationship. Eg: Human HAS-A Heart, College HAS-A Student.
When to use Composition?
Used when an object of the class describes or refers one or more different objects of another class as a variable(instance variable).
Note : I just touched the OOP concepts since its beginner level article and explained in simple words.
Here explained are the core and common concepts in OOP. After learning OOP concepts in-depth, you came to the point that OOP is the rudimentary for most of the technology. I bet you!
Books to Learn OOP languages
If you wish to learn more then read, code and repeat.
- Java
Beginner level- Java: A beginner’s Guide, 8th edition
Intermediate level- Java: The complete reference, 11th edition
2. C# Beginner level : docs Microsoft tutorial
Techipedia -read, share, follow and grow :)