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
Q

What must you make sure when overriding and calling a method that the original method has a checked exception?

A

That even if the base method isn’t being called, that the checked exception is still caught by the overridden method

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

What must a method to be an overloaded method?

A

It must change the argument list

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

Can overloaded methods change the return type?

A

Yes

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

Can overloaded methods change the access modifier?

A

Yes

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

Can overloaded methods declare new or broader exceptions?

A

Yes

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

public void doStuff(int y, String s){}
public void doStuff(int y, long p){}

Is the second method an override or an overload?

A

It’s an overload, the argument list has changed

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

When is the choice of which overloaded method decided?

A

At compile time - very nb

All about the reference, not the object

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

Can main() be overloaded?

A

Yes, but the only special one is the one that is not overloaded

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

if (animal instanceof Dog) {
Dog d = (Dog) animal;
d.playDead();
}

What is this an example of?

A

Downcasting - casting from generics down to specifics

34
Q

if (animal instanceof Dog) {
Dog d = (Dog) animal;
d.playDead();
}

Will this necessarily compile and execute?

A

It will compile but will probably not run.

Will probably throw a ClassCastException

35
Q

Can you upcast implicitly?

A

Yes

Dog d = new Dog();
Animal a1 = d;

Dog can do all Animal can do, hence no issue

36
Q

What is an interface?

A

A contract of what methods you must implement, but no rules on how they should be implemented

37
Q

Can you declare checked exceptions on implementation methods other than those declared in the interface?

A

No

38
Q

What happens if the class which is implementing the interface is abstract?

A

The buck to implement the methods is passed along to the next concrete class

39
Q

Do interfaces extend or implement other interfaces?

A

Extend

40
Q

Can an interface extend many interfaces?

A

Yes

41
Q

Can an interface extend a class?

A

No

42
Q

Can you change the return type of an overloaded method?

A

Yes

43
Q

What must be done to an overloaded method?

A

The argument list must be changed

44
Q
void go(){};
String go(){};

Will this compile if the second is declared in a subclass?

A

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
Q

Can an overrode method return anything except for what is return in the super method?

A

Yes, it can return a subtype of the return of the overrode method.

46
Q

Will this compile?

public Button doStuff(){
return null;
}

A

Yes, you can return null to object references

47
Q

Can you return an array?

A

Yes

48
Q

Will this compile?

public int foo(){
char c = ‘c’;
return c;
}

A

Yes, you can return this because chars can implicitly be converted to ints

49
Q

Will this compile?

public int foo(){
float f = 32.5f;
return (int) f;
}

A

Yes, you can return values that have been explicitly cast to the return type

50
Q

Will this work?

public void bar(){
return;
}

A

Yes, this will work

51
Q

Will this work?

public Animal getAnimal(){
return new Horse();
}
A
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
Q

What classes must have constructors?

A

Every class, including abstract classes

53
Q

Can a constructor have a return type?

A

No

54
Q

If only constructor is:

Foo(String name){}

Will this work:

Foo f1 = new Foo();

A

No, because there is no explicit no-arg constructor.

55
Q

What happens when a constructor of a subclass Horse is invoked?

A

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
Q

What access modifiers can constructors use?

A

Any

57
Q

Can you have a method with the same name as a constructor?

A

Yes, be careful of this in the exam

58
Q

What is the default constructor that is generated if their is no constructor typed in?

A

A no-arg constructor

59
Q

How do you get a no-arg constructor if you’ve typed in many other overloaded constructors?

A

You must type it in yourself

60
Q

What does every constructor inherently contain?

A

A call to an overloaded constructor (this()) or to the superclass constructor (super())

61
Q

Within a constructor, what is being called here (super.(Animal.NAME))

A

The static variable NAME

62
Q

Can an interface have a constructor?

A

No

63
Q

How is a constructor invoked?

A

Only through the call by another constructor

64
Q

If the superclass does not have a no arg constructor, what needs to happen in the base class.

A

The super method must be fleshed out to pass the required overloaded method parameters

65
Q

What can be called from the constructor?

A

Only static methods and static variables

66
Q

What is happening here?

Animal(){
this(makeRandomName());
}

A

Animal no arg constructor is making call to other overloaded Animal constructor.

67
Q

What is this and when does it run?

static { x= 8; }

A

A static init block which runs when the class is first loaded.

68
Q

What is this and when does it run?

{ o = 0 }

A

An instance init block that runs with each instance of the class

69
Q

What is the order of init blocks?

A

As they appear in the class, right after the call to super.

70
Q

Do static variables need to be initialised?

A

No, they get initialised automatically, like instance variables.

71
Q

Can static methods be overridden in a subclass?

A

No, they would be redefined and different from the static method which has the same name.

72
Q

What are the two features of encapsulation?

A
  • Instance variables are kept protected, usually with the private modifier
  • Getter and setter methods
73
Q

How is an IS-A relationship expressed?

A

Through extends or implements

74
Q

How is a HAS-A relationship expressed?

A

An instance of one class has a reference to an instance of another

75
Q

What determines which methods are called, the reference variable type of the object type?

A

The reference variable type.

76
Q

What is a covarient return with regards to overridden methods?

A

Return a subtype of the return type

77
Q

What determines what overridden method is used at runtime?

A

The object type

78
Q

What determines which overloaded method will be used at compile time?

A

The reference type

79
Q

How do you explicitly downcast from an object to a String?

A

Object o = “a String”;

String s = (String) s;

80
Q

How do you properly implement an interface?

A

By implementing all of it’s methods

81
Q

For a method that has a void return type, how do you escape the method quickly?

A

Simple issue a return;