Sunday, December 15, 2024

Chapter 5: Object-Oriented Programming (OOP)

5.1 Programming paradigms: procedural, structural and object oriented

A programming paradigm is a style or way of writing programs. It sets the rules and methods for organizing and structuring code to solve problems. Some common types of programming paradigms with real-world example are as follows:

1)    Procedural Programming:

A style or approach of programming where the code is organized into procedures or functions that perform specific tasks is known as Procedural Programming.

For example: Cooking a meal using a recipe, where each step is clearly defined and followed in a specific order.

2)    Structural Programming:

A subset of procedural programming that emphasizes dividing a program into small, manageable blocks or modules to improve organization and readability is known as Structural Programming.

For example: Building a house with a blueprint, where each part of the house is planned and constructed separately but contributes to the overall structure.

3)    Object-Oriented Programming (OOP)/Event-Driven (Object-Driven) Programming:

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which contain both data (attributes) and methods (functions). OOP promotes modularity, reusability, and scalability by organizing code into classes and objects. 

For example: Designing a car, where each part (like the engine, wheels, and seats) is an object with its own properties and functions, all working together to form the complete vehicle.

 

5.2 Features (Characteristics) of OOP: Class, Object, Polymorphism and Inheritance

Class:

A class is a blueprint or template for creating objects. It defines a set of properties (attributes) and methods (functions) that the objects created from the class will have. The class acts as a prototype from which individual objects are created, encapsulating data for the object and methods to manipulate that data.

For example:

Imagine you have a detailed blueprint for a car. This blueprint includes specifications like the car's make, model, colour, engine type, and the number of doors. It also outlines functions the car can perform, such as starting the engine, accelerating, and braking. The blueprint itself is not a car but a guide for manufacturing cars. All cars built using this blueprint will share these specifications and functionalities.

 

Object:

An object is an instance of a class. It is a specific realization of a class with actual values for the properties defined by the class. Objects can interact with one another through methods, making them the building blocks of an application. The object represents a real-world entity that combines both   attributes(data) and behaviours (methods/functions).

For example:

An object is a specific car manufactured using the blueprint. For example, a blue 2024 Tesla with a hybrid engine, sunroof, and leather seats is an object of the Car class. This specific car has actual values for the properties defined in the blueprint (class) and can perform the functions (methods) outlined by the blueprint. Each car (object) built from the blueprint can have different attributes (like colour and features) but shares the general design and capabilities defined by the class.

 

Encapsulation:

Encapsulation is a fundamental concept in Object-Oriented Programming (OOP) that involves wrapping(bundling) data (attributes) and methods (functions) into a single unit (class) and hides the internal details from the outside world.

Encapsulation plays a crucial role in data hiding by restricting direct access to the internal details of an object. In Object-Oriented Programming (OOP), encapsulation allows a class to control how its data is accessed and modified by:

1. Making Data Private: Class attributes (variables) can be declared as private so they cannot be accessed directly from outside the class.

2. Providing Controlled Access: Instead of allowing direct access to data, encapsulation provides getter and setter methods.

3. Hiding Internal Implementation Details: The internal workings of a class are hidden from external code, meaning users interact with the class through a controlled interface.

For example:

Think of encapsulation as the dashboard of a car. While we interact with the dashboard to control various functions (like speed, temperature, and fuel), we don’t need to know the inner workings of the engine or other systems. The complexities are hidden from us.


Polymorphism:

Polymorphism refers the ability of an object to take on different forms depending upon situations. It allows objects of different classes to be treated as objects of a common superclass. The same method(function) can behave differently on different classes. Different types of Polymorphism in OOP are as follows:

1) Compile-time Polymorphism (Static Polymorphism)

2) Runtime Polymorphism (Dynamic Polymorphism)

For example:

Consider a universal remote control that can be used to operate different devices. The “start” button might start the engine in a car, turn on the TV, or begin playback on a DVD player. The same button (method) works in different ways depending on the device (object).

 Advantages of Polymorphism:

