12 OOPS Concepts for Java Beginners - All OOPS Concepts in Java

In this article, we will discuss basic and advanced OOPS concepts that every Java beginners should know.

As a Java beginner, you may already know the below basic OOPS concepts:

Apart from the above basic OOPS concepts, you should also check out the below advanced OOPS concepts:
For a Java developer, having a strong understanding of object-oriented programming is a must. Without having a strong foundation on OOPS, one can't realize the beauty of an object-oriented programming language like Java. If you don't have a good idea of what OOPS is, even though you are using the OOP language, you may be still coding in a procedural way. 

Just studying OOPS concept definitions won't help much. We should know how to apply those OOPS concepts in designing a solution in an OO way. 

Let's quickly discuss all 12 OOPS concepts.

12 OOPS Concepts for Java Developers - All OOPS Concepts in Java

1. Object

The Object is the real-time entity having some state and behavior. In Java, Object is an instance of the class having the instance variables as the state of the object and the methods as the behavior of the object. 

The object of a class can be created by using the new keyword in Java Programming language.

For example: Let's create a simple Student class that has name and college fields:
package net.javaguides.corejava.oops;

public class Student {
    private String name;
    private String college;

    public Student(String name, String college) {
        super();
        this.name = name;
        this.college = college;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getCollege() {
        return college;
    }

    public void setCollege(String college) {
        this.college = college;
    }

    public static void main(String[] args) {

        Student student = new Student("Ramesh", "BVB");
        Student student2 = new Student("Prakash", "GEC");
        Student student3 = new Student("Pramod", "IIT");
    }
}
From the above program, the Student objects are:
Student student = new Student("Ramesh", "BVB");
Student student2 = new Student("Prakash", "GEC");
Student student3 = new Student("Pramod", "IIT");
From the above program, we have seen how to create, declare, and initialize a Student object in Java.

2. Class

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. In short, a class is the specification or template of an object.
Let’s look at an example of a class and analyze its various parts in the below diagram. This example declares the class Circle, which has the member-variables x, y, and radius of type Integer and the two member-methods, area() and fillColor().

Learn more about Class in Java at What is Class in Java with Programming Examples.

3. Abstraction

Abstraction means hiding lower-level details and exposing only the essential and relevant details to the users. 

In Java, abstraction is achieved by Interfaces and Abstract classes. We can achieve 100% abstraction using Interfaces.

Read more about Abstraction in Java with examples at Abstraction in Java with Example.

4. Encapsulation

Encapsulation is a process of wrapping data and methods in a single unit is called encapsulation.
In OOP, data and methods operating on that data are combined together to form a single unit, which is referred to as a Class

Encapsulation is the mechanism that binds together code and the data it manipulates and keeps both safe from outside interference and misuse. One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper.
Read more at Encapsulation in Java with Example

5. Inheritance

Inheritance is a process of obtaining the data members and methods from one class to another class, plus can have its own is known as inheritance. It is one of the fundamental features of object-oriented programming.

Inheritance - IS-A relationship between a superclass and its subclasses.

Super Class: The class whose features are inherited is known as a superclass (or a base class or a parent class).
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.
Read more about Inheritance at Inheritance in Java with Example.

6. Polymorphism 

The process of representing one form in multiple forms is known as Polymorphism.

Different definitions of Polymorphism are:
  • Polymorphism is to perform a single action in different ways.
  • Polymorphism allows you to define one interface and have multiple implementations
  • We can create functions or reference variables that behave differently in a different programmatic context.
  • Polymorphism means many forms.

 Read more at Polymorphism in Java with Example.

7. Association

Intent

  • It represents a relationship between two or more objects where all objects have their own lifecycle and there is no owner. The name of an association specifies the nature of the relationship between objects.
  • Association is a relation between two separate classes that establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many.
  • In Object-Oriented programming, an Object communicates to other Objects to use functionality and services provided by that object.
There are two forms of association
  • Composition
  • Aggregation
Read more on Association in Java with Example

8. Composition

Intent

Composition is an association that represents a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted. It has a stronger relationship. Key Points
  • It represents a part-of relationship.
  • In composition, both the entities are dependent on each other.
  • When there is a composition between two entities, the composed object cannot exist without the other entity.
  • For example, if order HAS-A line-items, then an order is a whole and line items are parts. If an order is deleted then all corresponding line items for that order should be deleted.
  • Favor Composition over Inheritance.
Read more on Composition in Java with Example

9. Aggregation

Intent

  • Aggregation is an association that represents a part of a whole relationship where a part can exist without a whole. It has a weaker relationship.
  • It is a specialized form of Association where all object has its own lifecycle but there is ownership. This represents a “whole-part or a-part-of” relationship.
  • Let’s take an example of the relationship between Department and Teacher. A Teacher may belong to multiple departments. Hence Teacher is a part of multiple departments. But if we delete a Department, Teacher Object will not destroy.
Read more on Aggregation in Java with Example

10. Cohesion

Intent

The term cohesion is used to indicate the degree to which a class has a single, well-focused responsibility. Cohesion is a measure of how the methods of a class or a module are meaningfully and strongly related and how focused they are in providing a well-defined purpose to the system.

Explanation

  • In object-oriented design, cohesion refers all about how a single class is designed. Cohesion is the Object-Oriented principle most closely associated with making sure that a class is designed with a single, well-focused purpose.
  • The more focused a class is, the cohesiveness of that class is more.
  • The advantages of high cohesion is that such classes are much easier to maintain (and less frequently changed) than classes with low cohesion. Another benefit of high cohesion is that classes with a well-focused purpose tend to be more reusable than other classes.
Read more on Cohesion in Java with Example.

11. Coupling

Intent

Coupling refers to the degree to which one class knows about another class. If one class uses another class, that is coupling. Low dependencies between “artifacts” (classes, modules, components). There shouldn’t be too much of dependency between the modules, even if there is a dependency it should be via the interfaces and should be minimal.
Key Points
  • While creating a complex application in java, the logic of one class will call the logic of another class to provide the same service to the clients.
  • If one class calling another class logic then it is called collaboration.
  • When one class is collaborating with another class then there exists tight-coupling between the two classes.
  • If one class wants to call the logic of a second class then the first-class needs an object of the second class it means the first class creates an object of the second class.
Read more on Coupling in Java with Example 

12. Delegation

Intent

  • Hand over the responsibility for a particular task to another class or method.
  • It is a technique where an object expresses certain behavior to the outside but in reality delegates responsibility for implementing that behavior to an associated object
Applicability
Use the Delegation in order to achieve the following
  • Reduce the coupling of methods to their class
  • Components that behave identically, but realize that this situation can change in the future.
  • If you need to use functionality in another class but you do not want to change that functionality then use delegation instead of inheritance.
Read more on Delegation in Java with Example

Conclusion

We have discussed below basic and advanced OOPS concepts:
  • Object
  • Class
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Association
  • Composition
  • Aggregation
  • Delegation
  • Coupling
  • Cohesion 

Comments