Computer ScienceChapter 3: Programming Fundamentals
3.1.2 Object Oriented Programming
Object Oriented Programming (OOP)
Explanation level
Introduction
Object Oriented Programming (OOP) is a software development paradigm based on data and objects, which include functions (methods) that define the behavior and data (attributes) to indicate the properties of objects. These are the program's building blocks to regenerate the actual objects and their functionalities in code.
The textbook explains that programs designed using the OOP paradigm represent the physical attributes of the objects from the real world, and therefore developers have a more natural understanding of the problem to be solved by devising the program around objects and their actions. The main advantage of using the OOP paradigm is that the emphasis in problem-solving is on objects and not the logic. The program reflects real-world objects like a car, fruit, etc. This approach makes it easier to understand the problem, design the solution, and change the code if needed.
Definition
Object Oriented Programming (OOP) is a software development paradigm based on data and objects, which include functions (methods) that define the behavior and data (attributes) that indicate the properties of objects.
In simple words: OOP is a style of programming where you model real-world things (objects) in your code, giving each object its own data (attributes) and behaviors (methods).
Main Idea
The core idea of OOP is to model real-world objects in code. Instead of focusing on logic and procedures, OOP focuses on objects — what they are (attributes) and what they can do (methods).
The textbook explains that OOP is implemented through classes, which allow defining the object and their functionalities initially, and later physical instances (objects) are created when needed in the program. This is useful for complex systems where developers can map objects and their associations realistically and handle them more efficiently. OOP also limits direct access to data for safety and reliability of data and programs.
Core Concept
- Focus on objects, not logic: The emphasis in problem-solving is on objects rather than the logic.
- Real-world modeling: The program reflects real-world objects like a car, fruit, etc.
- Classes as templates: OOP is implemented through classes, which define the object and their functionalities.
- Instances created when needed: Physical instances (objects) are created on the spot when they need to be used in the program.
- Useful for complex systems: Developers can map objects and their associations to be realistic and handle them more efficiently.
- Data safety: OOP limits direct access to data for safety and reliability of data and programs.
Pros and Cons of OOP The textbook provides a table (Table 3.2) listing the pros and cons:
- Simplifies complex systems by breaking them into smaller reusable components.
- Hides implementation details, hence improving code maintainability and security.
- Establishes hierarchical relationships between classes.
- Objects of different types are treated as if they were of the same type.
- Represents real-world objects and their relationships effectively.
- May result in complex designs if not implemented thoughtfully.
- At times code is harder to understand and debug due to encapsulation.
- Rigid design due to inheritance may reduce flexibility.
- Polymorphism increases complexity, making code harder to understand and reason about.
- May not be well-suited for certain problems, such as computationally intensive tasks.
Key Concepts of OOP
1. Class A class is an object creation stencil or template. It specifies a collection of methods (functions) and attributes (variables or properties) that its objects will hold. You are creating an instance of a class when you create an object from it. Although each object has a distinct set of data (attribute values), they all use the same class-defined methods.
Example from the book (Table 3.2): The 'StationaryItem' class acts as the stencil for all stationary items in a bookstore. Individual goods developed by this session include pens, pencils, and notebooks. Each item has unique values for attributes such as name, price, and color. The methods of the class allow you to retrieve or change these attributes.
| Class: StationaryItem | | | |---|---|---| | Attributes: | Object 1: A blue XYZ pen | Object 2: A red ABC pencil | | name (e.g., "Pen", "Pencil", "Notebook") | name: "Pen" | name: "Pencil" | | price | price: 200 | price: 100 | | color (e.g., "Blue", "Black", "Red", "Green") | color: "Blue" | color: "Red" | | brand (e.g., "XYZ", "ABC", "DEF") | brand: "XYZ" | brand: "ABC" | | It can have methods like `get_price()` to obtain the price of the item, `set_price()` to adjust the price of the said item, etc. | | |
2. Object An object is an instance of a class that denotes a physical thing, such as a person, a car, or a bank account. Every object has distinct properties (attributes) and behaviors (methods). For instance, each home (object) constructed from a class that specifies the structure of a house has particular properties, such as size, color, or number of rooms. All objects have the same basic structure as defined by the class, apart from their own specific characteristics. Objects in a program interact with one another to imitate real-world situations and improve the organization and modularity of the code.
3. Encapsulation Encapsulation is the process of information hiding by combining data (attributes) and the methods that operate on the data into a single entity, i.e., a class. The main concept is to keep together the data and methods that are relevant. Encapsulation does not allow direct access to some components but via some designated methods provided by the object. This way, internal working of an object is hidden. Encapsulation protects data from unwanted access or changes and also improves code to be written in modules and easy to read and understand.
Example from the book: In a bank, the account holder's name, account number, and balance are critical information. Rather than changing the balance directly, `deposit(amount)` method will add the amount to the balance while `withdraw(amount)` method will subtract the said amount from the balance. Further, `get_balance()` method will retrieve current balance without changing the balance figures. This way, critical information is encapsulated and cannot be modified directly.
4. Abstraction Abstraction simplifies creating complicated scenarios by showing only the necessary information and usefulness. It provides only essential information about the data to others, hiding the background details or implementation.
Example from the book: Consider a vehicle, where we are mainly concerned with common operations such as start, stop, and accelerate. But the underlying mechanism of how the engine burns fuel and shifting of gears, etc., are kept hidden.
5. Inheritance Inheritance allows you to create new classes (subclasses or derived classes) from existing parent classes. Subclasses inherit the attributes and methods of their parent class, helping in reducing repetition of code. This idea promotes the efficient extension of existing structures.
Example from the book: In general terms, a 'Vehicle' may have attributes such as speed, color, wheels and operations like `accelerate()` and `brake()`. If we specify it further for a 'Car' which is a type of 'Vehicle', we do not need to define the above-stated attributes and operations again because 'Car' inherited these from 'Vehicle' being a derived/sub-class and automatically owns these properties and functions of 'Vehicle'. However, 'Car' may have its own additional properties like 'seatingCapacity' and operation like `openSunroof()`.
6. Polymorphism Polymorphism allows objects from various classes to be considered as instances of a common type, though the implementation of the common type differs among the various classes in their own unique way. This allows writing generic code that can deal with a wide variety of objects. Polymorphism makes it easier to modify object behavior.
Example from the book: The common type 'Shape' can be a triangle, rectangle, or circle. Though all three are shapes, they have their own distinct properties and operations like draw, resize, calculate-area, etc. Polymorphism is useful in such scenarios where a common operation applies for all but implementation differs based on the type of shape. In context of specific shapes, triangle needs base and height, rectangle uses length and width while circle requires radius to calculate the area. Instead of writing three separate specific calculate-area methods (which may increase the more shapes we take into consideration), via polymorphism we can send a simple instruction to different objects such as 'draw-yourself' and each object will perform the operation in its own prescribed way on the basis of type of shape.
Intermediate Level
The textbook shows that OOP has both advantages and disadvantages:
- Simplifies complex systems by breaking them into reusable components.
- Hides implementation details, improving maintainability and security.
- Establishes hierarchical relationships between classes.
- Treats objects of different types as the same type (polymorphism).
- Represents real-world objects and their relationships effectively.
- May result in complex designs if not implemented thoughtfully.
- Code can be harder to understand and debug due to encapsulation.
- Rigid design due to inheritance may reduce flexibility.
- Polymorphism increases complexity.
- May not be suitable for computationally intensive tasks.
- Class defines the template.
- Object is an instance of the class.
- Encapsulation hides data.
- Abstraction hides complexity.
- Inheritance reuses code.
- Polymorphism allows different classes to be treated as the same type.
Important Points
- - OOP: A paradigm based on data and objects, with functions (methods) that define behavior and data (attributes) that indicate properties.
- - Focus: On objects and their actions, not on logic.
- - Implemented through classes: Classes define objects and their functionalities.
- - Instances: Objects are created when needed in the program.
- - Pros:
- - Simplifies complex systems.
- - Hides implementation details.
- - Establishes hierarchical relationships.
- - Treats objects of different types as the same type.
- - Represents real-world objects effectively.
- - Cons:
- - May result in complex designs.
- - Harder to understand and debug due to encapsulation.
- - Rigid design due to inheritance.
- - Polymorphism increases complexity.
- - May not suit computationally intensive tasks.
- - Key Concepts:
- - Class: Template for creating objects.
- - Object: Instance of a class.
- - Encapsulation: Hiding data and combining it with methods.
- - Abstraction: Showing only necessary information.
- - Inheritance: Creating new classes from existing ones.
- - Polymorphism: Objects of different classes treated as a common type.
Remember
- - OOP = Object Oriented Programming.
- - Focus = Objects, not logic.
- - Class = Template; Object = Instance.
- - Encapsulation = Hiding data.
- - Abstraction = Hiding complexity.
- - Inheritance = Reusing parent class.
- - Polymorphism = Many forms.
- - Pros: Simplifies complex systems, hides details, represents real-world objects.
- - Cons: Complex designs, harder to debug, rigid inheritance, polymorphic complexity.
Common Mistakes
- 1. Confusing encapsulation with abstraction — Encapsulation hides data; abstraction hides complexity.
- 2. Confusing a class with an object — A class is a template; an object is an instance of that template.
- 3. Thinking inheritance always improves flexibility — The textbook notes that inheritance can lead to rigid design and reduce flexibility.
- 4. Assuming polymorphism is always simpler — Polymorphism increases complexity, making code harder to understand and reason about.
- 5. Thinking OOP is always better — The textbook lists cons, including complexity and suitability issues for computationally intensive tasks.
- 6. Forgetting that OOP limits direct access to data — This is intentional (for safety and reliability), but it can make debugging harder.
Quick Revision
- Object Oriented Programming (OOP): A software development paradigm based on data and objects, which include functions (methods) that define behavior and data (attributes) that indicate properties.
- Focus: On objects and their actions, not on logic.
- Real-World Modeling: The program reflects real-world objects like car, fruit, etc.
- Implementation: Through classes, which define objects and their functionalities; instances are created when needed.
- Pros: Simplifies complex systems, hides implementation details, establishes hierarchical relationships, treats objects of different types as the same type, represents real-world objects effectively.
- Cons: May result in complex designs, harder to understand and debug, rigid design due to inheritance, polymorphism increases complexity, may not suit computationally intensive tasks.
- Key Concepts:
- Class: Object creation template; specifies methods and attributes.
- Object: Instance of a class; denotes a physical thing.
- Encapsulation: Information hiding; combines data and methods into a single entity.
- Abstraction: Simplifies complex scenarios by showing only necessary information.
- Inheritance: Creates new classes from existing parent classes; reduces code repetition.
- Polymorphism: Objects of different classes treated as instances of a common type; allows generic code.
60-Second Revision
- **OOP** = Objects, not logic.
- Class = Template; Object = Instance.
- Encapsulation = Hiding data.
- Abstraction = Hiding complexity.
- Inheritance = Reusing parent class.
- Polymorphism = Many forms.
- Pros: Simplifies, hides details, models real world.
- Cons: Complex, harder to debug, rigid inheritance, polymorphic complexity.
- Warning: OOP is not always the best choice — consider the cons.
Frequently Asked Questions (FAQs)
Q1. What is Object Oriented Programming (OOP)? Answer: OOP is a software development paradigm based on data and objects, which include functions (methods) that define behavior and data (attributes) that indicate properties. Programs designed using OOP represent physical attributes of real-world objects, making it easier to understand the problem.
Q2. What is the main advantage of OOP? Answer: The main advantage of OOP is that the emphasis in problem-solving is on objects and not the logic. The program reflects real-world objects like a car, fruit, etc., making it easier to understand the problem, design the solution, and change the code if needed.
Q3. What is a class? Answer: A class is an object creation stencil or template. It specifies a collection of methods (functions) and attributes (variables or properties) that its objects will hold.
Q4. What is an object? Answer: An object is an instance of a class that denotes a physical thing, such as a person, a car, or a bank account. Every object has distinct properties (attributes) and behaviors (methods).
Q5. What is encapsulation? Answer: Encapsulation is the process of information hiding by combining data (attributes) and the methods that operate on the data into a single entity (a class). It does not allow direct access to some components but via designated methods provided by the object.
Q6. What is abstraction? Answer: Abstraction simplifies creating complicated scenarios by showing only the necessary information and usefulness. It provides only essential information about the data to others, hiding the background details or implementation.
Q7. What is inheritance? Answer: Inheritance allows you to create new classes (subclasses or derived classes) from existing parent classes. Subclasses inherit the attributes and methods of their parent class, helping in reducing repetition of code.
Q8. What is polymorphism? Answer: Polymorphism allows objects from various classes to be considered as instances of a common type, though the implementation of the common type differs among the various classes. This allows writing generic code that can deal with a wide variety of objects.
- Simplifies complex systems by breaking them into smaller reusable components.
- Hides implementation details, improving maintainability and security.
- Establishes hierarchical relationships between classes.
- Objects of different types are treated as if they were of the same type.
- Represents real-world objects and their relationships effectively.
- May result in complex designs if not implemented thoughtfully.
- At times code is harder to understand and debug due to encapsulation.
- Rigid design due to inheritance may reduce flexibility.
- Polymorphism increases complexity, making code harder to understand and reason about.
- May not be well-suited for certain problems, such as computationally intensive tasks.
Q11. What is the difference between encapsulation and abstraction? Answer: Encapsulation is about hiding data — preventing direct access to internal state. Abstraction is about hiding complexity — showing only what is necessary. Encapsulation protects data; abstraction simplifies the interface.
Q12. Give an example of inheritance. Answer: A 'Vehicle' class may have attributes like speed, color, wheels and operations like `accelerate()` and `brake()`. A 'Car' class can inherit these attributes and operations from 'Vehicle', and add its own properties like `seatingCapacity` and `openSunroof()`.
Q13. Give an example of polymorphism. Answer: The common type 'Shape' can be a triangle, rectangle, or circle. All three are shapes, but they have different implementations for `calculateArea()`. Polymorphism allows a single instruction like 'calculate area' to work differently for each shape.
Q14. Why is OOP useful for complex systems? Answer: OOP is useful for complex systems because it breaks them into smaller, reusable components (objects), making the code easier to understand, maintain, and modify. It also models real-world objects and their relationships, which makes the design more intuitive.
Q15. When is OOP not suitable? Answer: OOP may not be well-suited for computationally intensive tasks, where performance is critical. The overhead of objects and the complexity of OOP design can make it less efficient than other paradigms in such scenarios.
Practice Questions
Short Response Questions (SRQs)
- Define OOP and explain its main advantage.
- What is a class? How is it different from an object?
- Explain encapsulation with an example.
- Explain abstraction with an example.
- What is polymorphism? Give an example.
Extended Response Questions (ERQs)
- Discuss the OOP paradigm in detail. Explain its focus, real-world modeling, and implementation through classes.
- Compare and contrast the pros and cons of OOP. In what situations would you choose OOP over other paradigms?
- Explain the six key concepts of OOP (class, object, encapsulation, abstraction, inheritance, polymorphism) with examples.
- Analyze the relationship between encapsulation and abstraction. How do these concepts contribute to data safety and code maintainability?
- You are designing a software system for a library. Describe how you would use OOP concepts (class, object, inheritance, polymorphism) to model books, members, and borrowing transactions.
Multiple Choice Questions (MCQs)
- Q1. What is OOP based on?
- A. Functions and logic
- B. Data and objects
- C. Procedures and steps
- D. Variables and loops
- Correct Answer: B
- Explanation: OOP is based on data and objects.
- Q2. What is a class?
- A. An instance of an object
- B. A template for creating objects
- C. A function in functional programming
- D. A type of data structure
- Correct Answer: B
- Explanation: A class is a template for creating objects.
- Q3. What is an object?
- A. A template for creating classes
- B. An instance of a class
- C. A function in functional programming
- D. A type of data structure
- Correct Answer: B
- Explanation: An object is an instance of a class.
- Q4. Which OOP concept involves hiding data and combining it with methods?
- A. Abstraction
- B. Inheritance
- C. Encapsulation
- D. Polymorphism
- Correct Answer: C
- Explanation: Encapsulation involves hiding data and combining it with methods.
- Q5. Which OOP concept simplifies complex scenarios by showing only necessary information?
- A. Encapsulation
- B. Abstraction
- C. Inheritance
- D. Polymorphism
- Correct Answer: B
- Explanation: Abstraction simplifies complex scenarios by showing only necessary information.
- Q6. Which OOP concept allows a subclass to inherit attributes and methods from a parent class?
- A. Encapsulation
- B. Abstraction
- C. Inheritance
- D. Polymorphism
- Correct Answer: C
- Explanation: Inheritance allows a subclass to inherit attributes and methods from a parent class.
- Q7. Which OOP concept allows objects of different classes to be treated as instances of a common type?
- A. Encapsulation
- B. Abstraction
- C. Inheritance
- D. Polymorphism
- Correct Answer: D
- Explanation: Polymorphism allows objects of different classes to be treated as instances of a common type.
- Q8. What is one advantage of OOP?
- A. It always makes code shorter
- B. It simplifies complex systems by breaking them into reusable components
- C. It eliminates the need for functions
- D. It is always faster than other paradigms
- Correct Answer: B
- Explanation: OOP simplifies complex systems by breaking them into smaller reusable components.
- Q9. What is one disadvantage of OOP?
- A. It cannot model real-world objects
- B. It may result in complex designs if not implemented thoughtfully
- C. It does not support encapsulation
- D. It cannot use functions
- Correct Answer: B
- Explanation: OOP may result in complex designs if not implemented thoughtfully.
- Q10. In the bank example, which method is used to retrieve the current balance without changing it?
- A. `deposit(amount)`
- B. `withdraw(amount)`
- C. `get_balance()`
- D. `set_balance()`
- Correct Answer: C
- Explanation: The `get_balance()` method retrieves the current balance without changing it.
- Q11. Which OOP concept is illustrated by a 'Car' inheriting attributes from a 'Vehicle'?
- A. Encapsulation
- B. Abstraction
- C. Inheritance
- D. Polymorphism
- Correct Answer: C
- Explanation: A 'Car' inheriting from a 'Vehicle' illustrates inheritance.
- Q12. Which OOP concept is illustrated by different shapes having their own `calculateArea()` method?
- A. Encapsulation
- B. Abstraction
- C. Inheritance
- D. Polymorphism
- Correct Answer: D
- Explanation: Different shapes having their own `calculateArea()` method illustrates polymorphism.
- Q13. What does encapsulation protect?
- A. The hardware
- B. The data from unwanted access or changes
- C. The network
- D. The operating system
- Correct Answer: B
- Explanation: Encapsulation protects data from unwanted access or changes.
- Q14. What does abstraction hide?
- A. The data
- B. The background details or implementation
- C. The objects
- D. The classes
- Correct Answer: B
- Explanation: Abstraction hides the background details or implementation.
- Q15. Why may OOP not be suitable for computationally intensive tasks?
- A. Because it cannot use functions
- B. Because it may have overhead and complexity
- C. Because it does not support objects
- D. Because it cannot model real-world objects
- Correct Answer: B
- Explanation: OOP may not be suitable for computationally intensive tasks due to overhead and complexity.
Did you understand this topic?
Your answer is saved with your learning record. It does not mark the topic as mastered.
Quick revision
Revision Zone
Review the key facts, formulas and checks before you move on.3.1.2 Object Oriented Programming
Sticky Notes · Quick Revision
Key Point 1
- **OOP** = Objects, not logic.
Key Point 2
- Class = Template; Object = Instance.
Key Point 3
- Encapsulation = Hiding data.
Key Point 4
- Abstraction = Hiding complexity.
Key Point 5
- Inheritance = Reusing parent class.
Key Point 6
- Polymorphism = Many forms.
Key Point 7
- Pros: Simplifies, hides details, models real world.
Key Point 8
- Cons: Complex, harder to debug, rigid inheritance, polymorphic complexity.
Same supplied content as this Notes page (shared canonical source).