Inheritance in java
• It means transferring properties from one class to another in java ,
properties of one class can be transfer to another class is known as
inheritance.
• Extends keywords= is used to perform inheritance in java.
Types of inheritance
• [Link] Inheritance
• [Link] Inheritance
• [Link] Inheritance
• [Link]
• 5. Hybrid Inheritance
Types of inheritance
• On the basis of class, there can be three types of inheritance: single, multilevel and hierarchical. Multiple and
Hybrid is supported through interface only. We will learn about interfaces later.
Syntax of Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
Single Inheritance
class A class Check
{ {
void msg()
public static void main(String args[])
{
[Link]("I Am Class A"); {
}} B b1=new B();
class B extends A [Link]();
{
[Link]();
void disp()
{ }
[Link]("I Am Class B"); }
}
}
Note-: When a class inherits another class , it is known as single
inheritance .In single inheritance only one super class and one is sub
class.
Multilevel Inheritance
class A class Check
{
void msg()
{
{ public static void main(String args[])
[Link](“I Am Class A”); {
} C c1=new C();
} [Link]();
class B extends A c1
{
void disp()
{
[Link](“I Am Class B”);
}
}
class C extends B
{
void show()
{
[Link](“I Am Class Css”);
}
}
Multiple Inheritance
• Whenever two or class transfer it’s property into single class is known as
multiple inheritance.
• Java does not support it because of ambiguity(duplication) problem.
Hierarchical
• Whenever single class transfer it’s property into more than one class is known as Hierarchical inheritance.
class BTech
{
void show()
{
[Link](“method of [Link] class”);
}
}
class MCA extends BTech
{
void disp()
{
[Link](“method of MCA class”);
}
}
class MBA extends BTech
{
void disp2()
{
[Link](“method of MBA class”);
}
}
Hierarchical
class Check
{
public static void main(String args[])
{
MBA m1=new MBA();
[Link]();
[Link]();
m1.disp2();
}
}
Method Overriding
• Whenever base class(super class) and derived class both have same name method than base class method lost it’s
definition is known as method overriding.
class A
{
void show()
{
[Link](“method of A class”);
}
}
class B extends A
{
void show()
{
[Link](“method of B class”);
}ss
}
class Check
{
public static void main(String args[])
{
B b1=new B();
Output-:
[Link](); method of B class
}
}
Super keyword
• To overcome the problem of method overriding we can use ‘super’ keyword.
• It is used to call base class method when base class and derived class have same name method .
class A
{
void show()
{
[Link](“method of A class”);
}
}
class B extends A
{
void show()
{
[Link]();
[Link](“method of B class”);
}
}
class Check
{
public static void main(String args[]) Output-:
{ method of A class
B b1=new B(); method of B class
[Link]();
}
}
final keyword
• Final is keyword that can be used with the variable, method and class.
final with variable(constant)
final with method(Restrict overriding)
final with class(Restrict inheritance)
Final variable-: It creates constant value that means it never change their value during execution.
class F1
{
final float f=3.14f;
public static void main(String args[])
{
F1 c=new F1();
c.f=3.14f+2.14f;
[Link](c.f);
}
Error-: cannot assign a value to final variable f
}
final with method
Final keyword is used with the method which restrict overriding that means derived class can not create
same name method and could not change their signature.
class A
{
final void show()
{
[Link](“method of base class”);
}}
class B extends A
{
void show()
{
[Link](“Method of derived class”);
}}
class F1{
public static void main(String args[]){
B b1=new B();
[Link](); Error-:code produce an error that is show() can not override
because created as final
}}
Final with class
• It can be used with the class to restrict inheritance that means user can cr eate instance of that class
but could not inherit it.
final class A{
void show1()
{
[Link]("method of base class");
}}
class B extends A
{
void show2(){
[Link]("Method of derived class");
}}
class F1{
public static void main(String args[]){
B b1=new B();
b1.show1();
b1.show2();
}}
this keyword
• [Link] keyword refers to the current object inside a method or constructor.
Ex:
class A Id number
{ a1
void show() A a1=new A();
{
[Link](this); This
}
public static void main(String args[])
{
A a1=new A(); Output-:
[Link](a1); A@36baf30c
[Link](); A@36baf30c
}}
this keyword
• Whenever the name of instance and local variable both are same then our runtime
environment JVM gets confused that which one is local variable and which one is instance
variable, to avoid this problem we should use this keyword.
class A{
int a; Ensure a is instance
A(int a) variable(this refer to current
{ object, here instance variable
this.a=a; a is part of object.
}
void show(){
[Link](a);
}
public static void main(String args[]){ Output
A a1=new A(200); 200
[Link]();
}}
this keyword
3. It is also used when we want to call the default constructor of it’s own class.
class A{
A()
{
[Link]("hello default constructor");
}
A(int a)
{
this();//called default constructor
[Link](a);
}
Output
public static void main(String args[]) hello default constructor
{ 200
A a1=new A(200);
}}
this keyword
• It is also called parametrized constructor of its own class.
class A
{
A()
{
this(100);//called default constructor
[Link]("hello default constrcutor");
}
A(int a)
{
[Link](a);
Output
} 100
public static void main(String args[]) hello default constructor
{
A a1=new A();
}}
super keyword
• Super keyword refer to the object of super class, it is used when we want to call the super class variable, method, and constructor
through sub class object
• Note-:Whenever the super class & sub class variable and method name both are same than it can be used only.
Example with variable
Class A
{
int a=10;
}class B extends A
{
int a=90;
void show()
{
[Link](a);
}
}
Class Check
{
public static void main(String args[])
{
B b1=new B();
[Link]();
}
}