OOPS Concepts in Java

In this article, we will learn about the Object-Oriented Programming concepts and its basics.

First, let us look at brief history of OOPS :

  • The first Object-Oriented Programming language is Simula which is the name of two simulation programming language – Simula I (1962-65) and Simula 67 (1967). It has Inheritance, Object , Classes etc concepts. However, It is not truly Object-Oriented Programming language.
  • The first truly Object-Oriented Programming language is Smalltalk as everything in Smalltalk language is represented as Object.

We have now many Object-Oriented Programming languages such as Java, PHP, C#, C++ , Python etc.

OOPS provides many concepts to simplify the development but we can say the main purpose of Object-Oriented Programming is to implement real world entities.

What is OOPS ?

Object Oriented Programming paradigm or OOPs is a programming concept which consider objects as the most important part of your program. Object-Oriented Programming languages are one which uses mainly objects in programming.

OOPs is a paradigm to design a program using Objects and classes to simplify the development process. It provides many concepts for development like inheritancepolymorphism, Objects, data binding, etc.

OOPs main purpose is to implement the real-world entities in programming. It binds the data and their operating functions together so that other part of code cannot access that data.

Object Oriented Programming / OOPs  is used in many modern programming languages like Java and Python.

OOPS Concepts

There are many concepts available in OOPs paradigm to make development easy . Main OOPS concepts are :

  1. Object
  2. Class
  3. Inheritance
  4. Abstraction
  5. Encapsulation
  6. Polymorphism

OOPS Concepts in Java

Below are some other concepts used in OOPs paradigm :

  1. Coupling
  2. Association
  3. Composition
  4. Aggregation
  5. Cohesion

Object

Object is simply an entity which has state and behavior. Object can be a Physical as well as Logical entity / component. For example, Car, Laptop, Book, Clock etc.

OOPS concepts in Java

Object is the basic and main unit of Object_Oriented Programming paradigm. It represent real-world entities. An Object in Java is the  instance of a class.  Classes does not consumes space but Objects does. Every Object contains an address and it takes space in memory.

Objects can communicate with each other without even knowing any data or details of each other’s code. The important thing is to know, which type of data is accepted by the Object and which type of data is returned by Object.

Creation of Object in Java : 

Car car= new Car();

****Car is the name of class, for which we are creating Object****

****car is the name of Object****

****new is the keyword used for runtime memory allocation, it allocates memory to the Object****

Example:

A car is an object in Java because it has states like name, color, brand etc. and its behavior is exposed by the methods such as accelerate and park etc.

OOPS concepts in Java

An object in Java has three parts :

  1. State : State of Object defines the properties of the Object and it is represented by attributes of the object.
  2. Behavior : Behavior of Object is exposed or represented by methods of the class.
  3. Identity : Each Object in Java is unique and at the time of creation unique id of Objects is created and get associated with it.

Class

Class in java is a Collection of objects or group of similar entities / objects.  Class is a logical entity and it does not exist as physical component. Class in Java doesn’t consume any memory or space.

A Class in Java is a user defined blueprint from which similar type of individual objects/entities are created. Class represents the set of methods or properties which are common to all objects of same type.

OOPS concepts in Java

Creation of class in Java : 

public class Animals {}

****Animals is the class name****

****class is the keyword used to declare class****

****public is the access modifier which denotes this class is accessible to all****

Example :

Suppose you have a class named “Animals”. It can have Objects like Cat, Dog , Bird etc. It can have properties or data as color etc.  The behaviour of these objects will be exposed by use of methods. So it can have methods as Barking etc.

Class declaration can have below mentioned components : 

  1. Access Modifiers: Access modifiers define the accessibility of Class.In Java, class can have public or default access.
  2. Class name: Any name could be assigned to a class , the first letter of class should be capital as per Naming Convention.
  3. Superclass (Optional) : If current class is inheriting any super class then extends keyword is used followed by super class’s name.In Java, one class can only inherit  one parent class.
  4. Interfaces(Optional):  If current class is implementing any interfaces the implements keyword is used followed by the interfaces name separated with commas. In java, one class can implement more than one interface.
  5. Class Body: The class body contains methods, properties etc.It is surrounded by braces, { }.

Inheritance

Inheritance is an Object-Oriented Programming concept and it is considered to be one of the most important pillar of OOPs.

Inheritance basically represents parent – child or  IS-A relationship between two classes. It is the concept where one object acquires all the properties and behavior of its parent Object.

Advantages of Inheritance :

  • Inheritance provides robust and natural mechanism for structuring your code.
  • It provides code reusability so that lesser memory gets used.
  • By Inheritance, we can achieve runtime polymorphism.

Types of Inheritance : 

OOPS Concepts in Java

Single Inheritance 

Where one class acquires all properties and methods of one other class.

Multilevel Inheritance

Suppose we have three classes : class A, class B and class C. If class A inherits class B and class B inherits class C, it is considered as Multilevel Inheritance.

Hierarchical Inheritance

When two classes inherits all properties and methods of one class, It is called as Hierarchical Inheritance.

Declaration of Inheritance in Java : 

class Dog extends Animals
{
// fields and methods
}

****Dog is the subclass which is inheriting Animals class****

****extends is the keyword used for Inheritance****

****Animals is the parent class****

OOPS concepts in Java

