Object-Oriented Programming (OOP) principles and their application in Java.
2 min readDec 27, 2023
Object-Oriented Programming (OOP) is a programming paradigm that uses the concept of “objects” to organize code. It revolves around the four main principles: encapsulation, inheritance, polymorphism, and abstraction. I’ll explain each principle and how they are applied in Java:
Encapsulation:
- Encapsulation is when data and the methods that work with the data are bundled together into a single unit called a class. It restricts access to some of the object’s components and prevents the accidental modification of data.
- Application in Java: In Java, you can use access modifiers (
private
,protected
,public
) to control the visibility of variables and methods. For example: public class Car { private String model; public String getModel() { return model; } public void setModel(String model) { this.model = model; } }
- Here, the
model
variable is encapsulated within theCar
class, and access is provided through getter and setter methods.
Inheritance:
- Definition: Inheritance is a mechanism that allows a class to inherit properties and behaviors from another class, establishing a relationship between them. The class that is inherited from is called the superclass (or base class), and the class that inherits is called the subclass (or derived class).
- Application in Java: In Java, you can use the
extends
keyword to achieve inheritance. For example: public class SportsCar extends Car { // Additional properties and methods specific to SportsCar }
- Here,
SportsCar
is a subclass of theCar
class, inheriting its properties and methods.
Polymorphism:
- Definition: Polymorphism allows objects of different classes to be treated as objects of a common base class. It allows a single interface to be used for a general class of actions.
- Application in Java: Polymorphism in Java can be achieved through method overriding and interfaces. For example:
public class Animal { public void makeSound() { System.out.println("Some generic sound"); } } public class Dog extends Animal { @Override public void makeSound() { System.out.println("Bark!"); } }
- Here, both
Animal
andDog
have amakeSound
method, and when you treat aDog
object as anAnimal
, the overridden method in theDog
class is invoked.
Abstraction:
- Definition: Abstraction is the process of hiding the complex implementation details and showing only the necessary features of an object.
- Application in Java: In Java, abstraction is achieved through abstract classes and interfaces. For example:
public abstract class Shape { abstract double area(); }
- Here,
Shape
is an abstract class with an abstract methodarea()
. Concrete subclasses (e.g.,Circle
,Rectangle
) provide their own implementations of thearea()
method.
These principles collectively contribute to the flexibility, modularity, and maintainability of code in object-oriented systems like Java. They enable developers to create reusable and understandable code by modeling real-world entities and their interactions.