oops concepts in java
OOPS Concepts in java or Object-Oriented Programming Concepts are very important. Without having an idea about OOPS concepts, you will not be able to design systems in the object-oriented programming model.
Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc in programming. The main aim of OOP is 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.
Core OOPS concepts in java are:-
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
- Association
- Aggregation
- Composition
![]() |
| oops concepts |
Let’s look into these object-oriented programming concepts one by one. We will use Java programming language for code examples so that you know how to implement OOPS concepts in java.
1.Oops concept java Abstraction:-
Encapsulation is the technique used to implement abstraction in object-oriented programming. Encapsulation is used for access restriction to class members and methods.
Access modifier keywords are used for encapsulation in object oriented programming. For example, encapsulation in java is achieved using
private, protected and public keywords.
- Hides the underlying complexity of data
- Helps avoid repetitive code
- Presents only the signature of internal functionality
- Gives flexibility to programmers to change the implementation of the abstract behaviour
- Partial abstraction (0-100%) can be achieved with abstract classes
- Total abstraction (100%) can be achieved with interfaces
Types of abstraction:
Typically abstraction can be seen in two ways:
Data abstraction:
-
Data abstraction is the way to create complex data types and exposing only meaningful operations to interact with the data type, whereas hiding all the implementation details from outside works.
The benefit of this approach involves capability of improving the implementation over time e.g. solving performance issues is any. The idea is that such changes are not supposed to have any impact on client code since they involve no difference in the abstract behavior.
Data abstraction is the way to create complex data types and exposing only meaningful operations to interact with the data type, whereas hiding all the implementation details from outside works.
The benefit of this approach involves capability of improving the implementation over time e.g. solving performance issues is any. The idea is that such changes are not supposed to have any impact on client code since they involve no difference in the abstract behavior.
Control abstraction:
-
A software is essentially a collection of numerous statements written in any programming language. Most of the times, statement are similar and repeated over places multiple times.
Control abstraction is the process of identifying all such statements and expose them as a unit of work. We normally use this feature when we create a function to perform any work.
2.Oops concept java Encapsulation: - A software is essentially a collection of numerous statements written in any programming language. Most of the times, statement are similar and repeated over places multiple times.Control abstraction is the process of identifying all such statements and expose them as a unit of work. We normally use this feature when we create a function to perform any work.
Encapsulation allows us to protect the data stored in a class from system-wide access. As its name suggests, it safeguards the internal contents of a class like a real-life capsule. You can implement encapsulation in Java by keeping the fields (class variables) private and providing public getter and setter methods to each of them. Java Beans are examples of fully encapsulated classes.
- Restricts direct access to data members (fields) of a class.
- Fields are set to private
- Each field has a getter and setter method
- Getter methods return the field
- Setter methods let us change the value of the field
3.Oops concept java Polymorphism:
Polymorphism refers to the ability to perform a certain action in different ways. In Java, polymorphism can take two forms: method overloading and method overriding. Method overloading happens when various methods with the same name are present in a class. When they are called they are differentiated by the number, order, and types of their parameters. Method overriding occurs when the child class overrides a method of its parent.
- The same method name is used several times.
- Different methods of the same name can be called from the object.
- All Java objects can be considered polymorphic (at the minimum, they are of their own type and instances of the Object class).
- Example of static polymorphism in Java is method overloading.
- Example of dynamic polymorphism in Java is method overriding.
Inheritance is the object-oriented programming concept where an object is based on another object. Inheritance is the mechanism of code reuse. The object that is getting inherited is called superclass and the object that inherits the superclass is called subclass.
We use
extends keyword in java to implement inheritance. Below is a simple example of inheritance in java.
package com.journaldev.java.examples1;
class SuperClassA {
public void foo(){
System.out.println("SuperClassA");
}
}
class SubClassB extends SuperClassA{
public void bar(){
System.out.println("SubClassB");
}
}
public class Test {
public static void main(String args[]){
SubClassB a = new SubClassB();
a.foo();
a.bar();
}
}
5. Oops concept java Association:
Association is the OOPS concept to define the relationship between objects. The association defines the multiplicity between objects. For example Teacher and Student objects. There is a one-to-many relationship between a teacher and students. Similarly, a student can have a one-to-many relationship with teacher objects. However, both student and teacher objects are independent of each other.
6. Oops concept java Aggregation:
Aggregation is a special type of association. In aggregation, objects have their own life cycle but there is ownership. Whenever we have “HAS-A” relationship between objects and ownership then it’s a case of aggregation.
7. Oops concept java Composition:
The composition is a special case of aggregation. The composition is a more restrictive form of aggregation. When the contained object in “HAS-A” relationship can’t exist on its own, then it’s a case of composition. For example, House has-a Room. Here the room can’t exist without the house. Composition is said to be better than inheritance, read more at Composition vs Inheritance.
Abstraction is more about hiding the implementation details. In Java abstraction is achieved through abstract classes and interfaces.
Encapsulation is about wrapping the implementation (code) and the data it manipulates (variables) within the same class. A Java class, where all instance variables are private and only the methods within the class can manipulate those variables, is an example of an encapsulated class.
If you want to read more about abstract classes and interfaces in Java, follow my next post Exploring interfaces and abstract classes in java.
thank you
Thank you

No comments
for more information plz do comment and follow my blog.