Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

Java

Learn step by step



Inheritance in Java

Inheritance is a mechanism in which one object acquires all the properties and behaviours of parent object.
Inheritance in Java is way to define relationship between two classes. Inheritance defines Parent and Child relationship between two classes. Similar to real world where a Child inherit his parents Surname and other qualities, In Java programming language, Child class inherit its Parent’s property and code. In Java programming, terms Super class and Sub Class is used to represent Parent and Child class, while in other object oriented language like C++, terms base and derived class is used to denote Inheritance. Inheritance is also simplest but not optimal way to reuse code and When one class use Inheritance to extend another class it get access to all non private code written in Super class.

Inheritance represents the IS-A relationship.

Why use Inheritance?

  • For Method Overriding (So Runtime Polymorphism).
  • For Code Reusability.

Syntax of Inheritance

class Subclass-name extends Superclass-name
{
//methods and fields
}

The keyword extends 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 superclass. The new class is called a subclass.

Programmer is the subclass and Employee is the superclass. Relationship between two classes is Programmer IS-A Employee.It means that Programmer is a type of Employee.


class Employee{
float salary=40000;
}

class Programmer extends Employee{
int bonus=10000;

public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}

Output

Programmer salary is:40000.0
Bonus of programmer is:10000

In the above example,Programmer object can access the field of own class as well as of Employee class i.e. code reusability.

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.

Inheritence

Fig1.

When a class extends multiple classes i.e. known as multiple inheritance. For Example:

Inheritence

Fig1.

Inheritance Example

Below you will find an example of inheritance in action using something that most everyone can relate to – a bicycle.


public class Bicycle {

// the Bicycle class has three fields

public int cadence;

public int gear;

public int speed;

// the Bicycle class has one constructor

public Bicycle(int startCadence, int startSpeed, int startGear) {

gear = startGear;

cadence = startCadence;

speed = startSpeed;

}

// the Bicycle class has four methods

public void setCadence(int newValue) {

cadence = newValue;

}

public void setGear(int newValue) {

gear = newValue;

}

public void applyBrake(int decrement) {

speed -= decrement;

}

public void speedUp(int increment) {

speed += increment;

}

}


public class Inheritance{

public static void main(String args[]) {
//Super class reference variable can hold Sub Class instance
Server server = new Tomcat();

//we need to cast to get actual Server instance back in reference variable.
Tomcat tomcat = (Tomcat) server;
tomcat.start(); //starting Server

System.out.println( "Uptime of Server in nano: " + server.uptime());

tomcat.stop();
}


}

class Server{
private long uptime;

public void start(){
uptime = System.nanoTime();
}

public void stop(){
uptime = 0;
}

public long uptime(){
return uptime;
}
}

class Tomcat extends Server{

@Override
public void start(){
super.start();
//Tomcat Server specific task
System.out.println("Tomcat Server started");
}

@Override
public void stop(){
super.stop(); //you can call super class method using super keyword
System.out.println("Tomcat Server Stopped");
}
}

Output

Tomcat Server started
Uptime of Server : 105898370823666
Tomcat Server Stopped


Related Videos