2. Object Orientation Flashcards
What is good encapsulation?
private instance variables with public get and set (accessor) methods
Can a subtype of an argument be passed?
Yes
If car extends vehicle, what is the relationship?
Car IS-A vehicle
If Car has a clutch variable, what is the relationship?
Car HAS-A clutch
What can a reference variable refer to?
An object of the same type or an object of the subtype of the declared type ie Animal a1 = new horse();
Can a reference variable be declared as a class type of interface type?
Both
If a reference variable is referred to as an interface type, what objects can it reference?
Any objects of any class which implements the interface (it passes the IS-A relation test)
What are the two ways to pass the IS-A relationship test?
If a class extends another class If a class implements another class
What does the compiler do at runtime with regard to object methods? For example:
Gamer p = new Player(); p.domethod();
The domethod() of Player object will be called at runtime rather than the Gamer version (This only applies to instance methods, not static methods and not variables)
What will happen if a static method is called from an object declaration?
It will use the reference to get the method, and not the object method.
What will happen is an variable is called from an object declaration?
It will get the reference variable, not the object variable.
Which are called by the object and not by the reference during run time?
variables
methods
static methods?
methods only
When can methods not be overriden?
When they are marked final
Do you have to implement an abstract method?
The first concrete class must implement all parent abstract methods
Can a method be invoked on the object side at runtime if it does not already exist and have been overridden on the reference side.
No, a reference version must exist for compilation to succeed.
Can you override a method with a more restrictive access modifier?
No
Can a overridden method have a different argument list?
No, that would cause it to be a overloaded method.
What are the rules for the return type of an overridden method?
They must be the same, or subtype of, the overridden return type.
Can the access level of an overridden method be less restrictive?
Yes
Can overriding methods throw new checked exceptions?
No
Can overriding methods throw new unchecked exceptions?
Yes
Can you override methods that are marked static or final?
No
How do you invoke the superclass method within the base class?
super.supermethod();
Can you use super.super.supermethod() to invoke the grandparent class?
No.
What must you make sure when overriding and calling a method that the original method has a checked exception?
That even if the base method isn’t being called, that the checked exception is still caught by the overridden method
What must a method to be an overloaded method?
It must change the argument list
Can overloaded methods change the return type?
Yes
Can overloaded methods change the access modifier?
Yes
Can overloaded methods declare new or broader exceptions?
Yes
public void doStuff(int y, String s){}
public void doStuff(int y, long p){}
Is the second method an override or an overload?
It’s an overload, the argument list has changed
When is the choice of which overloaded method decided?
At compile time - very nb
All about the reference, not the object
Can main() be overloaded?
Yes, but the only special one is the one that is not overloaded