Oops Concepts In Java
We are discussing Java Oops (Object-oriented programming) with examples in today’s session, which is designed to serve learners’ needs.
Our discussion will focus on OOPS concepts most frequently used in Java:
- Class
- Object
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
Java Oops
Oop is an acronym for Object-Oriented Programming, which we refer to in Java Oops.
Object-oriented programming involves creating objects that contain both data and methods, whereas procedural programming involves writing procedures or methods that perform operations on data.
In comparison to procedural programming, object-oriented programming offers several advantages:
- It is faster and easier to implement Oops.
- A clear structure for programs is provided by Oops.
- A key benefit of OOP is that it keeps Java code DRY, meaning “Don’t Repeat Yourself”, and makes it easier to maintain, modify, and debug.
- By using OOPs, full reusable applications can be developed efficiently with less code and a shorter development cycle.
Hint: It involves reducing code repetition by following the “Don’t Repeat Yourself” (DRY) principle. Rather than repeating all the codes that are common to the application, you should extract them out and place them at a single location.
Java Oops – Classes and Objects
In object-oriented programming, classes and objects are the key components.
Class:
An object collection is called a class in Java Oops. In other words, it is a logical entity.
An object can also be created from a class as a blueprint. The class does not take up any space.
Objects:
Objects are instances of classes. Addresses and memory space are both contained in an object.
Data and code can be exchanged between objects without knowing details of each other’s details.
All that matters are the types of messages that are accepted and the types of responses that the objects return.
Objects are entities that have state and behavior – In order to understand the difference between classes and objects, take a look at the following illustration:
Another example:
Class Objects Sports Football Baseball Table Tennis American Football (NFL)
In other words, classes are templates for objects, and objects are instances of classes.
All variables and methods from the class are inherited by individual objects when they are created.
The next chapter will cover classes and objects in more detail.
Java Oops – Inheritance
The process of inheriting all the properties and behaviors of a parent object is known as inheritance.
It can be applied to facilitate code reuse.
It is capable of achieving runtime polymorphism.
Java Oops – Polymorphism
Polymorphism occurs when a task can be performed in different ways.
It might be necessary to draw something, such as a shape, circle, triangle, etc., to convince the customer differently.
Polymorphism can be achieved with Java method overloading and overriding.
As another example, a HR team hires and fires and developers write codes etc.
Java Oops – Abstraction
Abstraction is the act of hiding internal details and displaying functionality.
For example, we don’t know the internal processing of electric cars.
To achieve abstraction in Java, we use abstract classes and interfaces.
Java Oops – Encapsulation
The process of encapsulating code and data into a single unit is known as encapsulation.
For example, an array contains multiple index values.
Java classes are an example of encapsulation. The Java bean’s data members are all private, making it a fully encapsulated class.