Class optional modifiers
final
abstract
sealed
non-sealed
static
Can you explicitly inherit Object class in Java
Yes, you can explicitly inherit Object in Java, but it’s completely redundant since every class already implicitly extends Object
How many public classes in one file
Almost one public top level.
Meaning it’s not required but when you do there has to be maximum 1
Class access modifiers for top level class
Note: nested classes can have more
public
{package}
protected is only for members
private not allowed because when you do other classes won’t be able to use it so what’s the point.
Hiding a variable
When you declare a variable in subclass that is also present in super class
You still have 2 copies in subclass but subclass takes precedence when referred. user super operator to refer to the super class members and this operator to refer to the current class (not object)
Also reference type being a super class type will always executes and refer to super class members
Default constructor
Java adds it during compile time when no other constructors are coded irrespective of signature
public Animal(){
super();
}
Are fields polymorphic when you override variables?
No
Only methods are polymorphic and evaluated at runtime. Variables are only hidden and accessed based on super/this/reference type
this() or super() placement in overloaded constructors
If used it should be the only one and first statement
Note: comment lines if placed before are ignored
Note: if neither used compiler puts super() [no argument constructor] automatically in 1st line of the overloaded constructor
Subclass not having any constructor but super class has overloaded constructor with arguments but not an explicit no argument constructor
Doesn’t compile
Since Java inserts default constructor in subclass when not declared it expects a similar constructor from super class either created by default or an explicit constructor coded
Solution: you should use constructor in subclass that calls super(arguments)
super() always refers to the most direct parent
It’s transitive. A super class can use super() to call it’s immediate super class type
final instance variable assignment
It has to be set exactly once during initialization process that includes declare statements, initializer and constructors
Note: static final variables have two places only. Declaration or static initializer
Note: unlike local final variables which are not required to be initialized before they are first used, instance variables need to be assigned during instantiating
Static variable initialization during class loading
1) starts with higher most super class and wind down to its inner most subclass
2) in each class execute static variable declaration and initializers in the order they appear
Instance variable order
1) initialize the super class by executing variable declaration and initializers in the order they appear then executes given constructors in super class
2) wind down to subclass and do the same step 1
Instance initialization order
1) class initialization
2) starts with constructor flow from subclass to the superclass. Once you start from super class execute instance variable and initializers in the order they appear
3) continue step 2 in construction chain from highest super class to lowest subclass
Instance creation with respect to static
1) start from super class to subclass and initialize both static variables and initializers in the order they appear
2) repeat step 1 for each class
3) follow constructor chain flow from subclass all the way out to the highest super class. You will end up working from super class. Initialize instance variables and instance initializers in the order they appear followed by constructor method execution
4) repeat step 2 for each class when chain flow comes to the current class
Method overriding rules
1) should have same signature
2) should have at least same access or more access modifier
3) child method should not introduce new checked exception or have a subtype checked exception
4) return type must be same or subtype
Note: Regd 3, it can have any number of unchecked exceptions
Note: if the parent method is a private method, the child method with same signature is not considered as overridden. In this case above rules don’t apply. You can freely use any return type or checked exceptions
Hiding static method
They are bound to be class not an instance
All 4 rules apply in addition to new rule
5) child method needs to be defined as static too. Else it won’t compile
Does static members are inherited
Yes they are even though they can be hidden. If not hidden they are treated as inherited
public class Bear{
public static void sneeze(){}
public void hibernate(){}
public static void laugh(){}
}
public class Panda extends Bear{
public void sneeze(){}
public static void hibernate(){}
protected static void laugh(){}
}
Which lines doesn’t compile
All 3 overriding child methods doesn’t compile
1) child needs to have static
2) can not make it static when you have an instance method in parent class
3) can not have more restrictive access
Abstract method rules
1) The declaration should end with a semicolon and no body
2) only instance methods can be abstract
3) follows all overridng rules
Abstract and final modifier
Not allowed
For obvious reasons. Purpose of abstract is to override whereas final is to stop overriding
Abstract and private modifiers on method
Not allowed
Abstract purpose is to override in another class. If it’s declared as private too then that defeats the purpose
Final and private
Allowed (albeit redundant)
They both serve similar purpose.
Final is to stop overriding and private is to hide it and no way of overriding
Abstract and static modifiers
Not allowed
Abstract is to be used on instance methods only