Basic Principles of OOPS (Object-Oriented Programming)

Principles of OOPS

OOP – Object-Oriented Programming Principle is the strategy or style of developing applications based on objects.
Anything in the world can be defined as an object. And in the OOPs, it can be defined in terms of its properties and behavior. 

For Example – Consider a Television, It is an object. And the properties related to it are. It’s  With and Height, color, type (SmartTV or CRT TV), etc. And the behavior can be – we can change the channel, Adjust volumes and settings, switch off, switch on, etc are lots of behavior are there. The operation on the behavior will affect the properties.  Changing the channel will affect its property. 

So, similar things happen in Object-Oriented programming also, that each object has some properties and behavior associated with it, that are defined in the class

In short, it is a programming methodology that has certain principles and with the help of these principles, the software is developed.

There are many other methodologies used for software development, But the most popular is Object-Oriented programming.

Object-Oriented Programming Principles is the most popular amongst others because it  relates to real-life objects. Every operation that is going to be functional is  considered in terms of classes and objects. That provides a better programming style because you need not write code that needs to run anytime. Instead, you can create classes defining the functionality and call that function by creating an object of that. 

Other than these, it also provides principles like inheritance, that increase the code reusability and help in applying updates very easily.  And Abstraction, Encapsulation helps in enhancing the security of the data.

Languages that support Object-Oriented Programming are – C++, Java, JavaScript, C#, PHP, etc. 
And Languages that are completely Object-Oriented Programming or Pure Object-Oriented are – python, Ruby, Scala, etc.

How Software is Developed in Object-Oriented Programming?

We say that in OOPs, there is an engineered approach is followed for the development of software. Like first designing a blueprint that contains the functionality of the object, operations of an object, etc. And then based on the blueprint, the object is created that implements the blueprint into existence. It is just like the other engineering things which are followed. For example – The construction of any building includes steps like – Planning, Designing a blueprint,  Finalizing a blueprint, after a blueprint is finalized then start construction of it. 

The same is followed in Object-oriented Programming using the concept of classes and objects, which are the pillars of it.

In OOPs, the work can be distributed between developers easily, and also the work can be reused between programmers.

Components of Object-Oriented Programming

Object-oriented Programming has mainly 4 components – 

  1. Objects – Object is the entity that makes the classes to be implemented into the program. It makes the features, properties, and behaviors of the class to be implemented. Example – A car is an object that has property color, model, brand name, fuel type, etc, and behaviors like, it runs. So these properties and behavior make an object (CAR).
  2. Classes – A class can be stated as the blueprint of the object. This states what an object can do. It contains all the features, attributes, and behavior of what the model can do. We can say that class contains the definition of the object. Example – Car color, engine type, etc. And with the definition, we can create any number of  objects.
  3. Methods – Methods are the attributes of the class which are defined for the specified behavior of the object. It can also modify the state of the class. Example – Method to drive a car, It changes the state of the car from parking state to running state.
  4. Instances – It is the members of the class who holds some values related to the objects of the class.

To understand better about these components better, let’s consider an example – Consider the same example of television.

So according to the Object-Oriented Programming approach, you will perform certain steps-

  • Define object name as a Television.
  • Define the properties of television like channel, volume, on and off, etc.
  • Create a function that will handle the functionality of tree-like changing the channel adjusting volume, or other things.
  • Now implement all features into a program.

Now when you define an object as Television then it is the class. And then you declare the properties of that Television -like channel, volume, etc is the instances of the class Television. You create functions to change the properties, which are the methods of Television. And finally when you implement this in your program then you will create an object of class Televisionto implement into the program.

Programmatically it should be like –

class Television{
	int channel; //Instance
	int volume; //Instance
      boolean on = false;
	.
	.
	void changeChannel(){} //Methods
	void adjustVolume(){} //Methods
      void switchOnOff(){} //Methods
      .
      .
}

Television tv = new Television(); //Television Object

Explanation – In the above snippet of code. Defined Television is a class that contains the instances that are the property of the class. And methods that help to change the behavior of the television class.
And we are creating one  object of television called tv, which will have all the properties and behavior of the class.

Why does OOP come into Existence?

Object-Oriented Programming was introduced by Alan Kay during 1960 – 1967.  At that time it was not so popular. Because at that time, computers were not meant for large-scale software. The software was made only for performing some specific tasks. But nowadays the software is becoming more popular and the size of the software has increased. So the procedural programming or functional programming approach will be very cumbersome to manage  Because the codebase will be very large. And writing this much code and in case any error occurs then debugging will be very inefficient. 

