Evaluating the Fundamentals of OOP
Modeling Real-World Entities with OOP
Classes and Objects
- Class: A blueprint for creating objects, defining their attributes (instance variables) and behaviors (methods).
- Object: An instance of a class, representing a specific entity with unique attribute values.
- Consider a video game with non-player characters (NPCs).
- Each NPC is an object created from a class that defines its movement and interactions.
Inheritance
- Inheritance allows a class ( subclass) to inherit attributes and methods from another class ( superclass).
- Promotes code reuse and efficiency by extending existing functionality.
- A Vehicle superclass might have attributes like owner and size.
- Subclasses like Car and Airplane add specific attributes like fuel type or maximum range.
Encapsulation
- Encapsulation bundles data and methods within a class, restricting direct access from outside.
- Ensures data integrity and security by exposing only necessary interfaces.
Encapsulation is achieved by using access modifiers like private and public to control visibility.
Polymorphism
- Polymorphism allows methods to have different implementations based on the context.
- Enables flexibility and extensibility in code design.
- Think of the word "write."
- In a language class, it might mean crafting a story.
- In science, it could mean documenting a lab report.
- The action varies based on the context.
Advantages of OOP
- Code Reusability
- Inheritance and polymorphism allow developers to reuse and extend existing code.
- Exam Tip: Be able to give an example, e.g., a Car class inherited by ElectricCar.