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