Java OOP Concepts with Polymorphism
1. Difference Between Runtime and Compile-Time Polymorphism
Compile-Time Polymorphism (Method Overloading):
- Occurs when multiple methods have the same name but different parameters.
- Decision is made at compile time.
- Achieved through method overloading.
- Example:
int add(int a, int b)
double add(double a, double b)
Runtime Polymorphism (Method Overriding):
- Occurs when a subclass provides a specific implementation of a method already defined in its superclass.
- Decision is made at runtime.
- Achieved through method overriding using inheritance.
- Example:
class Animal { void sound() {} }
class Dog extends Animal { void sound() { System.out.println("Bark"); } }
2. Runtime Polymorphism
Definition: Allows a method to behave differently based on the object that invokes it.
Implementation:
- Requires method overriding.
- Enables dynamic method dispatch.
Example:
class Animal { void makeSound() { System.out.println("Animal sound"); } }
class Dog extends Animal { void makeSound() { System.out.println("Bark"); } }
Animal a = new Dog(); a.makeSound(); // Output: Bark
Advantages:
- Supports dynamic behavior
- Code becomes flexible and extensible
Disadvantages:
- Slower performance due to runtime resolution
Java OOP Concepts with Polymorphism
3. Compile-Time Polymorphism
Definition: The method call is resolved at compile time.
Implementation:
- Achieved through method overloading.
- Methods with same name but different parameters.
Example:
class Math {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
Advantages:
- Faster execution
Disadvantages:
- Less flexible compared to runtime polymorphism
4. Class and Object (OOP Basics)
Class: A blueprint for creating objects.
Object: An instance of a class containing state and behavior.
Example:
class Car {
String color;
void drive() { System.out.println("Driving"); }
Car c = new Car();
c.color = "Red";
c.drive();
5. Java Introduction
Java is a high-level, class-based, object-oriented programming language.
Features:
- Platform independent (WORA)
Java OOP Concepts with Polymorphism
- Secure and robust
- Supports OOP principles (Inheritance, Polymorphism, Encapsulation, Abstraction)
- Has built-in garbage collection
6. Variables, Operators, Java ArrayList
Variables: Store data values (e.g., int, String)
Operators: Symbols that perform operations (+, -, *, /, ==, etc.)
ArrayList: A resizable array implementation in Java.
Example:
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.remove("Apple");
7. Constructor Details
Constructor: Special method to initialize objects.
Types:
- Default constructor
- Parameterized constructor
Example:
class Student {
String name;
Student(String name) {
this.name = name;
8. Encapsulation
Definition: Wrapping data and methods into a single unit (class) and restricting direct access to data.
Implementation:
- Use private variables and public getters/setters.
Example:
Java OOP Concepts with Polymorphism
class Bank {
private int balance;
public int getBalance() { return balance; }
public void deposit(int amount) { balance += amount; }
Benefits:
- Improves security
- Controls data access
9. Polymorphism Details
Definition: Ability of an object to take many forms.
Types:
- Compile-Time (Overloading)
- Runtime (Overriding)
Purpose:
- Achieve dynamic behavior
- Promote code reuse and flexibility
Example:
class Printer {
void print(String s) { System.out.println(s); }
void print(int i) { System.out.println(i); }