Imperative Paradigm: Data Abstraction in Object Orientation.
b) What is Inheritance? Explain different types of Inheritance in OPP.
(10) [may24; may23; dec22]
[In note book]
Q1. a) Explain Encapsulation and Abstraction with suitable examples from C++ or
Java. (05) [dec23]
Q1. a) Explain encapsulation with example. How does it differ from
abstraction? (10) [may24; may23]
Ans. Encapsulation
Encapsulation is a fundamental concept in object-oriented programming that
involves bundling data (attributes) and methods (functions) that operate on that
data within a single unit, called a class. This encapsulation helps in:
• Data Hiding: Protecting data from accidental or intentional modification.
• Modularity: Breaking down complex systems into smaller, manageable
units.
• Reusability: Creating reusable components that can be used in different
parts of a program or in other programs.
Example:
Consider a Car class:
Java
public class Car {
private int year;
private String model;
private String color;
public void setYear(int year) {
this.year = year;
}
public int getYear() {
return year;
// Similar methods for model and color
}
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
Abstraction is the process of simplifying complex systems by focusing on the
essential features and hiding unnecessary details. It's about identifying the key
characteristics of an object and representing them in a simplified form.
Key Points:
• Interface: An interface defines a set of methods that a class must
implement.
• Abstract Class: A class that cannot be instantiated directly and serves as a
base for other classes.
• Polymorphism: The ability of objects of different types to be treated as
objects of a common superclass.
By using abstraction, you can create more flexible, maintainable, and reusable
code.
Example in Java:
abstract class Animal {
public abstract void makeSound();
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Woof!");
}
}
class Cat extends Animal {
public void makeSound() {
System.out.println("Meow!");
}
}
Difference between Encapsulation and Abstraction:
While encapsulation focuses on bundling data and methods, abstraction focuses
on identifying the essential features of an object. Encapsulation is a mechanism to
achieve abstraction.
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
• Polymorphism means "many forms" and allows methods to have different
implementations depending on the object calling them.
• Polymorphism ensures flexibility and reusability in OOP.
Types:
Compile-Time Polymorphism (Static Binding): Achieved through
method overloading and operator overloading.
Runtime Polymorphism (Dynamic Binding): Achieved through method
overriding and virtual functions.
Example in Java:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog(); // Polymorphism
animal.sound(); // Output: Bark
}
}
Q] Dynamic Binding
• Dynamic Binding (or late binding) refers to resolving a method call at
runtime rather than compile-time.
• It is a key feature of polymorphism in OOP.
• Example in Java:
class Base {
void display() {
System.out.println("Base class");
}
}
class Derived extends Base {
@Override
void display() {
System.out.println("Derived class");
}
}
public class Main {
public static void main(String[] args) {
Base obj = new Derived();
obj.display(); // Output: Derived class
}
}
Q] Initialization and Finalization
Initialization (Constructor):
• Special method used to initialize objects.
• Same name as the class, and no return type.
Finalization (Destructor in C++ / finalize() in Java):
• Used to clean up resources before object destruction.
Example in Java:
class Car {
Car() { // Constructor
System.out.println("Car created");
}
@Override
protected void finalize() { // Finalizer
System.out.println("Car destroyed");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar = null; // Suggesting the object for garbage collection
System.gc(); // Requesting garbage collection
}
}
Q] Overloading
Definition:
Overloading allows multiple methods or operators to have the same name
but behave differently based on the number or type of parameters.
Types of Overloading:
1. Function/Method Overloading: Methods with the same name but
different parameter signatures.
2. Operator Overloading (in C++): Extending the behavior of operators for
user-defined types.
Example in Java (Method Overloading):
class MathOperations {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println(math.add(3, 5)); // Calls the int
version
System.out.println(math.add(3.5, 5.5)); // Calls the
double version
}
}