Core Java Session 2
Session
3
Inheritance
Inheritance
Defination:
“Deriving a new class from an old
class is called inheritance”.
Why to use Inheritance:
For to achieve reusability of code.
Scenarios:
Class A{ (Super/Base
class/parent class)
M1(){
}
}
Class B extends A{ (Subclass/Derived class/Child
class)
M1(){
}
}
1)
A a = new A();
a.M1(); //
here M1 of class A is called
2)
A a = new B();
a.M1(); //here
M1 of class B is called
(Method overriding)
3)
A a = new A();
B b=new B();
a=b;
a.M1(); //here
M1 of class B is called
(Method overriding)
-
In above 2 and 3 object a can call
all the methods of A and only those methods of B which are in A.
-
In case of variables they can’t be
overiden.
-
4)
B b = new A(); // Not allowed
Types of Inheritance
1.
Single Inheritance
2.
Multilevel
3.
Hierarchical
1.
Single Inheritance:
Class A //super class / Base class / parent class
Class B extends A //sub class / Derived class / child class
2.
Multilevel Inheritance
Class A //Base class
Class B
extends A //Intermediate
base class
Class C extends B //Derived class
3.
Hierarchical inheritance:
Class B Class C Class
D
Class E Class F
Note: Multiple inheritance in java is
not allowed because it creates ambiguity.
Class A Class B
(Both class Has calculate method)
Class C extends A,B
C c=new C();
c.calculate();
-Here which calculate method (i.e of
A class’s or of B class’s) to call, this is the confusion for JVM,
So multiple inheritance is not
allowed in java.
-Multiple inheritance is achieved in
java using interfaces or by writing repeated single inheritance.
Practicals:
Program 1:
1.Single Inheritance:
class P{
void DisplayP(){
System.out.println("Single
Inheritance: This is Class P's Display method");
}
}
public class
SingleInheritanceDemo2 extends P{
public static void main(String args[]){
SingleInheritanceDemo2
q=new
SingleInheritanceDemo2();
q.DisplayP();
}
}
Interview Questions :
package Single_Inheritance_Practical;
class A1{
void M1(){
System.out.println("This is M1
method of Class A");
}
}
class B1 extends A1{
void M1(){
System.out.println("This is M1
method of Class B");
}
}
public class Rahul_Inheritance1 {
public static void main(String[] args) {
A1 a1=new A1();
a1.M1();
A1 a2=new B1();
a2.M1();
A1 a3=new A1();
B1 b3=new B1();
a3=b3;
a3.M1();
//B
b= new A(); //not allowed
}
}
2.Multilevel Inheritance:
//Multilevel
Inheritance
package Multilevel_Inheritance_Practical;
public class X {
void
DisplayX(){
System.out.println("Multilevel
Inheritance: This is Class X's Display method");
}
}
package Multilevel_Inheritance_Practical;
public class Y extends X{
void
DisplayY(){
System.out.println("This is Class
Y's Display method");
}
}
package Multilevel_Inheritance_Practical;
//
Example of Multilevel Inheritance
public class Z extends Y{
public static void main(String args[]){
Z z=new Z();
z.DisplayX();
}
}
3.Hierarchical Inheritance:
package
Hierarchical_Inheritance_Practical;
public class R {
void DisplayR(){
System.out.println("Hierarchical_Inheritance: This is
Class R's Display method");
}
}
package
Hierarchical_Inheritance_Practical;
public class S extends R{
void DisplayS(){
System.out.println("Hierarchical_Inheritance: This is
Class S's Display method");
}
}
package
Hierarchical_Inheritance_Practical;
public class T extends R{
void DisplayT(){
System.out.println("Hierarchical_Inheritance: This is
Class T's Display method");
}
}
package
Hierarchical_Inheritance_Practical;
//Example of Multilevel Inheritance
public class U extends R{
void DisplayU(){
System.out.println("Hierarchical_Inheritance: This is
Class U's Display method");
}
}
package
Hierarchical_Inheritance_Practical;
public class V extends T{
public static void main(String args[]){
V
v=new V();
v.DisplayR();
}
}
Comments
Post a Comment