STEM · Computer Science · Foundational Concepts/Principles
SaveObject-Oriented Programming
Significance: OOP organizes code using objects (data + methods), classes (templates), inheritance (specialization), polymorphism (same interface, different implementations), encapsulation (information hiding). Enables code reuse, modularity, and modeling real-world entities. Most modern languages support OOP (Java, C++, Python, C#).
Questions to think through
- explain
Explain polymorphism: Define Shape interface with `area()` method. Implement Circle, Rectangle classes. Function `totalArea(shapes[])` calls `area()` on each shape—different implementations execute based on object type (dynamic dispatch). This is polymorphism. Explain why this is powerful: add Triangle class without modifying totalArea. Compare with procedural approach: switch statement on shape type (requires modification for new types).
- explain
Analyze inheritance: Design class hierarchy: Animal > Mammal > Dog, Cat. Dogs and cats inherit from Mammal (warm-blooded, fur) which inherits from Animal (move, eat). When is inheritance appropriate versus composition? Consider: Duck class—should it inherit from Bird? But some birds don't fly. Composition: Duck has-a FlyingBehavior object. Explain "favor composition over inheritance" principle and when each is appropriate.
