Methods & Encapsulation Flashcards

1
Q

What two elements of a method signature can never be separated?

A

The method name and its return type. If they are separated, the code will not compile

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

What are the 5 requirements for a method signature to be executable by the JVM?

A

(1) Be called “main”, (2) be public (3) be static (4) Accept an array of strings as a parameter (5) have a return type of void. Note : it can be final

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

Describe encapsulation

A

Encapsulation is the concept of writing software in components so that only the necessary parts are exposed for use by client code. e.g. making variables private and only providing access to them via setters and getters. It makes code maintainable (can be changed without changing the API).

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

Is method overloading determined at compile time or runtime?

A

Compile time!! Hence, the reference type is used rather than the object type for determining which method is invoked

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

Describe object creation

A

Objects can never be created without being constructed. In other words, you cannot have an instance of an object without not only its constructor (or one of its constructors being called) but also all of its superclasses constructors being called.

Whenever you use the new keyword, an objects constructor is called.

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

Does an abstract class have to have a constructor?

A

yes - all classes regardless of whether they are abstract must have a constructor even if it is implicit.

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

Describe constructors and there use

A

Typically constructors are used to initialize the instance varibales of an object. Avoid using methods that can be overriden.

You should try to have a default empty constructor if it makes sense to do so. It is a design principle - lots of apis make use of classes and require no args constructor.

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

Describe the call stack for 2 classes A and B, A extends B each having a default constructor, otherwise known as constructor chaining

A

A is created, then the constructor B is called then Object is called then Object finishes constructing, then B then back down to A.

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

Describe the default constructor

A

(1) It has the same access type as the class
(2) It has a no-args constructor
(3) It has a call to the super constructor of the super class

NOTE - every constructor has code added to call super() equivalent if it is not inserted by the programmer

If you supply a no-arg constructor in the super class then you must match that in the subclass. The compiler can only insert a no arg super() call.

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

Can constructors be inherited?

A

Nope.

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

What is the third construct that allows code to be written after methods and constructors?

A

initialization blocks.

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

What are the two types of initialization blocks and when are they run?

A

Static and instance initialization blocks. Static blocks are run when the class if first loaded into the JVM and instance blocks are run each time an instance of a class is created and loaded into the JVM.

The order in which they appear in a class matters. You can have multiple init blocks and they are executed in the order they appear in the class.

Instance blocks are executed immediately after the call to super() in the constructor.

Instance blocks are often used to put code that is shared by all constructors to remove duplication.

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

What error is thrown in a initialization block?

A

ExceptionInitializationError regardless of the error. It may then give you more details after throwing this error.

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

Why do you need static members?

A

They belong to the class rather than an object of the class. static methods are not dependant in any way upon the state of that particular object.

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

Can static methods be overriden?

A

No, they belong to that particular class rather than an instance of the class. They can, however be redefined or shadowed. This is not the same as being overriden, as there are now actually two different methods belonging to the overriden class and the class overriding.

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