OOPs in Java

Sreehari Nambiar KT
3 min readMay 24, 2021

OOPs stands for object-oriented programming. OOPs is all about creating an object, an object that can have attributes(variables) and methods(functions). In java, we can create objects of a class. Class is like a template of which objects can be created, it describes the state that the object of its type support. If we think of a real-life example, let's take a vehicle that can have x no. of wheels and can hold y no. of people inside. The vehicle can move and stop. But if I ask how many wheels does the vehicle have? it won't make sense right. If I say a car is a vehicle that has 4 wheels can hold 4 people and can move and stop, and when I ask you how many wheels does the car have? you know the answer is 4. Here vehicle can be related to a class, where no. of wheels, capacity, and the fact that it can move and stop describe its state, and car is an object of the vehicle class which actually has a state.

OOPs has several advantages like it provides a clean structure that is easier to understand, maintain, modify and debug. It also helps java to keep DRY (don’t repeat yourself) ie. it's more reusable.

There are 4 main pillars of OOPS in java.

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

Encapsulation — In java, encapsulation is the process of wrapping code and data into a single unit. Encapsulation is done to make sure that sensitive data is hidden from users. Just like a capsule is a mix of chemicals that act as a medicine for a disease, we don’t know what all chemicals are used in that. But we know it's going to help me recover. We can achieve encapsulation by declaring variables of the class as private and provide public get and set methods to access them(called getters and setters). Encapsulation is a type of data hiding done due to improving security. To sum it up, encapsulation helps us to keep related fields and methods together. It also provides us better control over the values of our data. It also can be used to make read-only/ write-only classes.

Abstraction — In java, Abstraction is the process of hiding the implementation details and showing only functionality to the end-user. Just like to switch on the computer we only press the power button we don’t want to know how the computer boots up, how it loads OS and all, we only want to see it working, a similar process is with abstraction, we only want t show the essentials and the rest is kept hidden from the user. We can achieve Abstraction using keywords like abstract(up to 100% abstraction) or interface(100% abstraction). We create an abstract class using the “abstract” keyword. An abstract class can have an abstract(methods with no body) as well as concrete methods. We can also create an interface to achieve abstraction. We use the “interface” keyword to create an interface. The difference between an interface and an abstract class is that interface cannot have concrete methods like the abstract class can. The interface helps us in achieving 100% abstraction. In Abstraction, we do achieve data hiding but it is for reducing complexity.

Inheritance — in java, it is possible to inherit properties of a parent class by using the keyword extends. Let's say we have a class Dog that extends class Animal then the public and protected attribute and methods will be inherited and hence can be used or reused by the child class’s object as well. Inheritance helps in creating modular code and improves code re-usability. To inherit behaviors of an interface we use implements instead of extends. Do note that in java we cannot inherit multiple classes because of the diamond problem, but we can implement multiple interfaces.

Polymorphism — Polymorphism, the word literally means many forms. In java, we can have the same name given to methods that have different behaviors. We can achieve polymorphism with inheritance. We can override a method in a child class to get the same method name to do different things. We can also achieve polymorphism by overloading methods, overloading is nothing but having different arguments making the methods different but has the same method name. Polymorphism is useful as it adds to the code re-usability. An object has a scope of polymorphism if it obeys the IS-A rule. eg-Audi is a car, hence there will be some methods that should be overridden by Audi than being a generic car.

--

--