Exam 3 CS121
Exam Information
- Exam Type: Object-Oriented Programming (OOP)
- Format: 15 questions (multiple choice, matching, short coding)
- Date/Time: Friday 04/04/25
- Administration: In-person, no Respondus required
1. Classes and Objects
- Class:
- A blueprint for creating objects.
- Defines fields (attributes) and methods (behavior).
- Object:
- An instance of a class that holds actual values for the fields.
- Can invoke methods defined in the class.
2. Class Discovery
- When identifying a class in the real world:
- Fields (State): What properties does it have?
- Methods (Behavior): What actions can it perform?
- Framework:
- Noun = Class
- Adjectives = Fields
- Verbs = Methods
3. Access Modifiers
- public: Accessible from anywhere.
- private: Accessible only within the same class.
- protected: Accessible in the same package and subclasses.
- default: Accessible within the same package.
4. Four Pillars of OOP
Pillar | Meaning | Example |
---|---|---|
Encapsulation | Hiding internal details and exposing only necessary parts | Using private fields + public getters/setters |
Abstraction | Hiding complex logic behind a simple interface | Using abstract classes or interfaces |
Inheritance | Reusing code by creating new classes from existing ones (IS-A relationship) | class Dog extends Animal |
Polymorphism | One name, many forms depending on context | Overriding or overloading methods |
5. Method Overriding vs Overloading
Feature | Overriding | Overloading |
---|---|---|
Definition | Redefining a method in a subclass | Defining multiple methods with same name but different parameters |
Annotation | @Override required for safety and clarity | No annotation required |
Class Relation | Requires inheritance | Can happen in the same class |
Polymorphism Type | Runtime polymorphism | Compile-time polymorphism |
6. Compile-Time vs Run-Time Polymorphism
Type | Definition | Example |
---|---|---|
Compile-Time | Method to call is known at compile time (overloading) | add(int a, int b) vs add(double a, double b) |
Run-Time | Method to call is determined at runtime based on the object’s type | Overridden makeSound() in subclasses |
7. Packages
- Used to group related classes and avoid name conflicts.
- Declaration example:
package myapp.animals;
8. Abstract Classes vs Interfaces
Feature | Abstract Class | Interface |
---|---|---|
Can have fields? | Yes | No (only constants - public static final) |
Can have methods? | Yes – abstract and concrete | Yes – abstract by default (Java 8+: default & static methods allowed) |
Use case | When classes share common behavior | When unrelated classes share a contract |
Inheritance | extends (only one abstract class) | implements (can implement multiple) |
9. Review Checklist
- ✅ Be able to define and use classes/objects
- ✅ Know how to use fields and methods properly
- ✅ Understand access modifiers and packages
- ✅ Be able to explain and apply the four pillars of OOP
- ✅ Distinguish between overriding and overloading
- ✅ Understand runtime vs compile-time polymorphism
- ✅ Know how to use abstract classes and interfaces
- ✅ Practice discovering classes from real-world problems