pcpf chp2
pcpf chp2
Ans. Encapsulation
Example:
Java
In this example:
• year, model, and color are private attributes, meaning they can only be
accessed within the Car class. This protects the data from accidental
modification.
• The setYear, getYear, and similar methods provide controlled access to the
attributes. This is known as encapsulation.
Abstraction
Key Points:
Example in Java:
}
}
In the Car example, the Car class is an abstraction of a real-world car. It represents
the essential features of a car, such as its year, model, and color. Encapsulation
ensures that these features are protected and can only be accessed through
specific methods.
In essence, encapsulation is a tool to implement abstraction. By hiding the
internal implementation details and providing a well-defined interface,
encapsulation allows for a more abstract representation of an object.
Q] Polymorphism
Example in Java:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
Initialization (Constructor):
Example in Java:
class Car {
Car() { // Constructor
System.out.println("Car created");
}
@Override
protected void finalize() { // Finalizer
System.out.println("Car destroyed");
}
}
Q] Overloading
Definition:
class MathOperations {
public int add(int a, int b) {
return a + b;
}