What is encapsulation?
Encapsulation combines an object’s state with the methods that operate on it while limiting direct access to its representation.
Study 8 Inheritance and Object-Oriented Design with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is encapsulation?
Encapsulation combines an object’s state with the methods that operate on it while limiting direct access to its representation.
What is an object invariant?
An invariant is a condition that should remain true, such as a bank account balance never becoming negative.
Which access level best protects internal state?
Use private fields for implementation details and state that outside code should not modify directly.
What does package-private access permit?
Package-private access allows use within the same package, but not by subclasses in another package or unrelated external classes.
When is protected access appropriate?
Use protected cautiously for a deliberate subclass extension point; it also permits access within the same package.
How do inheritance and composition differ?
Inheritance models an “is-a” relationship, while composition models a “has-a” relationship. A SalariedEmployee is an Employee; a Car has an Engine.
Why does a subclass constructor call super(name)?
The subclass constructor calls super(name) to initialize the superclass portion of the object.
What is method overriding?
Overriding supplies a subclass implementation with the same method signature and a compatible return type as an inherited instance method.
What does @Override verify?
@Override asks the compiler to verify that a method actually overrides an inherited method, catching misspellings and signature mismatches.
Which methods cannot participate in ordinary overriding?
A private method is not overridden because it is not inherited; a static method is hidden; and a final method cannot be overridden.
What is runtime polymorphism?
Polymorphism lets a general reference such as Employee refer to different specific objects while runtime dispatch selects each object’s overridden method.
How does polymorphism simplify employee processing?
A polymorphic loop can call employee.calculatePay() without testing each concrete employee class with if or instanceof.