Polymorphism in Java with Example
Polymorphism is a core concept in OOP (Object-Oriented Programming) that allows objects of different classes to be treated as objects of a common superclass. It enables one interface to represent different underlying forms (data types).
There are two types of polymorphism in Java:
Compile-time Polymorphism (Method Overloading)
Runtime Polymorphism (Method Overriding)
1). Compile-Time Polymorphism (Method Overloading)
When multiple methods have the same name but different parameters, Java determines which method to call at compile time.
Example: Method Overloading
class Calculator {
// Method to add two integers
int add(int a, int b) {
return a + b;
}
// Method to add three integers (Overloaded)
int add(int a, int b, int c) {
return a + b + c;
}
// Method to add two doubles (Overloaded)
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10)); // Calls int add(int, int) → 15
System.out.println(calc.add(5, 10, 15)); // Calls int add(int, int, int) → 30
System.out.println(calc.add(2.5, 3.5)); // Calls double add(double, double) → 6.0
}
}
Explanation:
The correct method is chosen at compile time based on number/type of arguments.
2. Runtime Polymorphism (Method Overriding + Inheritance)
When a subclass provides a specific implementation of a method already defined in its superclass, Java determines which method to call at runtime.
Example: Method Overriding
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks: Woof!");
}
}
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Cat meows: Meow!");
}
}
public class Main {
public static void main(String[] args) {
Animal animal1 = new Dog(); // Upcasting (Dog treated as Animal)
Animal animal2 = new Cat(); // Upcasting (Cat treated as Animal)
animal1.makeSound(); // Calls Dog's makeSound() → "Woof!"
animal2.makeSound(); // Calls Cat's makeSound() → "Meow!"
}
}
Explanation:
The method to execute is decided at runtime based on the actual object type, not the reference type.
This is also called Dynamic Method Dispatch.
Key Takeaways
Feature Compile-Time Polymorphism (Overloading) Runtime Polymorphism (Overriding)
Definition Same method name, different parameters Subclass overrides superclass method
Resolution At compile time At runtime
Inheritance Not required Required
Example add(int a, int b) vs add(double a, double b) Dog overrides Animal’s makeSound()
Real-World Use Cases
Method Overloading:
System.out.println() (works with int, String, double, etc.)
Constructor overloading (multiple ways to initialize an object).
Method Overriding:
Custom implementations in subclasses (e.g., different Payment methods: CreditCard, PayPal).
Implementing interfaces (e.g., List → ArrayList, LinkedList).
Polymorphism makes code flexible, reusable, and easier to maintain. 🚀
Encapsulation in Java with Example
Encapsulation is one of the four fundamental OOP concepts (along with Inheritance, Polymorphism, and Abstraction). It refers to bundling data (variables) and methods (functions) that operate on the data into a single unit (class) and restricting direct access to some of the object’s components.
Key Principles of Encapsulation
Data Hiding – Making fields private to prevent direct external access.
Controlled Access – Providing public getter/setter methods to modify and view data safely.
Increased Security – Preventing unauthorized modifications.
Example: Encapsulation in Java
public class Google {
private int emp_salary;
public static String ho_address = "Mountain View";
public static boolean working = true;
private void get_user_details() {
int no = 10;
System.out.println("All Users Details");
}
public void search_Results() {
System.out.println("Searching Results");
}
}
public class User {
public static void main(String[] args) { //Method Signature
Google googleObj = new Google() ;
//System.out.println(googleObj.emp_salary);
System.out.println(Google.working);
System.out.println(Google.ho_address);
//googleObj.get_user_details();
googleObj.search_Results();
User.main();
}
public static void main(){
System.out.println("Overloaded Main Method");
}
}
Key Rules
Class-level:
Only public or default (no private/protected).
Variables/Methods:
All 4 modifiers allowed.
Security Tip:
Start with private, widen access only when needed.
This ensures encapsulation while providing flexibility. 🚀
Inheritance in Java Explained Simply
Inheritance is like passing down family traits in programming. It lets one class (child) automatically get properties and methods from another class (parent).
*Basic Example: Animals *
// Parent class (Superclass)
class Animal {
void eat() {
System.out.println("This animal eats food");
}
}
// Child class (Subclass)
class Dog extends Animal { // "extends" means inherits
void bark() {
System.out.println("Dog barks: Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited from Animal
myDog.bark(); // Own method
}
}
Output:
This animal eats food
Dog barks: Woof!
Why This Matters:
Code Reuse - Dog automatically gets eat() without rewriting it
Organization - Related classes share common logic in one place
Key Concepts:
Keyword Purpose
extends Makes a class inherit another
super() Calls parent's constructor
protected Lets children access (but keeps private from others)
Note: AI-generated, for reference only
------------------------------- End of the Blog ----------------------------------