Important terms used in Inheritance:

  • Super Class: The class whose properties and methods are inherited is known as superclass / base class / parent class.
  • Sub Class: The class which inherits the properties and methods of other class is known as subclass / derived class / extended class / child class. The sub class can also add its own properties and methods.
  • Reusability: Reusability is the main advantage of using Inheritance. Suppose you want to create a new class which has some of the functionality that is same as one existing class so you can inherit that class and re-use the code written in parent class.

Abstraction

Abstraction can be seen as process of representing important features without showing background processes and details. In Abstraction, Only functionality is visible to the user and all the internal details are hidden.

Data Abstraction in Java can also be defined as process of identifying and using only the required properties of Object and ignoring others.

For example, A phone call : As user , we just know the functionality of the phone but we are not concerned about internal processed. Same works with driving Bike as well, we do know how to ride a bike i.e functionality but we are not aware about its internal processing.

OOPS concepts in java

  • Abstract classes and Interfaces are used to achieve abstraction in Java.
  • With Interfaces, we can achieve 100% abstraction.

Creation of Abstract Class in Java : 

public abstract class Animal
{
public abstract void run();
}

****abstract is the keyword to declare class or method as abstract****

Rules for Abstract class in Java : 

  • abstract class is declared with keyword abstract.
  • one abstract class must have at least one abstract method.
  • abstract method does not have any body.

Creation of Interface in Java : 

public interface Running
{
public abstract void run();
}

Rules for Interfaces in Java : 

  • An interface can only contain abstract methods.
  • abstract keyword is optional to declare the method as abstract in interface.

Encapsulation

Encapsulation is a concept of Object-Oriented paradigm, which is a technique of binding data and code together into a single unit.  Encapsulation concept ensures that data members of a class are always hidden from other classes. However, we can access data members of other class but only using the methods.

Simple example of encapsulation can be seen as a capsule, as it wraps medicines into it.

OOPS concepts in Java

In Java, a class is the example of encapsulation concept. However, Java bean / POJO class is considered to be fully encapsulated class as all the data members of these classes are private.

Encapsulation is also known as data-hiding as it prevents data to be accessed by the code outside the class.

Example of Encapsulation in Java :

public class Employee {

private String name;

public String getName()
{
return name;
}

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

Polymorphism

Polymorphism is a concept of OOPs paradigm , When one task is performed in different ways, it is called as Polymorphism. We can also say that, Polymorphism refers to the ability of  objects , functions to take multiple forms.

For Example :

Drawing a shape – draw triangle, rectangle , circle etc.

Speaking  – cat meows , dog barks etc.

OOPS concepts in java

Method overloading and method overriding is used in Java to achieve polymorphism.

There are two types of Polymorphism in Java:

OOPS concepts in Java

Example of Polymorphism in Java through Method Overloading :

public class PolyExample{
  
    public int sum(int a, int b)
    {
        return (a + b);
    }
  
    public int sum(int a, int b, int c)
    {
        return (a + b + c);
    }
    public double sum(double a, double b)
    {
        return (a + b);
    }
  
    public static void main(String args[])
    {
        PolyExampl obj= new PolyExample();
        System.out.println(obj.sum(20, 50));
        System.out.println(obj.sum(100, 300, 200));
        System.out.println(obj.sum(12.5,10.5));
    }
}

Output : 

60

600

23.0

Coupling

Coupling in OOPs paradigm refers to the concept where there is dependencies or flow of information or coordination is happening between different classes. This comes in picture whenever classes are accessible or aware-of each other. We use access modifiers (public, private, protected) to achieve coupling in java. Interfaces are used in java for weak coupling as interfaces do not have any concrete implementation.

Types of Coupling :

  • Loose coupling
  • Tight coupling

Association

Association in OOPs paradigm simply represents the relationships of different Objects. One Object can be associated with one object or many objects.

Four type of associations is possible between Objects :

  • One to One
  • One to Many
  • Many to One
  • Many to Many

Example :

  • One Country – One Prime Minister
  • One Prime Minister – Many Ministers
  • Many Ministers – One Prime Minister
  • Many Ministers – Many Departments

Association in Java can be :

  • Unidirectional
  • Bidirectional

Composition

The composition in OOPs paradigm is another way to achieve Association. The relationship where one object has other object as its state but this is to be noted that, composition relationship is strong relationship between those objects.

In composition, the containing objects do not have an independent existence that’s why it is strong relationship. In case of removal of parent object, all the child objects will also be discarded.

Aggregation

Aggregation in java is another way to achieve Association. The relationship where one object have other objects as its state is called as Aggregation. This type of relationship is a weak relationship between Objects.

class Employee{

String name;

Address address;

.......

}

class Address {

.........

}

It is also called as a has-a relationship in Java

Cohesion

Cohesion in OOPs paradigm is the level of a component which performs task which is single and well defined. Highly cohesive methods performs one single and well defined task. However, One task is separated into multiple parts by the weakly cohesive method.

Example :

The java.io package is a highly cohesive package as it only contains the I/O related interfaces and classes.

The java.util package is a weakly cohesive package as it contains multiple unrelated interfaces and classes.

Types of Cohesion:

  • High cohesion
  • Low cohesion

Conclusion

That’s all folks! In this article, you have got the complete overview of Object-Oriented Programming paradigm and its concepts and examples.

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply