Inheritance
Inheritance
Inheritance
Inheritance is the process of writing new class by inheriting commonly used state
and behaviour of existing class.
Existing Class is called as Super class or Base class or Parent Class.
Newly defined Class IS called as Sub class or Derived class or Child class
Inheritance is an important property or, features of OOPS.
Main goal of inheritance is code reusability i.e. Inheritance can' be used to reuse-the
properties and operations of the existing class in the newly defined class.
Types Of Inheritance
1) Simple Inheritance
2) Multilevel Inheritance
3) Hierarchical Inheritance
4) Multiple Inheritance
5) Hybrid Inheritance
6) Cyclic Inheritance
Simple Inheritance
In Simple inheritance,
o There will be only one super class and one sub class.
Multilevel Inheritance
In Multilevel inheritance,
o One super class can have only one direct sub class and many indirect sub
classes.
o One sub class can have only one direct super class and many indirect super
classes.
Hierarchical Inheritance
In Hierarchical inheritance,
o One super class can have many direct sub classes.
o One sub class can have only one direct super class.
Ex: class A {...}
Class B extends A {...}
class C extends A {...}
Multiple Inheritance
In Multiple inheritance,
o One sub class can have many direct super classes.
o One super class can have only one direct sub class.
Ex: class A {...}
class B {...}
class C extends A, B {...} // INVALID
Java does not support Multiple inheritance with classes.
Java support Multiple inheritance with interfaces.
Hybrid Inheritance
Super Keyword
super is a keyword and it will be used to refer the, members of immediate super
class.
super keyword cannot be accessed from static context.
super keyword can be used in three ways:
o To access the immediate super class variables.
Syntax:
super. <variableName>
Ex:
super.a
super.b
o To access the immediate super class methods.
Syntax:
Ex:
SUMMARY
17) When you access static members of super class using subclass name then subclass
will not be loaded.
18) When JVM creates the object then:
a. It allocates the memory of instance variables of super class.
b. It allocates the memory of instance variables of subclass.
c. it executes the instance blocks and constructors of super class.
d. It executes the instance blocks and constructors of sub class.
19) Constructors of super class will not be inherited to subclass.
20) You can use same name for super class variables, sub class variables or local
variables.
21) When you access any variable directly then following things will happen:
a. Checks whether that variable is declared in the local scope or not.
b. If found in the local scope, that local variable will be used.
c. If not found in the local scope then 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 in the class scope then checks whether that variable is inherited
from super classes or not.
f. if found, that inherited variable will be used.
22) When you have same name for local variables, class level variables and super class
variables then do the following:
a. Refer the local variable directly.
b. Refer the class level variable using this keyword.
c. Refer the super class level variable using super keyword.
23) super keyword can be used to access both instance and static members.
24) super keyword cannot be used from static context.
25) When you are not writing any constructor inside the class then one default
constructor will be inserted by the java Compiler.
26) When you are writing any constructor inside the class then default constructor will
not be inserted by the Java Compiler.
27) when you are not writing any super statement inside the constructor then default
super will be inserted by the lava Compiler.
28) When you are writing any super statement inside the constructor then default super
will not be inserted by the java Compiler.
29) You can invoke super class constructor explicitly using super keyword.
30) call to super constructor must be first statement from subclass constructor.
31) call to this constructor must be first statement from constructor. .
32) The first statement of any constructor can be either this or super, can't be both at a
time.
33) Order of constructor invocation is from subclass to super class.
34) Order of constructor execution is from super class to subclass.
35) private members of super class cannot be accessed using super keyword also.