OOPS Concepts | MyCodingNetwork | Alok Tripathi | Preview Version

oops image


Object Oriented Programming System (OOPS)

OOPS stands for Object-Oriented Programming System. It is a programming architecture that organizes data and behavior into reusable units called objects. Objects have properties (attributes) and methods (functions) that define their state and behavior. Objects can also interact with each other through messages.

OOPS is based on four main principles: abstraction, encapsulation, inheritance, and polymorphism. We will explore all these four principles in detail, but we need to first familiarize ourselves with the important terminologies in object-oriented programming system.

Let's explore OOPS terminologies:

Objects: Object is a real world-entity with some properties and behaviors. Examples- Book, Chair, Pen, Smartphone etc. 

Class-Group of similar objects is known as a class. Example-A class of 3rd standard shall be having all the students who have passed class 2nd and all students are of similar age group, thus, the students have some similarity in them that's they belong to the same class.

Abstraction-It means hiding the unnecessary details and showing only the essential features of an object. For example, in a bike we're not concerned about the internal working of the bike. All we focus is how to start, apply breaks, accelerator and other common things.

Encapsulation-Wrapping up of data member and member methods in a single unit is known as encapsulation. Precisely, encapsulation means bundling the data and methods of an object together and protecting them from outside interference.

Inheritance-It means creating new classes (subclasses) from existing classes (super classes) and inheriting their properties and methods. Basically, it means, the process of deriving the properties and behavior of parent object to child object.

Polymorphism-It means having different forms or behaviors of the same object depending on the context or input.



Now we'll be exploring each of them one by one.

Abstraction

Abstraction is a concept that allows us to hide the implementation details of a class or an interface and only expose the essential features to the users. Abstraction can be achieved by using abstract classes and interfaces in Java.


For understanding abstraction, we first need to familiarize ourselves with interface and abstract class along with abstract methods.

An ABSTRACT CLASS is a class that is declared with abstract keyword but can have abstract and non-abstract methods. An abstract class cannot be instantiated. Although, it can have constructors.

An abstract method is a method which is declared with the keyword abstract, but it does not have a body. It must be overridden by a subclass. In the subclass all the abstract methods of the super class must be overridden.

An INTERFACE is a blueprint of a class that is declared with interface keyword can only have abstract methods. It cannot have constructors.

The first question that can come to your mind is, how to implement abstraction? So, we have an answer to that. Carefully observe the source code given below, where you will get a clear picture about interface and abstract:

Abstract class & abstract function:
Code for abstract
Code 1.1

In code 1.1, first of all we are having an abstract class Phone inside that abstract class we are having a member function call( ) with void data type inside that there is a print statement. It is followed by an abstract function which is not having a body as it is abstract.

The class Phone is followed by class Redmi which extends Phone class (Concept of inheritance, discussed below), inside the class Redmi method overriding (Concept of polymorphism, discussed below) of camera function has been performed in which there is a void data type camera function having print statement inside it.

In the end, we have used Main class, which is having the main() function an object of Redmi Class has been created and to invoke the functions call() and camera().

Interface & Abstract functions:
Interface Code
Code 1.2

In code 2.2, there is an interface NewPhone having 3 abstract functions inside its body. The abstract functions are namely, call, camera and processor of void data type. It is followed by LG class which implements the interface NewPhone. Inside the body of LG class we are having call(), camera() and processor() all the three abstract functions of NewPhone interface have been overridden inside LG class.

At last there is a Main class, which is having the main() function. Inside the main() function, an object ob of LG class is created. This ob object is used to call invoke the functions call, camera and processor.

Output for Code 1.1:


Output for Code 1.2:



Encapsulation

Encapsulation is a fundamental concept in object-oriented programming that refers to the bundling of data and methods that operate on that data within a single unit, which is called a class in Java. Encapsulation is a way of hiding the implementation details of a class from outside access and only exposing a public interface that can be used to interact with the class.

For example, let's say we want to create a class that represents a person. A person has some attributes, such as name and age, and some behaviors, such as getting and setting their name and age. We can use encapsulation to define a class like this:

Code 1.3

By declaring the instance variables as private, we ensure that they can only be accessed within the class. To allow outside access to the instance variables, we define public methods called getters and setters, which are used to retrieve and modify the values of the instance variables, respectively.

By using getters and setters, we can also enforce some data validation rules and ensure that the internal state of the class remains consistent. For example, we can check that the name is not null or empty, or that the age is within a reasonable range.


Inheritance

Inheritance allows us to derive the properties and behavior of an existing class to a new class. Inheritance enables code reuse, polymorphism, and abstraction.


The new class is called the subclass or the child class, and the existing one is called the superclass or the parent class. 

The subclass inherits all the attributes and methods of the superclass and can also add its own or override some of them.

-One of the benefits of inheritance is that it enables code reuse. We don't have to write the same code for every class that shares some common features. We can write it once in the superclass and inherit it in the subclasses. This makes our code more concise, consistent, and easy to maintain. 

-Another benefit of inheritance is that it enables polymorphism. Polymorphism means that we can use a subclass object as if it were a superclass object. This makes our code more flexible and dynamic. 

-A third benefit of inheritance is that it enables abstraction. Abstraction means that we can hide the implementation details of a class and only expose its interface. For example, we can define an abstract class called Shape that has an abstract method called area. We can then create subclasses like Circle, Square, Triangle, etc. that inherit from Shape and implement the area method according to their own formulas. We can then use a Shape object without knowing its specific type or how it calculates its area.

Let's check out how to implement in the form of code.

Source Code:


Output:


In conclusion, inheritance is a powerful concept in object-oriented programming that allows us to create new classes from existing ones. Inheritance enables code reuse, polymorphism, and abstraction. Inheritance helps us to write code that is more concise, consistent, easy to maintain, flexible, dynamic, and abstract.