0% found this document useful (0 votes)
4 views7 pages

pcpf chp2

The document discusses key concepts of object-oriented programming (OOP) including encapsulation, abstraction, inheritance, polymorphism, dynamic binding, initialization, finalization, and overloading. It explains encapsulation as bundling data and methods within a class for data hiding and modularity, while abstraction simplifies complex systems by focusing on essential features. The document provides examples in Java to illustrate these concepts and highlights the differences between encapsulation and abstraction.

Uploaded by

Arfia Ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

pcpf chp2

The document discusses key concepts of object-oriented programming (OOP) including encapsulation, abstraction, inheritance, polymorphism, dynamic binding, initialization, finalization, and overloading. It explains encapsulation as bundling data and methods within a class for data hiding and modularity, while abstraction simplifies complex systems by focusing on essential features. The document provides examples in Java to illustrate these concepts and highlights the differences between encapsulation and abstraction.

Uploaded by

Arfia Ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy