Package Class Int Void: "Running Test1"
Package Class Int Void: "Running Test1"
Definition: Getting the features or properties of one class from another class is called as Inheritance.
-> The class from which other class acquires the features is called as Super class or Parent class or
Base class.
-> The class which acquires the features or properties is called as Sub class or Child class or Derived
Class.
Through Inheritance we can achieve,
1. Extensibility.
2. Code Optimization.
3. Code Re-usability.
4. Code Maintainability.
To Achieve inheritance between classes, we should use Extends keyword.
There are 4 types of Inheritance,
1. Single Level inheritance.
2. Multilevel inheritance.
3. Hybrid Inheritance.
4. Multiple inheritance.
1. Single Level inheritance: One class inheriting from only one Super class.
2. Multilevel inheritance: One class inheriting from another Sub class.
3. Hybrid Inheritance: Two or more class inheriting from common Super class.
4. Multiple inheritance: One class inheriting from multiple immediate Super class.
Note: Multiple inheritance is not possible is java through Classes instead happens through Interfaces
Note: Only non-static members of a class will be involved in Inheritance.
Example: Single Level inheritance:
package inheritance;
class A
{
int a=10;
void test1()
{
System.out.println("Running test1");
}
}
class B extends A
{
double d=10.5;
void demo1()
{
System.out.println("Running demo2");
}
}
class SingleInheritance
{
public static void main(String[] args)
{
}
}
Super
1. Super Calling Statement is used to call Super Class Constructor in case of
inheritance between Classes.
2. Super Calling Statement will call immediate Super constructor in case of
inheritance.
3. Through Super Calling Statement we can achieve Constructor Chaining.
4. Constructor Chaining means Sub class Constructor calling its immediate Super
class Constructor.
5. Rule: If there is inheritance between classes then Constructor chaining is
mandatory(Rule of Java)
Example: Super
package corejava;
class A
{
A()
{
System.out.println("Constructor of A class");
}
}
class B extends A
{
B()
{
super();
System.out.println("Constructor of B class");
}
}
class C extends B
{
C()
{
super();
System.out.println("Constructor of C class");
}
}
class SuperMain
{
public static void main(String[] args)
{
System.out.println("Main Starts");
C rv=new C();
System.out.println("Main Ends");
}
}
Output:
Main Starts
Constructor of A class
Constructor of B class
Constructor of C class
Main Ends
Note: If there is inheritance between classes and if the Programmer has not
developed super classing statement inside the constructor then there will be
a default Super calling constructor statement developed by the Compiler
i.e.,Super();Default Super calling statement will always be without Arguments
Example: Compiler providing Default Super calling statement
package corejava;
class D
{
D()
{
System.out.println("From D Cosntructor");
}
}
class E extends D
{
E()
{
//super(); //->Deafult Super calling(Commented Delibrately)
System.out.println("From E constructor");
}
}
class SuperMain1
{
public static void main(String[] args)
{
System.out.println("Main starts");
E rv=new E();
System.out.println("Main Ends");
}
}
Output:
Main starts
From D Cosntructor
From E constructor
Main Ends
Super(parameter): This will call the immediate super class constructor with the
matching arguments.
Example:
class H
{
H(int a)
{
System.out.println(" from H(int a) constructor");
}
}
class I extends H
{
I()
{
super(90);// will not work when double/flaot vlue is used
System.out.println(" from I(int a) constructor");
}
}
class Supermain2
{
public static void main(String[] args)
{
I rv=new I();
}
}
Output:
from H(int a) constructor
from I(int a) constructor
Rules for Super class:
1.Super Calling Statement should always be the first statement inside the class.
2.We can develop maximum one super calling statement inside the constructor.
3.Super Calling Statement can be used to call only constructor.
4.If the Programmer has not developed either this() or super(), then compiler
will develop the default Super Calling Statement.
5.If the Programmer writes this calling Statement then there will be no super().
Example:
class M
{
M()
{
System.out.println("From M()");
}
}
class N extends M
{
N(int a)
{
System.out.println("From N()" +a);
}
N()
{
this(10);
System.out.println("From N()");
}
}
class O extends N
{
O()
{
System.out.println("From O()");
}
}
class SuperMain3
{
public static void main(String[] args)
{
O rv=new O();
}
}
Output:
From M()
From N()10
From N()
From O()
Similarity between this and Super calling statement.
1. Both are used to call Constructor.
2. Both should be first statement inside the constructor.
3. Both can be developed only inside constructor.
4. Both this and super Calling Statement cannot be developed inside a constructor
simultaneously at a time.
5. Both this and Super Calling Statement cannot be developed multiple times.
Differences
this callin statement
1. Is used to call Current class
constructor.
2. will be used in case of Constructor
Overloading.
3. will be no default this calling
statement by compiler
Object class
1. Object class is the built-in class in Java.
2. Object class is the Super most class for any class in java automatically.
3. Any class is the sub class to Object class.
4. Object class is the first class in any program.
5. All subclass inherits from object class.
6. Object class contains only one Default constructor with No arguments.
7. There are lot of non-static methods already developed in Object class which
should be part of every object in java.
8. Any object of java will have all the non-static methods of object class.
9. The mandatory features which should be available in every object of java is
developed in object class. Ex: finalaize(),notify(), etc.,
Why Multiple Inheritance is not possible in Java through class.
A
C
Multiple Inheritance is not possible bcz, in case of MI as 1 class(sub class)
will be inheriting from Multiple immediate Super classes then to maintain
chaining we should develop 2 or more super calling statement in sub class which
is against the rule of java.
Multiple Inheritance
1. Multiple Inheritance means One class inheriting from multiple immediate
Super class.
2. If there is inheritance between classes, constructor chaining is mandatory.
3. Constructor chaining can be achieved through Super calling statement.
What is Diamond Problem.
If Multiple inheritance is supported in java then it could create a Diamond
problem or Path Issue i.e., in case of Multiple inheritance One class will have
multiple paths to object class, so same feature of objectc lass might be inherited
to same class multiple times in multiple paths, which will create a ambiguity
of method usage. This is called as Diamond problem.
Object class
clone()
A(Super Class)
Clone()
B(Super Class)
clone()
C(Sub Class)
Clone()
Method OverRiding
Changing the implementation of super class method in the sub class according
to the needs of the sub class is called Method overRididng.
To achieve Method OverRiding 3 things are mandatory,
1. Inheritance.
2. Non-Static Methods.
3. Signature or Methods Declaration should be same.
Whenever we perform Method overRiding super class implementation o fthat method
will be lost or masked in the sub class I.e., we will get he latest implementation
of sub class.