Java OOPs Concepts

Tusharchavhan
8 min readAug 19, 2021

In this article, we will learn about the basics of OOPs.

As the name suggests Object-Oriented Programming(OOPs) languages deal with objects.

Aim of OOPs in Java

  1. To improve code readability and reusability by defining a Java program efficiently.
  2. To bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.
  3. To keep the Java code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify, and debug.

List of OOPs concept in Java

1. Object:-

The object is a real-world entity that has a state, behavior, and identity. Is also known as an instance of the Java class. The object can be physical or logical.

An object has

  1. State: It is represented by attributes of an object. It also reflects the properties of an object.
  2. Behavior: It is represented by the methods of an object. It also reflects the response of an object with other objects.
  3. Identity: It gives a unique name to an object and enables one object to interact with other objects.

Examples of object, state, and behavior
Object: House
State: Address, Color, Area
Behavior: Open door, close door

The class for the above example will be

Fig: Java class

We can create the object of the Java class using a new keyword. Let us see an example.

Fig: Object creation in java

2. Class:-

A class is a user-defined blueprint or prototype from which objects are created. The Java class is not a real-world entity, it’s a logical entity. Java classes don't consume any memory. A java class contains variables, methods, and objects of classes. Also class has both static and instance initializers.

Fig: Examples of Class and Object

A class declaration consists of:-

  1. Modifiers: The Java class can be public or default access.
  2. Class name: The class name should start with an initial letter (followed by camel case by convention).
  3. Superclass: The name of the superclass, if any, preceded by the keyword “extends”.
  4. Interfaces: A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword “implements”. A class can implement more than one interface.
  5. Body: The class body surrounded by braces, { }.

Let us see an example of Java class

Fig: Declaration of Java class

In Java, we have an abstract class, which will be declared using abstract keywords.

3. Abstraction:-

Data abstraction is a process of showing only essential things (functional things) and hiding details (internal working).

Let’s see some real-time examples of abstraction

Car:- In a car, we know the functionality of breaks and accelerators. In breaks, we know that if we press the brake, the car stops, but we don’t know the internal mechanism of breaks. In the accelerator, we know that after pressing the accelerator the speed of the car is increased but how the speed was increased we don’t know that. That is the concept of abstraction.

ATM:- In ATM we see options like withdrawal, mini statement, change pin, etc. but we don’t know how it actually works internally what is the internal working of that machine.

In Java, we can achieve abstraction using abstract class as well as interface. Let's see one by one.

  1. Abstract Class:-

The class which is declared by abstract keyword is known as abstract class. By using the abstract class we can achieve 0 to 100% of abstraction. We can not create the object (Instance) of an abstract class. An abstract class contains abstract methods as well as non-abstract methods. The abstract method(only signature, no implementation) defined in the abstract class is must be implemented by the child class.

Let’s see the declaration of abstract class and abstract method.

Fig: Declaration of abstract class

2. Interface:-

The interface is a blueprint of class in which all the methods are abstract, as well as all the variables, are final, static, and public. By using the interface we can achieve 100% of abstraction. An interface can not contain any constructor. We can not create objects of the interface. To use interface methods and variables must be inherited and implemented all the methods by the child class. And one interface can inherit another interface by using extends keyword.

Let's see the declaration of the interface

Fig: Declaration of Interface

Advantages of interfaces in Java

  1. We can achieve 100% abstraction by the interface.
  2. By using interfaces we can achieve multiple inheritances in java which is not supported by using class.
  3. It is also used to achieve loose coupling.

4. Inheritance:-

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system). We can inherit the classes by using the “extend” keyword.

Important terminology used in the interface

  1. Super Class: The class whose features are inherited is known as superclass(or a base class or a parent class).
  2. Sub Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
  3. Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
Fig: Declaration of Inheritance in Java

Types of inheritance present in java:-

  1. Single Inheritance:- In this, B class inherits the A class then that type of inheritance is called single inheritance. Class B is called a derived class, child class, and class A is called a base class or a parent class or superclass.

2.Multilevel Inheritance:- When a class extends a class, which extends another class then this is called multilevel inheritance. For example, class C extends class B and class B extends class A then this type of inheritance is known as multilevel inheritance.
Let's see this in a diagram:

Fig: Multilevel Inheritance

3. Hierarchical Inheritance: When more than one classes inherit the same class then this is called hierarchical inheritance. For example classes B, C and D extend the same class A. Let's see the diagram representation of this:

Fig: Hierarchical Inheritance

4. Multiple Inheritance: When a class can inherit properties of more than one parent class. The problem occurs when there exist methods with the same signature in both the superclass and subclass. On calling the method, the compiler cannot determine which class method to be called and even on calling which class method gets the priority. As Java is simple is the programming language and the compile-time error is better than runtime error so that Java does not support multiple inheritances.

Fig: Multiple Inheritance

Java supports multiple inheritances by using interfaces.

4. Hybrid Inheritance: Hybrid Inheritance in Java is a combination of Inheritances. In this type of Inheritance, more than one kind of inheritance is observed. For example, if we have class B and class C that extend class A and then there is another class D that extends class B and class C, then this type of Inheritance is known as Hybrid Inheritance.

In the diagram shown below, we see another example of Hybrid Inheritance.

Fig: Hybrid Inheritance

But as Multiple inheritance is not supported in java so Hybrid Inheritance is also not supported by Java.

5. Polymorphism:-

The derivation of the word Polymorphism is from two different Greek words- poly and morphs. “Poly” means many and “Morphs” means forms. So polymorphism means many forms. Polymorphism is one of the most significant features of Object-Oriented Programming.

A real-life example of polymorphism: A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism.

In Java polymorphism is mainly divided into two types:

  1. Compile-time Polymorphism

Compile-time is also called static polymorphism and the method overloading. In a class, if two methods with the same name but a different number of parameters or different data type of parameter is present then its called compile-time polymorphism. A real-time example of Compile time polymorphism is Notepad, if we ctrl+A button then it selects all the characters and if we press ctrl+Z then it performs undo operation. Let’s see an example

Fig: Compile time Polymorphism

2. Runtime Polymorphism

Runtime polymorphism is also called Dynamic polymorphism and Method overriding. If two or more classes present the methods with the same name, the same number of parameters, and the same data types of parameters. Let see a real-time example of runtime polymorphism. Tv remote, In tv remote, if we press the volume on home then it will work to increase or decrease volume, and if we to setting and press the volume key then it will increase or decrease the brightness of tv screen. Here the Home is one class and volume key is a parameter and the setting is another class. The volume key in the home class works to change volume and in setting class works to change the brightness of tv screen.

Fig: Method Overriding

6. Encapsulation:-

Encapsulation is the process that binds together the data and code into a single unit and keeps both from being safe from outside interference and misuse. In this process, the data is hidden from other classes and can be accessed only through the current class’s methods. Hence, it is also known as data hiding. Encapsulation acts as a protective wrapper that prevents the code and data from being accessed by outsiders.

Encapsulation is achieved by declaring the variables as private and providing public setter and getter methods to modify and view the variable values(It is also called POJO class). In encapsulation, the fields of a class are made read-only or write-only. This method also improves the re-usability. Let's see the example of Encapsulation

Fig: Encapsulation

--

--