2 Object Orientation Flashcards

1
Q

How do you use encapsulation in your code?

A

Keep instance variables hidden (often with private). If necessary provide public accessor methods according to the naming convention.

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

What are the fancy names for getters and setters?

A

accessors and mutators

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

What are the advantages of providing ‘empty’ getters and setters?

A

Future maintainability: You can add logic to them later without changing the public API of the class

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

Every Java class is a subclass of …

A

Object

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

Mention some methods which every Java class inherits from Object.

A

equals, hashCode, toString, clone, notify, wait

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

How can you create inheritance relationships in Java?

A

extending a class; implementing an interface (since Java 8)

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

Which elements of a class can be inherited and which not?

A

instance variables, static (class) variables, methods: abstract, instance, static; NOT: constructors, initialization blocks

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

Which elements of an interface can be inherited?

A

constants = static final variables, methods: abstract, default, static

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

What determines which methods of an object you can call?

A

the declared type. And NOT the runtime type.

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

Which operator checks for an ‘IS-A’ relationship?

A

instanceof

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

What is a polymorphic object?

A

An object which passes instanceof for multiple types. Thus any object which is not declared as Object.

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

What can change and what can not with a reference variable?

A

not changeable: its type; changeable: the object it refers to (exception: final variables)

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

What does polymorphic method invocation mean?

A

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.

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

Which methods can be overridden?

A

Every inherited method except the final ones.

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

What about the access level of overriding methods?

A

It can be the same or less restrictive than the superclass’ method.

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

Can a subclass in the same package override a default / package method of the superclass?

A

yes

17
Q

Which exceptions can an overriding method throw?

A

arbitrary runtime exceptions. The checked exceptions thrown by the super method or narrower exceptions.

18
Q

What happens, if a class implements two interfaces which both have a default method with the same signature?

A

The class has to override the method. Otherwise compiler error.

19
Q

Can you implement an interface method with a different return type?

A

Yes, with a subtype.

20
Q

What’s the term for reusing the name of a method?

A

overloading

21
Q

What does covariant return mean?

A

Overriding a method and declaring a subtype as return type

22
Q

What can a method with a primitive return type return?

A

Any value or variable which can be * implicitly converted * explicitly cast to the declared return type

23
Q

Can an abstract class have a constructor?

A

Yes. It must.

24
Q

What does constructor chaining mean?

A

A constructor always calls a super constructor. Default: implicitly the one without arguments. Directly or via this and another constructor of the same class.

25
Q

What can be the 1st statement of a constructor?

A

this, super

26
Q

What can you use as an argument to a super or this call?

A

only static variables and methods

27
Q

What is the access modifier of the default constructor?

A

The same as for the class.

28
Q

What happens in the case of circular reference?

A

StackOverflowError

29
Q

When does the code in an instance initialization block run?

A

after all super constructors have been called

30
Q

Where can you put code that performs operations?

A

methods, constructors, initialization blocks

31
Q

When does the code in a static initialization block run?

A

Once. When the class is first loaded.

32
Q

Can you have multiple initialization blocks in a class?

A

Yes. They are run in the order of appearance.

33
Q

What happens if an Exception is thrown in a static init block?

A

java.lang.ExceptionInInitializerError