a) Method Overriding: Subclasses can modify inherited methods for specific behaviour.

b) Method Overloading: Allows multiple functions with the same name but different parameters, improving readability.

c) Dynamic Binding (Runtime Polymorphism): Enables flexible and efficient function execution, supporting late binding.

d) Simplifies Code Maintenance: Reduces complexity by allowing generic function calls.


Abstraction:

Abstraction involves hiding the complex implementation details and showing only the essential features of the object.

For example:

Think of a car's gear shift. When we shift gears, we don’t need to understand the complexities of the transmission system. We just need to know how to use the gear shift to drive the car.


Inheritance: In Object-Oriented Programming (OOP), inheritance is a fundamental concept that allows a class (called a subclass or derived class) to inherit properties and behaviours (methods and attributes) from another class (called a superclass or base class).

For example:

Superclass = Animal (base class)

Subclass = Mammal, Bird, Fish (derived classes)

Inheritance = Mammals, Birds, and Fish inherit common features (like eat()) from the Animal class, but they also add their own specific features (like nurse() for Mammals, fly() for Birds, swim() for Fish).

Different types of inheritance in Object-Oriented Programming (OOP) are as follows:

1)     Single Inheritance:

A subclass inherits from one superclass.

Example: Dog inherits from Animal.

2)     Multiple Inheritance:

A subclass inherits from more than one superclass.

Example: Dog inherits from Mammal and Canine.

3)     Multilevel Inheritance:

A subclass inherits from another subclass, forming a chain.

Example: Puppy inherits from Dog, and Dog inherits from Animal.

4)     Hierarchical Inheritance:

Multiple subclasses inherit from a single superclass.

Example: Dog and Cat both inherit from Animal.

 Advantages of Inheritance:

a) Code Reusability: Common functionalities are defined in a base class and reused in derived classes.

b) Faster Development: New classes can inherit existing logic instead of writing from scratch.

c) Scalability: Easy to extend existing code with additional features.

d) Better Organization: Establishes a clear relationship between general and specific classes.


5.3 Advantages of OOP

1) Modularity:

OOP allows you to divide your program into smaller, self-contained units (classes), making it easier to manage and debug.

2) Code Reusability:

Through inheritance, OOP promotes code reuse. Once a class is written, it can be reused by other classes without modification.

3)Maintainability:

OOP's modular structure makes it easier to update and maintain. Changes in one class can be made without affecting others.

4)Abstraction:

OOP helps hide complex implementation details and exposes only essential features, making it easier to understand and use objects.

5)Encapsulation:

OOP allows data and methods to be bundled together in classes, protecting data from unintended access or modification, leading to better data integrity.

6)Flexibility & Scalability:

OOP supports polymorphism and inheritance, allowing the system to evolve easily by adding new classes or modifying existing ones.

7)Security:

OOP supports data hiding through access modifiers (private, public, protected), which helps in controlling how data is accessed and modified, providing an additional layer of security.

 Some disadvantages of OOP are as follows:

1.     OOP can be harder to learn and understand, especially for beginners, due to its advanced    concepts.

2.     Programs written in OOP can be slower and use more memory compared to procedural programming.

3.     OOP often results in larger codebases because of the multiple classes and objects.

4.     Designing OOP-based applications can take more time due to the need for careful planning and structuring.

5.     For simple or small programs, OOP might be overkill, making procedural programming a more practical choice.


5.4 Application of OOP

Object-Oriented Programming (OOP) is widely used across many domains and applications due to its advantages like modularity, reusability, and ease of maintenance. Here are some common applications of OOP:

1. Graphical User Interface (GUI) Applications

Examples: Java Swing, C# Windows Forms.

2. Video Games

