Object oriented programming

I read through so many articles and used many examples from these articles which I felt may simplify the explanation of complex terms in this post.

You might have heard of OOPS and classes many times. To keep it simple, Object oriented programming is the basic way of thinking or comparing programming with real life objects. ie; thinking programming in the real life way. We have been using this from late 1970’s. Let’s see how they actually work.

A class is like a blue print of something and we can create multiple copies of it.

Let’s understand it a little more in a practical way. Consider a car factory. We create a blue print of a particular model and we create multiple copies of it. So, here the blue print is the class called Car and we create or assemble the cars based on the blue print provided. These newly created car is the object created based on the blue print.

Vechicle class

Let’s consider another example. Let’s say you want to use a person in your program. You want to be able to describe the person and have the person do something. A class called ‘person’ would provide a blueprint for what a person looks like and what a person can do. To actually use a person in your program, you need to create an object. You use the person class to create an object of the type ‘person.’ Now you can describe this person and have it do something.

Class contains variables for storing data and functions to performing operations on these data.Class will not occupy any memory space. Hence to work with the data represented by the class you must create a variable for the class, which is called as an object.

Classes are very useful in programming. Consider the example of where you don’t want to use just one person but 100 people. Rather than describing each one in detail from scratch, you can use the same person class to create 100 objects of the type ‘person.’ You still have to give each one a name and other properties, but the basic structure of what a person looks like is the same.

Classes are a fundamental part of object-oriented programming. They allow variables and methods to be isolated to specific objects instead of being accessible by all parts of the program. This encapsulation of data protects each class from changes in other parts of the program. By using classes, developers can create structured programs with source code that can be easily modified.

The class is one of the defining ideas of object-oriented programming. Among the important ideas about classes are:

A class can have subclasses that can inherit all or some of the characteristics of the class. In relation to each subclass, the class becomes the superclass.

Subclasses can also define their own methods and variables that are not part of their superclass.

The structure of a class and its subclasses is called the class hierarchy.

IMPORTANT: Objects are pass by reference. NOT BY VALUE. Many developers makes mistakes on passing objects to functions and other classes.

Objects are pass by reference.

An object is having a reference in memory. We cannot copy an object. Value types like Int, Double, Float etc are pass by value.

Principles of OOPs :

All the programming languages supporting object oriented Programming will be supporting these three main concepts:

Inheritance :

When a class acquire the property of another class is known as inheritance. Inheritance is process of object reusability. Through inheritance, an object acquires some/all properties of another object.

For example: Car is a classification of Four Wheeler. Here Car acquires the properties of a four-wheeler. Other classifications could be a jeep, tempo, van etc. Four Wheeler defines a class of vehicles that have four wheels, and specific range of engine power, load carrying capacity etc. Car (termed as a sub-class) acquires these properties from Four Wheeler (termed as a super-class), and has some specific properties, which are different from other classifications of Four Wheeler, such as luxury, comfort, shape, size, usage etc.


In the above code, the two subclasses Truck and SportsCar inherited all the methods and properties from its super class Vehicle and modified it as per the requirement. The acceleration for Truck and Sports are different. SO the method accelerate is modified accordingly.

Encapsulation :

Wrapping up data member and method together into a single unit (i.e. Class) is called Encapsulation. Encapsulation means hiding the internal details of an object, i.e. how an object does something. Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public.

Looking at the example of a power steering mechanism of a car. Power steering of a car is a complex system, which internally have lots of components tightly coupled together, they work synchronously to turn the car in the desired direction. It even controls the power delivered by the engine to the steering wheel. But to the external world there is only one interface is available and rest of the complexity is hidden. Moreover, the steering unit in itself is complete and independent. It does not affect the functioning of any other mechanism.

Similarly, same concept of encapsulation can be applied to code. Encapsulated code should have following characteristics:

Polymorphism :

Polymorphism means to process objects differently based on their data type. Polymorphism is where the method to be invoked is determined at runtime based on the type of the object. This is a situation that results when you have one class inheriting from another and overriding a particular method. However, in a normal inheritance tree, you don’t have to override any methods and therefore not all method calls have to be polymorphic.

Lets us look at same example of a car. A car have a gear transmission system. It has four front gears and one backward gear. When the engine is accelerated then depending upon which gear is engaged different amount power and movement is delivered to the car.

Polymorphism could be static and dynamic both. Overloading is static polymorphism while, overriding is dynamic polymorphism.

If in an interview if someone ask you to explain polymorphism, better explain it with some example.

“Imagine there is a class called Shape. You have to calculate the area of the shape.But you don’t know which shape will be passed on to calculate the area on run time. It can be a triangle, a square or a rectangle. All these shapes have a common property called area but the way this is calculated is different. So, we create a calculate area function and pass in to it two values. Lets see them in code: ”


The calculateArea method in the above code will perform its operation differently based on the shape on runtime. So the superclass implementation of this method is used in a different form in its subclasses. This is polymorphism. ie; the ability to exist in more than one form.

Data abstraction:

Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.

Data abstraction is a programming (and design) technique that relies on the separation of interface and implementation.

Let’s take one real life example of a TV, which you can turn on and off, change the channel, adjust the volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know its internal details, that is, you do not know how it receives signals over the air or through a cable, how it translates them, and finally displays them on the screen.

Thus, we can say a television clearly separates its internal implementation from its external interface and you can play with its interfaces like the power button, channel changer, and volume control without having zero knowledge of its internals.

In OOPs , classes provides great level of data abstraction. They provide sufficient public methods to the outside world to play with the functionality of the object and to manipulate object data, i.e., state without actually knowing how class has been implemented internally.

For example, your program can make a call to the sort() function without knowing what algorithm the function actually uses to sort the given values. In fact, the underlying implementation of the sorting functionality could change between releases of the library, and as long as the interface stays the same, your function call will still work.

If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!

Written by

iOS and tvOS developer, dreamer, photographer 🤨

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store