1.
Encapsulation Example
Encapsulation is the concept of wrapping data (variables) and methods that operate on the data into
a single unit, i.e., a class. It restricts direct access to some of the object's components, which is a
means of preventing unintended interference and misuse of the data.
class Student {
// Step 1: Make variables PRIVATE
private String name;
private int age;
// Step 2: Provide PUBLIC getter method
public String getName() {
return name;
}
public int getAge() {
return age;
}
// Step 3: Provide PUBLIC setter method with validation
public void setName(String newName) {
if(newName != null && ![Link]()) {
name = newName;
} else {
[Link]("Invalid name!");
}
}
public void setAge(int newAge) {
if(newAge > 0) {
age = newAge;
} else {
[Link]("Invalid age!");
}
}
}
public class EncapsulationDemo {
public static void main(String[] args) {
Student s1 = new Student();
[Link]("Prakhar");
[Link](21);
[Link]("Student Name: " + [Link]());
[Link]("Student Age: " + [Link]());
}
}
Key Points:
- Private variables prevent direct access from outside.
- Public getters and setters provide controlled access.
- Validation ensures data integrity and correctness.
- Encapsulation improves security and maintainability.
2. Compile-Time Polymorphism (Method Overloading)
Compile-time polymorphism is achieved through method overloading. It allows multiple methods in
the same class to have the same name but different parameter lists. The method to be executed is
determined at compile time based on the method signature.
class Calculator {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
public double add(double a, double b) {
return a + b;
}
}
public class OverloadingDemo {
public static void main(String[] args) {
Calculator calc = new Calculator();
[Link]("Sum of 2 ints: " + [Link](5, 10));
[Link]("Sum of 3 ints: " + [Link](5, 10, 15));
[Link]("Sum of 2 doubles: " + [Link](5.5, 10.5));
}
}
Key Points:
- Method overloading uses same method name with different parameters.
- Compiler determines which method to call based on arguments.
- Improves code readability and reusability.
- Known as compile-time polymorphism.
3. Runtime Polymorphism (Method Overriding)
Runtime polymorphism is achieved through method overriding. 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 based on the object type.
class Animal {
public void sound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
public void sound() {
[Link]("Dog barks");
}
}
class Cat extends Animal {
@Override
public void sound() {
[Link]("Cat meows");
}
}
public class OverridingDemo {
public static void main(String[] args) {
Animal a;
a = new Dog();
[Link](); // Output: Dog barks
a = new Cat();
[Link](); // Output: Cat meows
}
}
Key Points:
- Method overriding allows subclass to provide specific implementation.
- Parent reference can point to child object.
- JVM decides which method to call at runtime.
- Known as runtime polymorphism.