Examples: Unity (C#), Unreal Engine (C++).

3. Web Development

Backend Examples: Laravel (PHP).

Frontend Examples: React (JavaScript).

4. Enterprise Applications

Examples: ERP (Enterprise Resource Planning), CRM (Customer Relationship Management) systems.

5. Simulation Systems

Examples: Traffic simulation, flight simulation software.

6. Artificial Intelligence & Machine Learning

Examples: TensorFlow, scikit-learn (Python).

7. Operating Systems

Examples: Windows (parts using C++), macOS (Objective-C, Swift).

8. Embedded Systems

Examples: Robotics software, IoT applications.

9. Cloud Computing

Examples: AWS, Microsoft Azure.

10. Mobile Applications

Examples: Android (Java/Kotlin), iOS (Swift/Objective-C).


Imp Question) Compare the OOPs and procedural programming language.

        Aspect

OOP (Object-Oriented Programming)

Procedural Programming (Structural Programming)

1. Paradigm

Focuses on objects and classes.

Focuses on functions and procedures.

2. Structure

Organized around objects that encapsulate data and behaviour.

Organized around a sequence of tasks or steps.

3. Data Handling

Emphasizes data encapsulation and security.

Data is often exposed and shared globally.

4. Code Reusability

Achieved through inheritance and polymorphism.

Reusability is limited to function calls.

5. Modularity

Programs are divided into objects and classes.

Programs are divided into functions.

6. Scalability

Better suited for large and complex systems.

Less suitable for complex, large systems.

7. Development Approach

Follows a bottom-up approach, building reusable objects first.

Follows a top-down approach, breaking the task into smaller procedures.

8. Examples of Usage

Used for GUI, simulations, and real-world modeling.

Used for smaller, process-driven tasks.

9. Examples of Languages

Java, C++, Python , C#.

C, Pascal, Fortran, Basic.

10. Polymorphism and Inheritance

Supports both for flexibility and code reuse.

Not supported natively.

11. Debugging

Easier to debug and maintain due to modularity.

Harder to debug in large systems.

 


MULTIPLE CHOICE QUESTIONS:

(ASMITA'S Question Bank)

1. Which OOP feature allows a class to inherit properties and behaviour from another class?

a. Inheritance

b. Encapsulation

c. Polymorphism

d. Abstraction
Answer: a. Inheritance



2. Which concept allows you to reuse the written code in OOPs?

a. Encapsulation

b. Inheritance

c. Polymorphism

d. Class
Answer: b. Inheritance



3. Which of the following concepts in object-oriented programming refers to binding data and function into a single unit?

a. Encapsulation

b. Abstraction

c. Polymorphism

d. Inheritance
Answer: a. Encapsulation



4. Which of the following is not an OOPs concept?

a. Class

b. Exception

c. Polymorphism

d. Encapsulation
Answer: b. Exception



5. What is called in a class to wrapping/hiding of data into a single unit?

a. Abstraction

b. Polymorphism

c. Encapsulation

d. Inheritance
Answer: c. Encapsulation



6. Which feature of OOP is illustrated by code reusability?

a. Polymorphism

b. Abstraction

c. Encapsulation

d. Inheritance
Answer: d. Inheritance



7. OOP stands for...

a. Operator overloading programming

b. Object-Oriented Programming

c. Object overloading programming

d. None of the above
Answer: b. Object-Oriented Programming



8. ...is a concept or thing with defined boundaries that are relevant to the problem we are dealing with.

a. Object

b. Class

c. Variable

d. None of the above
Answer: a. Object

9. ___ is a collection or group of similar objects that have the same properties, common behavior, and relationships.
a. object
b. class
c. constant
d. None of the above
Answer: b. class


10. ___ is a property that allows the reuse of an existing class to build a new class.
a. class
b. operation
c. inheritance
d. object
Answer: c. inheritance


11. ___ is the process that allows selective hiding of data and functions in a class.
a. class
b. inheritance
c. encapsulation
d. polymorphism
Answer: c. encapsulation


(BUDDHA PUBLICATION)

1. Which of the following languages was developed as the first purely object-oriented programming language?
a. SmallTalk
b. C++
c. Kotlin
d. Java
Answer: a. SmallTalk


2. Who developed object-oriented programming?
a. Adele Goldberg
b. Dennis Ritchie
c. Alan Kay
d. Andrea Ferro
Answer: c. Alan Kay


3. Which of the following is not an OOPS concept?
a. Encapsulation
b. Exception
c. Polymorphism
d. Inheritance
Answer: b. Exception


4. Which feature of OOPS describes the reusability of code?
a. Abstraction
b. Encapsulation
c. Polymorphism
d. Inheritance
Answer: d. Inheritance


5. Which of the following OOP concepts binds the code and data together and keeps them secure from the outside world?
a. Encapsulation
b. Inheritance
c. Abstraction
d. Polymorphism
Answer: a. Encapsulation


6. Which among the following features is not in the general definition of OOPS?
a. Many forms
b. Efficient code
c. Code reusability
d. Duplicate data
Answer: d. Duplicate data


7. Which feature of OOPS derives the class from another class?
a. Inheritance
b. Data hiding
c. Encapsulation
d. Polymorphism
Answer: a. Inheritance


8. In OOPS, public, private, and protected are?
a. Classes
b. Interfaces
c. Access Modifiers
d. Method signatures
Answer: c. Access Modifiers


9. Which two features of object-oriented programming are the same?
a. Abstraction and Polymorphism features are the same
b. Inheritance and Encapsulation features are the same
c. Encapsulation and Polymorphism features are the same
d. Encapsulation and Abstraction features are the same
Answer: d. Encapsulation and Abstraction


(KRITI PUBLICATION)

Which of the following best defines a class?

a. Parent of an object

b. Instance of an object

c. Blueprint of an object

d. Scope of an object

Answer: c. Blueprint of an object

What is the additional feature in classes that was not in structures?

a. Data members

b. Member functions

c. Static data allowed

d. Public access specifier

Answer: b. Member functions

Which is not a feature of OOP in general definitions?

a. Code reusability

b. Modularity

c. Duplicate/Redundant data

d. Efficient Code

Answer: c. Duplicate/Redundant data

Which Feature of OOP illustrates code reusability?

a. Polymorphism

b. Abstraction

c. Encapsulation

d. Inheritance

Answer: d. Inheritance

How many classes can be defined in a single program?

a. Only 1

b. Only 100

c. Only 999

d. As many as you want

Answer: d. As many as you want

Which of the two features match each other?

a. Inheritance and Encapsulation

b. Encapsulation and Polymorphism

c. Encapsulation and Abstraction

d. Abstraction and Polymorphism

Answer: c. Encapsulation and Abstraction

Which among the following best describes polymorphism?

a. It is the ability for a message/data to be processed in more than one form

b. It is the ability for a message/data to be processed in only 1 form

c. It is the ability for many messages/data to be processed in one way

d. It is the ability for undefined message/data to be processed in at least one way

Answer: a. It is the ability for a message/data to be processed in more than one form

If the same message is passed to objects of several different classes and all of those can respond in a different way, what is this feature called?

a. Inheritance

b. Overloading

c. Polymorphism

d. Overriding

Answer: c. Polymorphism

Which among the following best describes encapsulation?

a. It is a way of combining various data members into a single unit

b. It is a way of combining various member functions into a single unit

c. It is a way of combining various data members and member functions into a single unit which can operate on any data

d. It is a way of combining various data members and member functions that operate on those data members into a single unit

Answer: d. It is a way of combining various data members and member functions that operate on those data members into a single unit

Which feature can be implemented using encapsulation?

a. Inheritance

b. Abstraction

c. Polymorphism

d. Overloading

Answer: b. Abstraction

Using encapsulation, data security is ___________

a. Not ensured

b. Ensured to some extent

c. Purely ensured

d. Very low

Answer: b. Ensured to some extent

Which definition best describes an object?

a. Instance of a class

b. Instance of itself

c. Child of a class

d. Overview of a class

Answer: a. Instance of a class

Which among the following best defines abstraction?

a. Hiding the implementation

b. Showing the important data

c. Hiding the important data

d. Hiding the implementation and showing only the features

Answer: d. Hiding the implementation and showing only the features

Class is _________ abstraction.

a. Object

b. Logical

c. Real

d. Hypothetical

Answer: b. Logical

Which feature of OOP indicates code reusability?

a. Encapsulation

b. Inheritance

c. Abstraction

d. Polymorphism

Answer: b. Inheritance

Which among the following defines single level inheritance?

a. One base class derives another class

b. One derived class inherits from one base class

c. One base class inherits from one derived class

d. One derived class derives from another derived class

Answer: b. One derived class inherits from one base class

Which among the following best defines multilevel inheritance?

a. A class derived from another derived class

b. Classes being derived from other derived classes

c. Continuing single level inheritance

d. Class which have more than one parent

Answer: a. A class derived from another derived class



(OCEAN PUBLICATION)

Who developed object-oriented programming?

i. Adele Goldberg

ii. Dennis Ritchie

iii. Alan Kay

iv. Andrea Ferro

Answer: iii. Alan Kay

Which of the following is not an OOPS concept?

i. Encapsulation

ii. Exception

iii. Polymorphism

iv. Abstraction

Answer: ii. Exception

Which feature of OOPS describes the reusability of code?

i. Abstraction

ii. Encapsulation

iii. Polymorphism

iv. Inheritance

Answer: iv. Inheritance

Which of the following definitions is incorrect for polymorphism?

i. Polymorphism helps in redefining the same functionality

ii. Polymorphism concept is the feature of object-oriented programming (OOP)

iii. It always increases the overhead of function definition

iv. Ease in the readability of the program

Answer: iii. It always increases the overhead of function definition

Which of the following OOP concepts binds the code and data together and keeps them secure from the outside world?

i. Polymorphism

ii. Inheritance

iii. Abstraction

iv. Encapsulation

Answer: iv. Encapsulation

Which of the following is shared structure of a set of similar objects?

i. Class

ii. Encapsulation

iii. Inheritance

iv. None

Answer: i. Class


(MODERN PUBLICATION)

1. Which of the following is not an Object-oriented programming language?

a. C
b. C++
c. PHP
d. Java
Answer: a. C

2. Which of the following is not an OOP feature?

a. Encapsulation
b. Polymorphism
c. Exception
d. Abstraction
Answer: c. Exception

3. The feature of OOP ............ describes the reusability of code.

a. Abstraction
b. Encapsulation
c. Polymorphism
d. Inheritance
Answer: d. Inheritance


4. Which feature of OOPs derives the class from another class?

a. Data hiding
b. Encapsulation
c. Inheritance
d. Polymorphism
Answer: c. Inheritance


5. Which of the following definition is incorrect for polymorphism?

a. Polymorphism helps in redefining the same functionality
b. Polymorphism concept is the feature of object-oriented programming (OOP)
c. It always increases the overhead of function definition
d. Ease in the readability of the program
Answer: c. It always increases the overhead of function definition


6. Which of the following OOP concepts binds the code and data together and keeps them secure from the outside world?

a. Polymorphism
b. Inheritance
c. Abstraction
d. Encapsulation
Answer: d. Encapsulation

7. Which of the following is not a type of inheritance?

a. Hybrid
b. Multilevel
c. Multiple
d. Tree
Answer: d. Tree

8. In which of the following programming, the emphasis is given on data rather than on procedures?

a. Object-oriented programming
b. Procedural programming
c. Structural programming
d. All of the above
Answer: a. Object-oriented programming


9. Which of the following is not a feature of procedural programming?

a. Emphasizes on algorithms
b. Uses top-down approach
c. Uses bottom-up approach
d. Large programs are divided into smaller programs
Answer: c. Uses bottom-up approach

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home