2 Object Orientation Flashcards
How do you use encapsulation in your code?
Keep instance variables hidden (often with private). If necessary provide public accessor methods according to the naming convention.
What are the fancy names for getters and setters?
accessors and mutators
What are the advantages of providing ‘empty’ getters and setters?
Future maintainability: You can add logic to them later without changing the public API of the class
Every Java class is a subclass of …
Object
Mention some methods which every Java class inherits from Object.
equals, hashCode, toString, clone, notify, wait
How can you create inheritance relationships in Java?
extending a class; implementing an interface (since Java 8)
Which elements of a class can be inherited and which not?
instance variables, static (class) variables, methods: abstract, instance, static; NOT: constructors, initialization blocks
Which elements of an interface can be inherited?
constants = static final variables, methods: abstract, default, static
What determines which methods of an object you can call?
the declared type. And NOT the runtime type.
Which operator checks for an ‘IS-A’ relationship?
instanceof
What is a polymorphic object?
An object which passes instanceof for multiple types. Thus any object which is not declared as Object.
What can change and what can not with a reference variable?
not changeable: its type; changeable: the object it refers to (exception: final variables)
What does polymorphic method invocation mean?
JVM knows the real type of the executing object and executes the overridden version of an instance method, if there is one. Scope: only instance methods. No static methods, no variables.
Which methods can be overridden?
Every inherited method except the final
ones.
What about the access level of overriding methods?
It can be the same or less restrictive than the superclass’ method.