Java OOP Concepts with Polymorphism
1. Difference Between Runtime and Compile-Time Polymorphism
Runtime Polymorphism:
- Achieved through method overriding.
- The method to be executed is determined at runtime based on the object's actual type.
- Requires inheritance.
- More flexible, supports dynamic method dispatch.
- Example:
class Animal { void sound() { System.out.println("Animal sound"); } }
class Dog extends Animal { void sound() { System.out.println("Dog barks"); } }
Animal a = new Dog(); a.sound(); // Output: Dog barks
Compile-Time Polymorphism:
- Achieved through method overloading.
- The method to be executed is determined at compile time.
- No inheritance required.
- Faster due to early binding.
- Example:
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
2. Run-Time Polymorphism (Definition, Case, Method, Adv & Disadv, Implementation)
Definition: Polymorphism where method call is resolved during runtime.
Use Case: When behavior needs to vary dynamically during execution.
Methods: Uses method overriding and dynamic method dispatch.
Advantages:
- Provides flexibility and extensibility.
- Supports interface-based design.
Disadvantages:
- Slightly slower due to late binding.
- More complex debugging.
Java OOP Concepts with Polymorphism
Implementation:
interface Shape { void draw(); }
class Circle implements Shape { public void draw() { System.out.println("Circle"); } }
Shape s = new Circle(); s.draw(); // Output: Circle
3. Compile-Time Polymorphism (With Scenario to Implementation)
Definition: Polymorphism where method resolution is done at compile time.
Scenario: Same operation, different number or types of parameters.
Example:
class Printer {
void print(String s) { System.out.println(s); }
void print(int i) { System.out.println(i); }
Benefits:
- Improves code readability.
- Increases performance due to early binding.
Limitation:
- Cannot override methods with different parameters in subclass.
4. Class and Object (OOP Basic) [In Details]
Class: Blueprint of objects that defines fields and methods.
Object: Instance of a class which holds actual data.
Example:
class Car {
String color;
void drive() { System.out.println("Driving"); }
Car c = new Car(); c.color = "Red"; c.drive();
OOP Principles:
- Encapsulation
- Inheritance
- Polymorphism
Java OOP Concepts with Polymorphism
- Abstraction
5. Java Introduction
Java is a high-level, object-oriented, platform-independent programming language.
Key Features:
- Write Once, Run Anywhere (WORA)
- Secure and robust
- Built-in memory management and garbage collection
- Supports multithreading and networking
Used for:
- Web development
- Android apps
- Enterprise software
6. Variables, Operators, Java ArrayList
Variables:
- Hold data values.
- Types: int, float, String, boolean, etc.
Operators:
- Arithmetic: +, -, *, /
- Relational: ==, !=, >, <
- Logical: &&, ||, !
Java ArrayList:
- Dynamic array from java.util package.
- Allows duplicate values and maintains insertion order.
Example:
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple"); fruits.remove("Apple");
Java OOP Concepts with Polymorphism
7. Constructor Details (Definition, Type, Use, Implementation)
Definition: Special method used to initialize objects.
Types:
- Default Constructor: No parameters.
- Parameterized Constructor: Accepts parameters.
Use:
- Set initial values to object fields.
Example:
class Student {
String name;
Student(String n) {
name = n;
8. Encapsulation (Definition, Type, Use, Adv & Disadv, Purpose, Implementation)
Definition: Binding of data and methods that operate on the data into a single unit (class).
Purpose: Data hiding and controlled access.
Types: No specific types, but achieved using private fields and public getters/setters.
Advantages:
- Improved maintainability and flexibility
- Controlled access to fields
Disadvantages:
- Slight overhead in accessing private data
Example:
class Account {
private int balance;
public int getBalance() { return balance; }
public void deposit(int amt) { balance += amt; }
}
Java OOP Concepts with Polymorphism
9. Polymorphism Details (Definition, Type, Use, Adv & Disadv, Purpose, Implementation)
Definition: Ability of an object to take on many forms.
Types:
- Compile-Time (Overloading)
- Runtime (Overriding)
Purpose:
- Code reusability and flexibility
Advantages:
- Enables dynamic behavior
- Improves readability and code management
Disadvantages:
- Slight complexity in understanding runtime behavior
Implementation:
class Shape {
void draw() { System.out.println("Drawing"); }
class Circle extends Shape {
void draw() { System.out.println("Circle"); }
10. Method Overloading (Implementation)
Definition: Same method name with different parameters in the same class.
Rules:
- Vary number or type of arguments
- Return type can be different (but not sufficient alone)
Example:
class Display {
void show(int a) { System.out.println(a); }
void show(String s) { System.out.println(s); }