2. Object Orientation Flashcards

1
Q

What is good encapsulation?

A

private instance variables with public get and set (accessor) methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can a subtype of an argument be passed?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

If car extends vehicle, what is the relationship?

A

Car IS-A vehicle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If Car has a clutch variable, what is the relationship?

A

Car HAS-A clutch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What can a reference variable refer to?

A

An object of the same type or an object of the subtype of the declared type ie Animal a1 = new horse();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can a reference variable be declared as a class type of interface type?

A

Both

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If a reference variable is referred to as an interface type, what objects can it reference?

A

Any objects of any class which implements the interface (it passes the IS-A relation test)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the two ways to pass the IS-A relationship test?

A
If a class extends another class
If a class implements another class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the compiler do at runtime with regard to object methods? For example:

Gamer p = new Player();
p.domethod();
A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What will happen if a static method is called from an object declaration?

A

It will use the reference to get the method, and not the object method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What will happen is an variable is called from an object declaration?

A

It will get the reference variable, not the object variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which are called by the object and not by the reference during run time?

variables
methods
static methods?

A

methods only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When can methods not be overriden?

A

When they are marked final

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Do you have to implement an abstract method?

A

The first concrete class must implement all parent abstract methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

No, a reference version must exist for compilation to succeed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can you override a method with a more restrictive access modifier?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Can a overridden method have a different argument list?

A

No, that would cause it to be a overloaded method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are the rules for the return type of an overridden method?

A

They must be the same, or subtype of, the overridden return type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Can the access level of an overridden method be less restrictive?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Can overriding methods throw new checked exceptions?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Can overriding methods throw new unchecked exceptions?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Can you override methods that are marked static or final?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How do you invoke the superclass method within the base class?

A

super.supermethod();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Can you use super.super.supermethod() to invoke the grandparent class?

A

No.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
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
26
What must a method to be an overloaded method?
It must change the argument list
27
Can overloaded methods change the return type?
Yes
28
Can overloaded methods change the access modifier?
Yes
29
Can overloaded methods declare new or broader exceptions?
Yes
30
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
31
When is the choice of which overloaded method decided?
At compile time - very nb | All about the reference, not the object
32
Can main() be overloaded?
Yes, but the only special one is the one that is not overloaded
33
if (animal instanceof Dog) { Dog d = (Dog) animal; d.playDead(); } What is this an example of?
Downcasting - casting from generics down to specifics
34
if (animal instanceof Dog) { Dog d = (Dog) animal; d.playDead(); } Will this necessarily compile and execute?
It will compile but will probably not run. | Will probably throw a ClassCastException
35
Can you upcast implicitly?
Yes ``` Dog d = new Dog(); Animal a1 = d; ``` Dog can do all Animal can do, hence no issue
36
What is an interface?
A contract of what methods you must implement, but no rules on how they should be implemented
37
Can you declare checked exceptions on implementation methods other than those declared in the interface?
No
38
What happens if the class which is implementing the interface is abstract?
The buck to implement the methods is passed along to the next concrete class
39
Do interfaces extend or implement other interfaces?
Extend
40
Can an interface extend many interfaces?
Yes
41
Can an interface extend a class?
No
42
Can you change the return type of an overloaded method?
Yes
43
What must be done to an overloaded method?
The argument list must be changed
44
``` void go(){}; String go(){}; ``` Will this compile if the second is declared in a subclass?
No, it's not overloading properly, it needs to change the argument list. Or it needs to use the same return type to override
45
Can an overrode method return anything except for what is return in the super method?
Yes, it can return a subtype of the return of the overrode method.
46
Will this compile? public Button doStuff(){ return null; }
Yes, you can return null to object references
47
Can you return an array?
Yes
48
Will this compile? public int foo(){ char c = 'c'; return c; }
Yes, you can return this because chars can implicitly be converted to ints
49
Will this compile? public int foo(){ float f = 32.5f; return (int) f; }
Yes, you can return values that have been explicitly cast to the return type
50
Will this work? public void bar(){ return; }
Yes, this will work
51
Will this work? ``` public Animal getAnimal(){ return new Horse(); } ```
``` Yes, you can return any object that can be explicitly cast to another Also works with interfaces as the class will therefore pass the IS-A test ```
52
What classes must have constructors?
Every class, including abstract classes
53
Can a constructor have a return type?
No
54
If only constructor is: Foo(String name){} Will this work: Foo f1 = new Foo();
No, because there is no explicit no-arg constructor.
55
What happens when a constructor of a subclass Horse is invoked?
The constructor of the superclass is called (Animal) which is turn calls the constructor of the Object class, then it comes back down, giving out all values as needed
56
What access modifiers can constructors use?
Any
57
Can you have a method with the same name as a constructor?
Yes, be careful of this in the exam
58
What is the default constructor that is generated if their is no constructor typed in?
A no-arg constructor
59
How do you get a no-arg constructor if you've typed in many other overloaded constructors?
You must type it in yourself
60
What does every constructor inherently contain?
A call to an overloaded constructor (this()) or to the superclass constructor (super())
61
Within a constructor, what is being called here (super.(Animal.NAME))
The static variable NAME
62
Can an interface have a constructor?
No
63
How is a constructor invoked?
Only through the call by another constructor
64
If the superclass does not have a no arg constructor, what needs to happen in the base class.
The super method must be fleshed out to pass the required overloaded method parameters
65
What can be called from the constructor?
Only static methods and static variables
66
What is happening here? Animal(){ this(makeRandomName()); }
Animal no arg constructor is making call to other overloaded Animal constructor.
67
What is this and when does it run? static { x= 8; }
A static init block which runs when the class is first loaded.
68
What is this and when does it run? { o = 0 }
An instance init block that runs with each instance of the class
69
What is the order of init blocks?
As they appear in the class, right after the call to super.
70
Do static variables need to be initialised?
No, they get initialised automatically, like instance variables.
71
Can static methods be overridden in a subclass?
No, they would be redefined and different from the static method which has the same name.
72
What are the two features of encapsulation?
- Instance variables are kept protected, usually with the private modifier - Getter and setter methods
73
How is an IS-A relationship expressed?
Through extends or implements
74
How is a HAS-A relationship expressed?
An instance of one class has a reference to an instance of another
75
What determines which methods are called, the reference variable type of the object type?
The reference variable type.
76
What is a covarient return with regards to overridden methods?
Return a subtype of the return type
77
What determines what overridden method is used at runtime?
The object type
78
What determines which overloaded method will be used at compile time?
The reference type
79
How do you explicitly downcast from an object to a String?
Object o = "a String"; | String s = (String) s;
80
How do you properly implement an interface?
By implementing all of it's methods
81
For a method that has a void return type, how do you escape the method quickly?
Simple issue a return;