Summary
- Java does not support multiple inheritance with classes.
- Java supports multiple inheritance with interfaces.
- Java does not support hybrid inheritance which uses multiple inheritance.
- You can use
extendskeyword to inherit superclass functionalities in subclass. - You can access only superclass members by using superclass object.
- You can access superclass members as well as subclass members by using subclass object.
- All the superclass members can be accessed from subclass directly.
- Subclass members cannot be accessed from superclass directly.
Objectis the default superclass for all Java classes.- When a class is not extending any class, then
Objectclass becomes direct superclass.
class Hello { }// Object is direct superclass
- When a class is extending any superclass, then
Objectbecomes indirect superclass.
class Hai { }
class Hello extends Hai { }// Object is indirect superclass
- Java does not support cyclic inheritance.
- Cyclic inheritance involves two cases:
a)
class A extends A { }
b)
class A extends B { }
class B extends A { }
finalclasses cannot be subclassed.privatemembers of superclass will not be inherited to subclass.
- When JVM loads a class then:
a) It checks whether superclass is loaded or not.
b) If superclass is not loaded, then it loads superclass first, then subclass.
c) If superclass is already loaded, then it loads subclass directly.
- When you access static members of superclass using subclass name, then subclass will not be loaded.
When JVM creates the object then:
a) It allocates the memory of instance variables of superclass.
b) It allocates the memory of instance variables of subclass.
c) It executes the instance blocks and constructors of superclass.
d) It executes the instance blocks and constructors of subclass.
- When superclass contains
main()method, then it will be inherited to subclass also. → You can execute the Java program either with superclass name or subclass name. - Constructors of superclass will not be inherited to subclass.
- You can use the same name for superclass variables, subclass variables, or local variables.
When you access any variable directly, the following happens:
a) Checks whether that variable is declared in the local scope or not.
b) If found in local scope → that local variable will be used.
c) If not found → checks whether that variable is declared in the class scope or not.
d) If found → that class-level variable will be used.
e) If not found → checks whether that variable is inherited from superclass or not.
f) If found → that inherited variable will be used.
When same variable name exists in all levels (local, class, superclass):
a) Refer the local variable directly.
b) Refer the class-level variable using this keyword.
c) Refer the superclass variable using super keyword.
super keyword rules
superkeyword can be used to access both instance and static members.superkeyword cannot be used from static context.
If same variable or method exists in both superclass and subclass:
a) Using superclass reference → accesses only superclass members.
b) Using subclass reference → accesses only subclass members.
Important Notes
superkeyword is used to access immediate superclass members only.- You cannot access indirect superclass members using
superif the same member exists in the immediate superclass. - To access indirect superclass members → use methods (not direct
super).
Quick Understanding
- Variable resolution order → Local → Class → Superclass
this→ current classsuper→ parent class- Constructors → not inherited
- Methods → inherited
Constructor Rules & super / this (Important Points)
- When you are not writing any constructor inside the class, then one default constructor will be inserted by the Java compiler.
- When you are writing any constructor inside the class, then default constructor will not be inserted by the Java compiler.
- When you are not writing any
superstatement inside the constructor, then defaultsuper()will be inserted by the Java compiler. - When you are writing any
superstatement inside the constructor, then defaultsuper()will not be inserted by the Java compiler. - You can invoke superclass constructor explicitly using
superkeyword. - Call to super constructor must be the first statement in subclass constructor.
- Call to this constructor must be the first statement in constructor.
- The first statement of any constructor can be either
this()orsuper(), but not both at the same time. - Order of constructor invocation is from subclass to superclass.
- Order of constructor execution is from superclass to subclass.
- Private members of superclass cannot be accessed using
superkeyword also.