Main pillars of OOP in Java

Category : Java | Sub Category : Object- oriented Programming | By Prasad Bonam Last updated: 2023-07-31 00:56:43 Viewed : 347


In Java, Object-Oriented Programming (OOP) is a fundamental programming paradigm that revolves around the concept of objects. OOP helps in organizing code in a more modular and reusable manner. There are four main pillars of OOP in Java:

  1. Encapsulation: Encapsulation is the process of hiding the internal implementation details of an object and exposing only the necessary functionalities through methods. This is achieved by using access modifiers (public, private, protected) to control access to the objects data and methods. The goal is to protect the objects state and ensure that it can be accessed and modified only in a controlled way.

Example:

java
public class Person { private String name; private int age; // Getters and setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
  1. Inheritance: Inheritance allows a class (subclass/derived class) to inherit the properties and behaviors of another class (superclass/base class). This promotes code reuse and enables the creation of a hierarchy of classes. The subclass can add its own specific behavior while inheriting the common behavior from the superclass.

Example:

java
public class Animal { public void makeSound() { System.out.println("Some generic animal sound."); } } public class Dog extends Animal { @Override public void makeSound() { System.out.println("Bark! Bark!"); } }
  1. Polymorphism: Polymorphism allows objects to be treated as instances of their parent class, enabling multiple forms or behaviors based on the actual type of the object at runtime. There are two types of polymorphism: compile-time (method overloading) and runtime (method overriding).

Example of method overloading (compile-time polymorphism):

java
public class MathOperations { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } }

Example of method overriding (runtime polymorphism):

java
public class Animal { public void makeSound() { System.out.println("Some generic animal sound."); } } public class Dog extends Animal { @Override public void makeSound() { System.out.println("Bark! Bark!"); } } public class Cat extends Animal { @Override public void makeSound() { System.out.println("Meow!"); } }
  1. Abstraction: Abstraction involves creating a simplified representation of an object by only exposing the relevant characteristics and hiding the unnecessary details. Abstract classes and interfaces are used to achieve abstraction in Java.

Example of abstract class:

java
public abstract class Shape { public abstract double calculateArea(); }

Example of an interface:

java
public interface Drawable { void draw(); }

These OOP concepts help developers to create well-organized, modular, and maintainable code by promoting code reuse, reducing complexity, and enhancing flexibility.

Search
Related Articles

Leave a Comment: