Computer ScienceChapter 3: Programming Fundamentals
3.1.2 Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a software development paradigm based on data and objects, which include functions (methods) that defin
Explanation level
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 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.
- Emphasis in problem-solving is on objects, not logic.
- The program reflects real-world objects like car, fruit, etc.
- OOP is implemented through classes which define the object and their functionalities.
- Physical instances (objects) are created when needed in the program.
- OOP limits direct access to data for safety and reliability.
- 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 The textbook explains four 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.
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).
Example from the book: The 'StationaryItem' class acts as the stencil for all stationary items in the bookstore. Individual goods developed by this class 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.
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.
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 the 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. We are mainly concerned with common operations such as start, stop, and accelerate. But the underlying mechanism of how the engine burns fuel and the 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.
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.
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 to all but implementation differs based on the type of shape. Instead of writing three separate specific calculate-area methods, 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.
Intermediate Level
The two paradigms differ in their approach:
| Aspect | Functional Programming | Object-Oriented Programming |
|---|---|---|
| Focus | Functions and what to compute | Objects and their interactions |
| Structure | Code divided into functions | Code divided into classes and objects |
| Data | Data is separate from functions | Data and methods are combined in objects |
| Real-world modeling | Less emphasis on real-world entities | Directly models real-world entities |
| Best for | Small applications, mathematical computations | Complex systems, large applications |
| Testing | Functions are easy to test individually | Objects can be tested in isolation |
The textbook notes that functional programming is often preferred for small applications, while OOP is better for complex systems where real-world modeling is important.
Important Points
- - Programming Paradigm: A particular style or methodology for structuring code, managing program flow, and organizing data.
- - Two Main Paradigms:
- 1. Functional Programming: Divides code into functions; focuses on what to compute.
- 2. Object-Oriented Programming (OOP): Based on objects and classes; models real-world entities.
- - Functional Programming:
- - Pros: Simpler code, easier to debug, supports parallel programming.
- - Cons: Not easy for beginners, overhead with large data structures, may be less efficient for performance-critical tasks.
- - OOP:
- - Pros: Simplifies complex systems, improves maintainability and security, models real-world objects.
- - Cons: Can result in complex designs, harder to debug due to encapsulation, inheritance can reduce flexibility.
- - Key OOP Concepts: Class, Object, Encapsulation, Abstraction, Inheritance, Polymorphism.
- - The choice of paradigm depends on the problem and the context.
Remember
- - Programming Paradigm = Style/methodology of programming.
- - 2 Main Paradigms: Functional Programming, Object-Oriented Programming.
- - Functional Programming = Functions, what to compute.
- - OOP = Objects/classes, models real-world entities.
- - OOP Concepts: Class, Object, Encapsulation, Abstraction, Inheritance, Polymorphism.
- - Class = Template; Object = Instance.
- - Encapsulation = Hiding data; Abstraction = Showing only essentials.
- - Inheritance = Reusing parent class; Polymorphism = Many forms.
- - No paradigm is perfect for all situations.
Common Mistakes
- 1. Thinking one paradigm is always better — Each paradigm has strengths and weaknesses; the choice depends on the problem.
- 2. Confusing encapsulation with abstraction — Encapsulation hides data; abstraction hides complexity and shows only essentials.
- 3. Confusing inheritance with polymorphism — Inheritance reuses code from a parent class; polymorphism allows different classes to be treated as the same type.
- 4. Thinking functional programming is only for small apps — Functional programming can also be used for large applications, though the textbook notes it is often preferred for small ones.
- 5. Believing OOP is always easier — OOP can become complex if not implemented thoughtfully.
- 6. Ignoring the cons of OOP — The textbook lists several cons, including complexity, harder debugging, and reduced flexibility.
Quick Revision
- Programming Paradigm: A particular style or methodology of programming that defines how code is structured and how data is organized.
- Functional Programming: Divides code into functions based on functionality. Focuses on what to achieve. Pros: simpler code, easier to debug, supports parallel programming. Cons: not easy for beginners, overhead with large data structures, may be less efficient.
- Object-Oriented Programming (OOP): Based on data and objects. Programs represent real-world objects. Pros: simplifies complex systems, improves maintainability and security, models real-world objects. Cons: can result in complex designs, harder to debug due to encapsulation, inheritance can reduce flexibility.
- Key OOP Concepts:
- Class: Template for creating objects.
- Object: Instance of a class.
- Encapsulation: Combining data and methods, hiding internal details.
- Abstraction: Showing only essential information.
- Inheritance: Creating new classes from existing ones, reusing code.
- Polymorphism: Objects of different classes treated as instances of a common type.
60-Second Revision
- Programming Paradigm = Style of programming.
- 2 Main Paradigms: Functional, OOP.
- Functional = Functions, what to compute.
- OOP = Objects/classes, real-world modeling.
- OOP Concepts: Class, Object, Encapsulation, Abstraction, Inheritance, Polymorphism.
- Class = Template; Object = Instance.
- Encapsulation = Hiding data.
- Abstraction = Showing essentials.
- Inheritance = Reusing parent class.
- Polymorphism = Many forms.
- Warning: No paradigm is perfect — choose based on the problem.
Frequently Asked Questions (FAQs)
Q1. What is a programming paradigm? Answer: A programming paradigm describes a vital process for creating and organizing computer program code. It helps in the structure of code, program flow, and managing of data for programmers. In other words, it is a particular methodology to use a programming language for problem-solving.
- Functional Programming: Divides code into functions based on functionality, focusing on what to achieve.
- Object-Oriented Programming (OOP): Based on data and objects, modeling real-world entities.
Q3. What is functional programming? Answer: Functional programming is a paradigm where code is divided into functions based on functionality. Developers focus more on what is to be achieved rather than how to address the problem. Each step of the solution is mapped to a group of functions.
Q4. What are the pros and cons of functional programming? Answer: Pros: Simpler code, easier to track logic and debug, functions are easier to test and run, naturally supports parallel and concurrent programming, more concise code. Cons: Not easy for beginners, may involve overhead with large data structures, complex algorithms may be harder to express, requires careful handling of data sharing and synchronization, may be less efficient in performance-critical scenarios.
Q5. 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 represent physical attributes of real-world objects, making it easier to understand the problem.
Q6. What are the pros and cons of OOP? Answer: Pros: Simplifies complex systems by breaking them into reusable components, hides implementation details (improving maintainability and security), establishes hierarchical relationships, objects of different types treated 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 be suitable for computationally intensive tasks.
Q7. 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.
Q8. 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).
Q9. 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.
Q10. 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. For example, a vehicle's common operations (start, stop, accelerate) are shown, while the underlying mechanism (how the engine burns fuel) is hidden.
Q11. 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. For example, a 'Car' class can inherit from a 'Vehicle' class.
Q12. 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. For example, different shapes (triangle, rectangle, circle) can all have a `calculateArea()` method, but each implements it differently.
Practice Questions
Short Response Questions (SRQs)
- Define programming paradigm and explain its importance.
- What is functional programming? Give an example.
- What is object-oriented programming (OOP)? Give an example.
- Explain the difference between encapsulation and abstraction.
- What is inheritance? Give an example.
Extended Response Questions (ERQs)
- Discuss the two main programming paradigms (functional and OOP) in detail. Compare their approaches to problem-solving.
- Explain the key concepts of OOP (class, object, encapsulation, abstraction, inheritance, polymorphism) with examples.
- Analyze the pros and cons of functional programming and OOP. In what situations would you choose one over the other?
- How does OOP model real-world objects? Provide examples of how classes, objects, and inheritance represent real-world entities.
- Compare and contrast encapsulation and abstraction. How do these concepts contribute to the security and maintainability of code?
Multiple Choice Questions (MCQs)
- Q1. What is a programming paradigm?
- A. A particular style or methodology of programming
- B. A type of computer hardware
- C. A programming language
- D. A software application
- Correct Answer: A
- Explanation: A programming paradigm is a particular style or methodology of programming.
- Q2. Which paradigm focuses on dividing code into functions based on functionality?
- A. Object-Oriented Programming
- B. Functional Programming
- C. Procedural Programming
- D. Structural Programming
- Correct Answer: B
- Explanation: Functional programming divides code into functions based on functionality.
- Q3. Which paradigm is based on data and objects that model real-world entities?
- A. Functional Programming
- B. Object-Oriented Programming
- C. Procedural Programming
- D. Structural Programming
- Correct Answer: B
- Explanation: Object-Oriented Programming is based on data and objects that model real-world entities.
- Q4. 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.
- Q5. 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.
- Q6. 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.
- Q7. 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.
- Q8. 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.
- Q9. What is one advantage of functional programming?
- A. It models real-world objects
- B. It supports parallel and concurrent programming
- C. It uses classes and objects
- D. It hides implementation details
- Correct Answer: B
- Explanation: Functional programming naturally supports parallel and concurrent programming.
- Q10. 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 cannot use functions
- D. It does not support encapsulation
- Correct Answer: B
- Explanation: OOP may result in complex designs if not implemented thoughtfully.
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 (OOP)
Sticky Notes · Quick Revision
Key Point 1
- Programming Paradigm = Style of programming.
Key Point 2
- 2 Main Paradigms: Functional, OOP.
Key Point 3
- Functional = Functions, what to compute.
Key Point 4
- OOP = Objects/classes, real-world modeling.
Key Point 5
- OOP Concepts: Class, Object, Encapsulation, Abstraction, Inheritance, Polymorphism.
Key Point 6
- Class = Template; Object = Instance.
Key Point 7
- Encapsulation = Hiding data.
Key Point 8
- Abstraction = Showing essentials.
Key Point 9
- Inheritance = Reusing parent class.
Key Point 10
- Polymorphism = Many forms.
Key Point 11
- No paradigm is perfect.
Same supplied content as this Notes page (shared canonical source).