Classes Flashcards
What is the implicit import of every class?
java.lang.object
What type of inheritance model Java supports?
Single inheritance model, multiple inheritance model is not allowed for classes (only interfaces)
What modifiers do classes have?
final, abstract, sealed, non-sealed, static
What is relationship of “this” or “super” and static?
“this” and “super” is instance keyword, so it is not available to static members of a class
How do we create immutable objects?
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
What is defensive copy in context of classes?
Creating copy of a object via new instantiation and passing to it a object that is being “protected”
What is a default constructor?
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.
What is “this()”?
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.
What do we need to watch out for when one of the parent classes is not using default constructor?
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
Is it allowed to have multiple explicit calls from constructor to another constructor?
No, only one - super() or this()
Is var allowed as constructor parameter type?
No
What is super()?
Call to parent class constructor. It is auto. added as first line in every constructor, but we can add it yourself also.h
What do we need to watch out regarding classes signatures?
When using abstract keyword, it must come before class keyword
What can only be abstract in classes?
Classes and methods, no variables, no static members or constructors
When do we must mark a class as abstract?
When we have at least one abstract method inside a class
What is the rule for abstract class signature?
It cannot be static or final, must contain abstract, and it can be placed before or after access modifier
What do we need to watch out regarding method signatures when using abstract?
When using abstract keyword, it must come before method return type. abstract keyword can be omitted if abstract is used in class signature
What are rules for abstract classes instantiation?
It cannot be instantiated
What are rules regarding constructors in abstract class?
Abstract class has them, all rules apply same as concrete class,
What are rules for abstract classes inheritance?
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
What are rules regarding non-abstract methods in abstract class?
Abstract classes can have normal non-abstract methods
Can private methods be overridden?
No
Can static methods be overridden?
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
Can final methods and variables be overridden or hidden?
No
Can variables be overridden?
No, they can be hidden. Child class defines variable with same name as in parent class.
What are rules on overriding a method?
- 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)
How can final fields be initialized?
Via init blocks or constructor
Do final fields need to be initialized?
Yes, before code exits constructor
Can fields be null?
Yes, only if they are objects
When is main method ran?
After class initialization
Class is being initialized in two phases …
- Class initialization - static variables in all parent classes and current class
- Instance initialization - all other
How is class being initialized?
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
How is class instance being initialized?
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