MB

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

PillarMeaningExample
EncapsulationHiding internal details and exposing only necessary partsUsing private fields + public getters/setters
AbstractionHiding complex logic behind a simple interfaceUsing abstract classes or interfaces
InheritanceReusing code by creating new classes from existing ones (IS-A relationship)class Dog extends Animal
PolymorphismOne name, many forms depending on contextOverriding or overloading methods

5. Method Overriding vs Overloading

FeatureOverridingOverloading
DefinitionRedefining a method in a subclassDefining multiple methods with same name but different parameters
Annotation@Override required for safety and clarityNo annotation required
Class RelationRequires inheritanceCan happen in the same class
Polymorphism TypeRuntime polymorphismCompile-time polymorphism

6. Compile-Time vs Run-Time Polymorphism

TypeDefinitionExample
Compile-TimeMethod to call is known at compile time (overloading)add(int a, int b) vs add(double a, double b)
Run-TimeMethod to call is determined at runtime based on the object’s typeOverridden makeSound() in subclasses

7. Packages

  • Used to group related classes and avoid name conflicts.
  • Declaration example: package myapp.animals;

8. Abstract Classes vs Interfaces

FeatureAbstract ClassInterface
Can have fields?YesNo (only constants - public static final)
Can have methods?Yes – abstract and concreteYes – abstract by default (Java 8+: default & static methods allowed)
Use caseWhen classes share common behaviorWhen unrelated classes share a contract
Inheritanceextends (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