Java, one of the most widely used programming languages, is built on the principles of Object-Oriented Programming (OOP). This paradigm promotes the organization of software design around data, or objects, rather than functions and logic. Three of the fundamental concepts of OOP in Java are polymorphism, encapsulation, and overloading. In this post, we will explore these concepts, their significance, and how to implement them in Java.
1. Polymorphism
Polymorphism is derived from the Greek words "poly," meaning many, and "morph," meaning forms. In Java, polymorphism allows objects to be treated as instances of their parent class, enabling methods to perform different tasks based on the object that is calling them.
Types of Polymorphism
There are two main types of polymorphism in Java:
Compile-Time Polymorphism (Method Overloading): This occurs when two or more methods in the same class have the same name but different parameters (type or number). The method to be executed is determined at compile time.
Run-Time Polymorphism (Method Overriding): This occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method to be executed is determined at runtime, allowing for dynamic method dispatch.
Example of Method Overloading
2. Encapsulation
Encapsulation is the mechanism of restricting access to certain components of an object and bundling the data (attributes) and methods (functions) that operate on that data into a single unit, or class. This is typically achieved using access modifiers.
Access Modifiers
- private: The member is accessible only within its own class.
- public: The member is accessible from any other class.
- protected: The member is accessible within its own package and by subclasses.
- default: If no modifier is specified, the member is accessible only within its own package.
Example of Encapsulation
In this example, the name
variable is private, meaning it cannot be accessed directly from outside the Student
class. Instead, we use public getter and setter methods to modify and access it, demonstrating encapsulation.
3. Overloading
Overloading refers to the ability to define multiple methods with the same name but different parameters within the same class. This allows for more flexible code as developers can perform different actions based on the arguments passed to the method.
Key Points on Overloading
- Overloaded methods must have different parameter lists (type, number, or both).
- Return type alone cannot be used to distinguish overloaded methods.
Example of Overloading
The previous MathOperations
class is a perfect example of method overloading, as it includes multiple versions of the add
method.