Classes Flashcards

1
Q

What is the implicit import of every class?

A

java.lang.object

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

What type of inheritance model Java supports?

A

Single inheritance model, multiple inheritance model is not allowed for classes (only interfaces)

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

What modifiers do classes have?

A

final, abstract, sealed, non-sealed, static

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

What is relationship of “this” or “super” and static?

A

“this” and “super” is instance keyword, so it is not available to static members of a class

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

How do we create immutable objects?

A

class needs to be final, all constructors must be private, no setters, all instance variables must be private and all objects related to class must be secure from changes

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

What is defensive copy in context of classes?

A

Creating copy of a object via new instantiation and passing to it a object that is being “protected”

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

What is a default constructor?

A

If we dont define any constructor, compiler auto. adds default constructor to class, which auto. instantiates member fields. It is a constructor with same name as class and contains no parameters.

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

What is “this()”?

A

It is a call to constructor from current instance. It can only be used from within constructor, and can call constructor from instance, but with different signature.

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

What do we need to watch out for when one of the parent classes is not using default constructor?

A

If one of the parent classes has defined a different constructor, all child classes must implement constructors that have same signature as that parent’s constructor class

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

Is it allowed to have multiple explicit calls from constructor to another constructor?

A

No, only one - super() or this()

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

Is var allowed as constructor parameter type?

A

No

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

What is super()?

A

Call to parent class constructor. It is auto. added as first line in every constructor, but we can add it yourself also.h

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

What do we need to watch out regarding classes signatures?

A

When using abstract keyword, it must come before class keyword

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

What can only be abstract in classes?

A

Classes and methods, no variables, no static members or constructors

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

When do we must mark a class as abstract?

A

When we have at least one abstract method inside a class

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

What is the rule for abstract class signature?

A

It cannot be static or final, must contain abstract, and it can be placed before or after access modifier

17
Q

What do we need to watch out regarding method signatures when using abstract?

A

When using abstract keyword, it must come before method return type. abstract keyword can be omitted if abstract is used in class signature

18
Q

What are rules for abstract classes instantiation?

A

It cannot be instantiated

19
Q

What are rules regarding constructors in abstract class?

A

Abstract class has them, all rules apply same as concrete class,

19
Q

What are rules for abstract classes inheritance?

A

They can extend other abstract or concrete classes.
Class that extends abstract class must implement all methods from its parent abstract classes, except if this class is being extended by another class. In other words, any concrete class must implement abstract methods

20
Q

What are rules regarding non-abstract methods in abstract class?

A

Abstract classes can have normal non-abstract methods

21
Q

Can private methods be overridden?

A

No

22
Q

Can static methods be overridden?

A

No, but they can be hidden with the same name and signature in child class. So each static method in parent class needs to be static in child class

23
Q

Can final methods and variables be overridden or hidden?

A

No

24
Q

Can variables be overridden?

A

No, they can be hidden. Child class defines variable with same name as in parent class.

25
Q

What are rules on overriding a method?

A
  • method signature must be the same
  • return type must be the same or a covariant (broader to narrower)
  • if it throws a exception, it can be omitted, or it must be the same or covariant of it
  • access modifier must be the same or broader (protected -> public)
26
Q

How can final fields be initialized?

A

Via init blocks or constructor

27
Q

Do final fields need to be initialized?

A

Yes, before code exits constructor

28
Q

Can fields be null?

A

Yes, only if they are objects

29
Q

When is main method ran?

A

After class initialization

30
Q

Class is being initialized in two phases …

A
  1. Class initialization - static variables in all parent classes and current class
  2. Instance initialization - all other
30
Q

How is class being initialized?

A

Class initialization goes as:
Upmost parent class is being initialized first - all static fields and static blocks are being initialized from top to bottom, as they appear.
Then initialization proceeds to next child class and does the same all down to the starting child class.
Static variables are initialized once.
super() are not being called.

Output would be eg.:
3 static block
2 static block
1 static block

31
Q

How is class instance being initialized?

A

Instance initialization goes as:
Upmost parent class is being initialized first - all static fields and static blocks are being initialized from top to bottom, as they appear.
Then initialization proceeds to next child class and does the same all down to the starting child class.
Static variables are initialized once.
Then init blocks are being run from upmost parent class from top to bottom, as they appear. After that, constructor is being run. This process proceeds to next child class and does the same all down to the starting child class.
super() are being called before init blocks in upper classes.

Output would be eg.:
3 static block
2 static block
1 static block
3 init block
3 constructor
2 init block
2 constructor
1 init block
1 constructor