Features of OOPS

Back to home
Logicmojo - Jan 23, 2023



Object-Oriented Programming is one of the most important concepts every programmer should know. Universally recognized as one of the best ways to write clean and readable code, understanding how to use and manipulate sometimes complex relationships between objects.

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.

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.


What is OOP?

Object-Oriented Programming or OOP, in short, is a programming paradigm much like procedural programming, which introduces the concept of programming to structure your programs in a way that properties and behaviors are bundled together as one entity, termed as an object. Every object has certain properties (attributes) and behaviors (methods) attached to it. For example, a person can be an object with name, age, gender, etc as its properties and walking, talking, sleeping, etc as its various behaviors. Another such example can be a car. Its model, color, year of the build can be its properties with starting, brake, etc as its some behaviors. In another way, object-oriented programming can be defined as a way of modeling real-world objects such as cars in addition to the relation between certain classes of objects like parents and children, school and students, and so on. In short, OOP models real-world entities as software objects, which can have some attributes to describe them and can perform some actions just like in the real world.

In conclusion, objects are at the heart of the OOP and don’t just represent the data, as in procedural programming but also define the whole structure of the code


Why OOP over Procedural Programming?

Following are the three main reasons for using object-oriented programming paradigm over procedural programming paradigm:

• Procedural programming is only applicable when the project size is too small. OOP makes the development and maintenance much easier as compared to procedural programming especially as the project size grows.

• OOP provides data hiding to ensure data is available only where it’s needed whereas in procedural programming, global data is used which is accessible from everywhere in the project.

• OOP makes the code reusable as the same classes can be used for other purposes whereas procedural programming can only be used for a specific purpose.

• OOP provides the ability to simulate real-world scenarios much more effectively than procedural programming


Basic Terminologies:

Object: It’s the instance of a class/ it’s the working entity of a class.

Class: It is a template or blue print about the capability of what an object can do.

Method: The behaviours of a class. It tells what a method can do.

Instance: Object and Instance both are same with small difference.



Principles of OOP

Object-oriented programming provides the ability to find solutions to real-world problems by simulating real-world scenarios in a much better way than procedural programming. There are some core principles of object-oriented programming that it aims to achieve. The four principles of OOP are Encapsulation, Abstraction, Inheritance, and Polymorphism. We will try to explain these concepts intuitively first without any code and in a later section, we will look at them in detail with examples in code:


Encapsulation:

Encapsulation is said to be achieved when each object in our program keeps its state private, inside a class. Other objects, whether of the same class or different, don’t have direct access to this state. On the other hand, they can only invoke a list of public functions — called methods (behaviors of the class).

Basically, the object will manage its own properties (state) via methods, and no other objects are allowed to manipulate it unless explicitly allowed to. If an object wants to communicate with this object, it will use the methods that are provided but by default, one can not change the state.

Let’s try to explain this with the help of an example. We are building a problem where there are some objects of class People and one object of class Cat. They communicate with each other in order to complete the program. Suppose we want to apply encapsulation to this project. We will encapsulate (bundle) all the logic of ‘cat’ i.e, its behaviors and attributes into a Cat class. It may look like the below diagram:


Here the attributes of the cat such as mood, hungry and energy are its private attributes along with a private method, meow(). The objects of class People or any other object for that matter can’t change these attributes or tell the cat when to meow. Although, what they can do is invoke the public methods i.e, sleep(), feed() or play(). These methods do modify the internal state somehow and do invoke meow(). Thus, there is a binding between the private state of the class and the public methods. This is encapsulation.

Abstraction:

In the object-oriented programming paradigm, the programs are often extremely large in a project and the objects of different classes communicate with each other a lot. Maintaining such a large codebase, especially for years with changes occurring frequently, is difficult. Abstraction is aimed in the direction to ease this problem. Applying abstraction to an object means that the object should only expose the minimum high-level mechanism in order to use that and nothing more. This means it should hide the internal implementation details of the object and should only reveal those which are relevant to the other objects.

For instance, consider a coffee machine. It does a lot of stuff and makes different noises under the hood but you only need to put in the coffee and press the required buttons. So, this mechanism will be easy to use and will rarely change over time. You can better visualize it as a small set of public methods that any other class can invoke without ever knowing the internal implementation of those methods. Perhaps a real-world example will solidify the concept of abstraction. Consider your phone as shown below.


Inheritance:

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another.(OR) In object-oriented programming, inheritance is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

Types Of Inheritance:

Single Inheritance

Multiple Inheritance

Multi-Level Inheritance

Hierarchical Inheritance

Hybrid Inheritance


Advantages of inheritance are as follows:

• Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of inherited class.

• Reusability enhanced reliability. The base class code will be already tested and debugged.

• As the existing code is reused, it leads to less development and maintenance costs.

Disadvantages of inheritance are as follows:

• Inherited functions work slower than normal function as there is indirection.

• Improper use of inheritance may lead to wrong solutions.

• Often, data members in the base class are left unused which may lead to memory wastage.


Polymorphism:

Polymorphism is an important aspect of classes, and allows for referencing of the same method across several classes. The word ‘polymorphism’ literally means ‘the condition of occurring in several different forms’.

In short, polymorphism aims to give away to use a child class exactly like its parent so there’s no confusion with mixing types. This is achieved normally by defining a parent interface that will be reused. It outlines a set of common methods. But each of the child classes implements its own version of these methods.

Types of Polymorphism in Oops

In Object-Oriented Programming (OOPS) language, there are two types of polymorphism as below:

1. Static Binding (or Compile time) Polymorphism, e.g., Method Overloading

2. Dynamic Binding (or Runtime) Polymorphism, e.g., Method overriding


Compile Time or Static Polymorphism

With Method Overloading, static polymorphism is achieved in Object-Oriented Programming languages that allow the programmer to implement various methods. The names they use can be the same, but their parameters are different. Certain conditions are conducive for static polymorphism as below:

• Types of All Parameters should be different.

• The sequence of the Parameters can be different.

• The number of parameters of one method should differ from the other method.


Runtime or Dynamic Polymorphism

In the Dynamic Polymorphism, a call to a single overridden method is solved during a program’s runtime. Method overriding is one of the prominent examples of Runtime Polymorphism. In this process, the overriding is done through pointers and virtual functions.

• In Method Overriding, a single method is declared in a sub-class present in a parent class. The child class gains a method for implementation.

• During Runtime Polymorphism, the class offers the specification of its own to another inherited method. This transfer between methods is achieved without modifying the parent class object codes.


Method Overloading

Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading in Java, that allows a class to have more than one constructor having different argument lists.

Invalid case of method overloading

When I say argument list, I am not talking about return type of the method, for example if two methods have same name, same parameters and have different return type, then this is not a valid method overloading example. This will throw compilation error.

Method Overriding

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.

In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.

Rules for Java Method Overriding

The method must have the same name as in the parent class

The method must have the same parameter as in the parent class.

There must be an IS-A relationship (inheritance).


Conclusion

Object-oriented programming necessitates planning and thinking about the program’s structure before starting to code and examining how to decompose the requirements into basic, reusable classes that you may utilize to create object instances. Overall, using OOP provides for more reusable data structures and saves time in the long run.