So the Object-Oriented Programming solves this problem by using its principle. In which the tasks are defined in the class according to their functionality. And each class can be easily managed and debugged in case of any error.

Example –  If we are developing the software of a Bank. The bank has multiple Departments and operations associated with it. 

Like – Loan Department, Finance Department, Insurance Department, etc are there. Now managing these all with functional programming will be very inefficient as each department also have different functions associated with it and in case an anty error occurs then it becomes very hard to detect that and the whole software will be affected.

Then in OOPs, we can manage this all with the help of classes and objects. Like we can divide Loan Department into a class, Insurance Department into another class, and so on. Now consider that if any error occurs in the loan department then the other classes will not be affected.

Principles of Object-Oriented Programming

Object-Oriented Principles mainly include the 4 pillars that together make the OOP a very powerful concept. That is – 

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

OOP is based on real-life engineered things. Like the approaches followed in the other engineering – Electronic Engineers are making some device, Automobile engineers are making some vehicle, etc. So the design approaches followed their, the same replicated to the software engineering with the concept of these 4 Object-Oriented Programming principles.

If we analyze the programming concept then we will find that there are two elements of programming – 

  1. Data
  2. Operations on Data [functions]

So If we develop software then that software is meant for performing tasks on data only. And if any function is performed on data, then there might be chances that in the future also the same operation can be performed either in the same way or in some modified way. So that is very smoothly done using the concept of OOP principles. Let’s see the principles:

Abstraction

Abstraction can be defined as hiding internal implementation and showing only the required features or set of services that are offered. This is the most essential part of Object-Oriented programming.

Let’s understand with the help of an example – 

Example 1 – We all have been using ATMs. And if we want to withdraw the money from that then we simply interact with the Use Interface Screen where we have been asking for details. 

As a user, we only want that I can use that ATM for the services. We are not interested in knowing the internal implementation that what is the process happening inside the ATM Machine like getting card details, validation, etc kind of things. We only have been provided an interface to interact with that and simply ask for the work. And the internal implementation we don’t need to know. So that is the Abstraction.

Explanation-

The above image shows the GUI screen of the ATM. In this when we want to withdraw then we simply use that. For the implementation part, we don’t care.

The same concept is applicable in the programming implementation of the Abstraction. Although different programming has a different syntax of implementation of Abstraction. But the most popular programming languages like JAVA  use a keyword called abstract which applies to the class and methods to make a class abstract and achieve abstraction. And it can also be achieved using the interfaces in java.

C++ achieves it by grouping the attributes and using an access specifier allowing what data is to be visible outside. 

Access Specifiers are the keywords that state, from where the (attributes and methods) of a class can be accessed.  For example – private can only be accessible within the class, the public can be accessed from anywhere, etc. 

Similarly, other programming languages follow their own implementation for achieving abstraction.

class Television{
    void changeChannel(){
        //Implementation
    }
    void adjustVolume(){
        //Implementation
    }
    void otherFeatues(){
        //Implementation
    }
}

This pseudo-code example states that the television class is providing features like channel change, adjust volume, etc. so as a user we only need to use that functions. We need not know the internal implementation like how the function is working for changing channels, how the codes manage to adjust the volume, etc. We have been provided the method to do that task, So just utilize that.If we consider an example more specific to programming then we can say that the functions included in the Math library like – pow(), min(), max(), etc are there, and we are only using that to get our result. It’s not the case where we check its implementation. So it is also an abstraction.

Features of Abstraction – 

  1. Security- With Abstraction, the Outside persons don’t know the internal implementation so it makes the classes more secure, so the unauthorized user will not have access to the data.
  2. Easy Enhancement- Suppose in the future we want to change the logic of the implementation in class, So we don’t have to change the entire external logic which is there for the user. Just we need to make changes for the methods and it will not affect the functionality.
  3. Improves Easiness- It helps to use the features without knowing implementation so it improves easiness for the users to use that.
  4. Maintainability will improve-  Without affecting the user, it can able to perform any types of changes internally. SO maintainability will improve.

Encapsulation

Encapsulation can be defined as the binding of data and attributes or methods and data members in a single unit. In classes, we have Data and attributes that perform operations on that data. So  according to the OOPs principle of Encapsulation, that data can be merged into a single unit. Encapsulation enhances more security of the data as everything related to a single task must be grouped and access to the data is given as per need.

And this can be achieved using the concept of Data Hiding.

Encapsulation  =  Data Hiding + Abstraction.

Data Hiding – It means hiding the data of the class and restricting access to the outside world. Example – Using the access specifier keywords like private that restricts the data to only accessible and modifiable in the same class. Outside users can not access the data.

Let’s understand with the help of an example-

Example 1- In the market, Capsules are available to cure different medical problems. Consider capsule for fever. Now in that capsules, there are different compositions are grouped to make a complete medicine that cures fever. So the grouping of that compositions into a single unit in the capsule is a form of encapsulation. Here, consider fever as data, and the capsule as the operations on the data. So everything is grouped here. 

Explanation-

In the above image, all the dots represent the compositions for curing the fever (as per the example). And these are grouped into a capsule. Similarly in programming, when there are multiple operations related to a particular data are present, then grouping that into a simple unit is Encapsulation.


Example 2- In a car, we have all the required things to make a car complete. Like engines, gear shift, Wheels, etc, and these are grouped into a single body that makes a car. It is not like the wheels are not attached to the car, Engines are misplaced and dragged outside, etc. Everything is grouped into a single body. So in the OOP Principle also the same principle applies that whatever operations are there for a particular group, must be capsuled to a single unit.

Explanation-

In the image we can see that the car has an engine, steering to control, gear shift, paddles to race and stop the car are present, and these are grouped into a complete car. 
So we want the same things in programming also, And that’s what OOP Principle makes it possible. Similarly, if we take an example more specific to programming then we can state an example of a LinkedList. We create a node that contains data and this node is related to the LinkedList class. And Operation on that node like – Add, Delete, Search, etc are grouped into a single class.

class LinkedList{
    private class Node{
        data;
        next pointer;
    }
    public createNode(){}
    public addNode(){}
    public deleteNode(){}
    .
    .
}

This pseudo-code stated the encapsulation feature that every operation related to the LinkedList is grouped as private and the operation to be public that is accessible to everyone.

Implementation of Encapsulation is language-specific. Like java uses the setter and getter Method, C++ uses accessors and mutators, etc.

If any components follow Data Hiding and Abstraction, such type of component is said to be encapsulated component.

Features of Encapsulation – 

  1. It provides extra security by the concept of Data Hiding. 
  2. It groups the data and operation in that data into a single unit.
  3. It attaches an extra security layer to the data and allows to access data only to authorized ones.

Inheritance

Inheritance is the method of acquiring features of the existing class into the new class. Suppose there is a class, considered as the parent class, that has some methods associated with it. And we declare a new class, considered as the child class, that has its own methods. So, when a child class is inherited from the parent class then it will have all the methods associated with parent class are available in the child class along with the child class own methods also. So that is Inheritance.


Example 1 – We all have seen the updates which receive on gadgets both in terms of hardware and software. Like firstly mobile phones are their only to talk and then used for media access, Internet, Camera, etc. So these are evolution that arrives by adding some extra feature and making a new product without rebuilding the complete functionality so it is an example of inheritance. Similarly, in software, regular updates are with some new features are also the inheritance.

Explanation-

In the above image, we see that from old mobile new mobile is derived by adding new features without affecting the old functionality, so it is an inheritance.

Note- This is just an example of what inheritance is. In OOPs it has some differences.

Now, In terms of Object-Oriented Programming Inheritance can be stated as –  

There are two keywords involved in the inheritance – Base and Derived Class.

Base Class – It is also termed the parent class. It is the main class that has the basic properties and methods which is defined.

Derived Class – This is the extension of the base class and it is also called the child class. It has the properties and methods that are in the base class with its own features in it.

Inheritance has multiple types that depend on programming languages implementation. Some of the common types of inheritance are- 

  1. Single Inheritance – When there is only one derived class.
  2. Multiple Inheritance – When child class is inheriting more than one base class.
  3. Multilevel Inheritance – When there is a level of inheritance. From class A to class B to class C.
  4. Hierarchical Inheritance – When more than one derived class is inheriting one base class.

Programming Implementation of Inheritance-

class Base{
    data;
    feature1(){}
    feature2(){}
}
class Derived extends Base{
    feature3(){}
}

In the above snippet of code there is a base class that has some data and features. And now the derived class is implementing the base class which has an additional feature. So overall derived class have all the feature which are in the base class with its own feature. So this is inheritance. 

