Inheritance in Java
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
Syntax
public class vipul
{
}
public class Agravat extends vipul
{
}
The extends keyword indicates that you are making a new class that derives from an existing class.
In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.
class vipul{
float salary=200;
}
class Agravat extends vipul
{
int bonus=100;
public static void main(String args[]){
Agravat A=new Agravat();
System.out.println("Programmer salary is:"+A.salary);
System.out.println("Bonus of Programmer is:"+A.bonus);
}
}
OUTPUT
Programmer salary is:200
"Bonus of Programmer is:100