Object oriented programming in Java
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which can contain data in the form of fields (attributes or properties) and code in the form of procedures (methods or functions). OOP focuses on organizing software into objects that interact with each other to accomplish tasks.
OOP promotes modular, maintainable, and scalable software development by organizing code into reusable and understandable components. It emphasizes concepts such as data encapsulation, code reuse, and separation of concerns, which contribute to building robust and flexible software systems.
What are the 4 pillars of OOPs?
The four fundamental principles of Object-Oriented Programming (OOP) are commonly referred to as the "Four Pillars of OOP." They are:
Encapsulation:
Encapsulation refers to the wrapping of data (attributes or properties) and its associated methods (functions or procedures) that operate on the data into a single unit, called a class. It allows the internal state of an object to be hidden from outside interference and accessed only through well-defined interfaces (public methods). Encapsulation provides data protection, abstraction, and modularity, enhancing code maintainability and reusability.
Encapsulation Example:
class User {
private String username;
private String emailAddress;
public User(String username, String email) {
this.username = username;
this.emailAddress = email;
}
public String getUsername() {
return this.username;
}
public String getEmailAddress() {
return this.emailAddress;
}
}
public class EncapsulationExample {
public static void main(String[] args) {
User user = new User("Ankit", "ankit@gmail.com");
System.out.println("username: " + user.getUsername());
}
}
Inheritance:
Inheritance is a mechanism that allows a class (subclass or derived class) to inherit properties and behavior (methods) from another class (superclass or base class). It promotes code reuse by enabling subclasses to inherit and extend the functionality defined in their superclass. Inheritance facilitates the creation of a hierarchy of classes with specialized behavior, promoting code organization and scalability.
Inheritance example:
package main.java.dev.byankit.oops;
class Dog {
public void type() {
System.out.println("Dog is a domestic animal");
}
}
class Labrador extends Dog {
public void type() {
System.out.println("Labrador is a breed of dog");
}
}
class GermanShepherd extends Dog {
public void type() {
System.out.println("German Shepherd is a breed of dog");
}
}
public class Inheritance {
public static void main(String[] args) {
Dog dog = new Dog();
dog.type();
Labrador labrador = new Labrador();
labrador.type();
GermanShepherd germanShepherd = new GermanShepherd();
germanShepherd.type();
}
}
Polymorphism:
Polymorphism enables objects to be treated as instances of their superclass, allowing them to take on multiple forms. It allows different objects to respond to the same message (method call) in different ways, based on their specific implementations. Polymorphism simplifies code by allowing methods to be written to work with objects of a superclass, without needing to know the exact subclass at compile time. Polymorphism is achieved through method overriding and method overloading.<br/> There are two types of polymorphism:
- Compile time polymorphism (method overloading), also known as static binding
- Runtime Polymorphism (method overriding), also known as dynamic binding
Polymorphism Example:
package main.java.dev.byankit.oops;
class Calculator {
// Method overloading (Compile time polymorphism)
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
class Animal {
// Method overriding (Run time polymorphism)
public void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
public class Polymorphism {
public static void main(String[] args) {
// Compile time polymorphism
Calculator calculator = new Calculator();
System.out.println(calculator.add(5, 10));
System.out.println(calculator.add(5.5, 10.5));
// Run time polymorphism
Animal animal = new Dog();
animal.sound();
}
}
Abstraction:
Abstraction involves simplifying complex systems by focusing on the essential properties while hiding unnecessary details. It allows developers to create abstract classes and interfaces to define common behavior without specifying implementation details. Abstraction helps in managing complexity, improving code maintainability, and promoting code reuse by providing a clear separation between interface and implementation.
Abstraction Example:
abstract class Shape {
public abstract double area();
public void describe() {
System.out.println("This shape has an area of " + area());
}
}
class Circle extends Shape {
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape {
private final double width;
private final double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double area() {
return width * height;
}
}
public class AbstractionExample {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 6);
circle.describe();
rectangle.describe();
}
}
Summary
The four pillars of object-oriented programming work together to make Java applications easier to understand, extend, and maintain:
- Encapsulation protects an object's state and exposes controlled access through methods.
- Inheritance allows a class to reuse and extend behavior from a parent class.
- Polymorphism lets the same method call produce different behavior depending on the object.
- Abstraction exposes essential behavior while hiding unnecessary implementation details.
Used together, these principles help separate responsibilities, reduce duplication, and make code flexible as an application grows.