Derived class = feature1 (from base class), 
feature2 (from base class), 
data (from base class), and 
feature3 (it’s own).

Note- Different programming languages used their different keyword for inheritance. Like java uses extends keyword, c++ uses : (colon to inherit), Python uses () Parenthesis, etc.

The biggest advantage of inheritance is code reusability. The methods which are already declared in the base class need not be rewritten in the derived class, They can be directly used. It is automatically available for use on the derived class.

Polymorphism

Polymorphism is the most essential concept of the Object-Oriented Programming principle. It has meaning ‘poly’ – many, ‘morph’ – forms.  So polymorphism means many forms. 

In Object-Oriented Programming, any object or method has more than one name associated with it. That is nothing but polymorphism.

For Objects – When any object can hold the reference of its parent object then it is a polymorphism. To understand this concept more clear, let’s consider an example- 

Example – We have Television from different companies, Let’s say Samsung TV, LG TV, Xiaomi TV, etc. Now if we have to call it then mostly we don’t call it with its brand name. We simply call it a Television, And it can be of a different brand. But in general, we call it television. So this can be said to be polymorphism.

Polymorphism Example

Explanation-

In the above image we have given these different brands of Television a common name which we call Television. Here the term Polymorphism is followed.

It can also be stated as the Generalization because we have given a general name that can apply to all the subclasses it has been derived from.
And yes, It can be achieved using inheritance. And the derived class object can also be called with the name of the base class.

Example- 

class Television{
    public void TVOn(){}
    public void TVOff(){}
}
class SamsungTV extends Television{
    public void surfInternet(){}
}


Television TV = new SamsungTV(); //Yes, It is absolutely correct.

Explanation – In the above snippet the SamsungTV implemented Television class and method TVOn() and TVOff() is available in SamsungTV class.

Now the object we have created of reference of Television class and created the object of the SamsungTV class. Now, if we call TV.TVOn() then this works perfectly well. So it is the way to achieve Polymorphism.

Because here it is polymorphism. And we can say that the SamsungTV is a Television. Because the Television, as it has all the methods related to that. If we call Television.on() then the SamsungTV will on as we have objected to that.

For Methods – Methods can also have multiple names associated with them. That we also consider as the types of polymorphism.

OOPs states 2 types of Polymorphism – 

  1. Compile Time Polymorphism. Also called Static binding.
  2. Runtime Polymorphism. Also called Dynamic binding.

Compile Time Polymorphism is achieved using  Method Overloading.

  • Method Overloading – When more than one method is declared with the same name but with a different number of parameters or different data-type of parameter, that is called method overloading. 
  • Example –
class Base{
    public void method1(int x){ }
}
class Derived extends base{
    //Overloaded Method
    public void method1(float y){ }
}
  • In the above snippet their is method1 declared twice but both have different signature. One is Accepting integer value as a parameter and the other is accepting the float value as a parameter.
  • It is called Static Binding because, during the compilation time itself, it’s been clear which method to call. It is recognized with the help of Data Type and during the compilation time of the program.

Similarly, Runtime polymorphism is achieved using Method Overriding.

  • Method Overriding – When more than one method is declared as the same name and same signature but in a different class. Then the derived class method will override the base class method. This means the Base class method will be shadowed by the derived class method. And when the method is called then it can be called based on the overridden method.
  • Example – 
class Base{
    public void method1(int x){ } // Shadowed Method
}
class Derived extends base
    public void method1(int y){ }//Overriden Method
}
  • In the above snippet, method1 is overridden in derived class. And when the object of the derived class will be created and method1 will be called then the Derived class method1 will be called. The Base class  method1 will be shadowed.
  • It is called Dynamic binding because, when the object gets the memory during runtime of the program and based on the object the overridden method will be called. So it happens during the execution of the program.

Conclusion

Object-Oriented Programming Principle is the most widely used programming paradigm that helps in developing large-scale applications in the real world using the modules that allows multiple developers to work together with such that, It can make the complete system. Object-Oriented Programming Principles also enhance the security features with the help of powerful concepts like Abstraction & Encapsulation.

Most of the popular programming languages are completely based on the OOPs Principles like Python, Ruby, etc. 

Previous Post

Spiral Model (Software Engineering)

Next Post
Salesforce Developer Salary

Salesforce Developer Salary

Total
0
Share