01 - Java Class Design Flashcards

1
Q

what is the equals and hashCode contract?

A

If two object are equal according to the equals method then calling the hashcode method on each of the two objects must produce the same integer result.

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

What is the goal of the Singleton Pattern?

A

The goal of the singleton pattern is to make sure that only one instance of the class is created.

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

How to achieve the singleton pattern?

A

To achieve the singleton pattern, you need to do three things:

  1. Make the constructor of the class private
  2. Add a private static variable of the same class to the class and instantiat it.
  3. Add a public static method, that returns the class member in step 2.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you aggregate features of another class?

A

To aggregate features of another class, you either extend that class(Inheritance) of have an object of the other class in your class(Composition).

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

Wich exceptions can an overriding method trow?

A

An overriding method can only throw either the checked exceptions(and subclasses) listed in the throw clause of the overridden method or decide not to throw any exceptions at all.

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

Are unchecked exceptions required to be listed in the throw clause?

A

Unchecked exceptions (Runtime exceptions) are not required to be listed in a throw clause, thus any method can declare any unchecked exceptions in its throw clause

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

What will the hashcode method in object return?

A

The default HashCode method in object will return a different hashcode for each object.

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

Can you override a final method?

A

You can not override a final method

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

How are fields in an interface defined?

A

All fields defined in an interface are implicitly public, static final

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

How are methods in an interface defined?

A

Interface methods do not have a body and are by default abstract and public

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

can a overriding method make the overridden method more private?

A

An overriding method cannot make the overridden method more private. It can make the method more public though

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

How to make a class immutable?

A
  1. Don’t provide setter methods
  2. Make all fields final and private
  3. Don’t allow subclasses to override methods, simplest way is to make the class final
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are default methods in an interface?

A

Default methods are declared with the default keyword and are implicitly public. and they provide an implementation

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

Can you change the acces modifier when overloading a method?

A

When overloading a method, you are free to make it private, default, protected or public

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

what is method overloading?

A

With method overloading, multiple methods can have the same name with different parameters.

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