Inheritance in Java 7

This tutorial describe concept of Inheritance. It is one of OOPs concept.

Inheritance in Java 7

Inheritance in Java 7

This tutorial describe concept of Inheritance. It is one of OOPs concept.

Inheritance :

Inheritance is the capability of a class to use the properties and methods of another class and you can also add its new properties and methods. In OOPs, the concept of inheritance provides the idea of reusability.

Take a simple real world example of Son and Father to understand the concept of inheritance. Father works as super class and having some properties and functionality. His Son is like subclass who inherits some properties and functionality from his Father and also having his own new properties and functionality.

extends keyword is used for inheritance.

For example -

class Class1{
..............
}

class Class2 extends Class1{
...........
}

Example :

In this example Employee.java is class which contains details of employee as id, name and salary. We are inheriting in in another two class which are PartTimeEmployee.java and FullTimeEmployee.java which have their own properties too. In main class we are instantiating these two and calling methods of base class.

Employee.java -

package inheritance;

class Employee {
	private int empId;
	private String empName;
	private long salary;

	public int getEmpId() {
		return empId;
	}

	public void setEmpId(int empId) {
		this.empId = empId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public long getSalary() {
		return salary;
	}

	public void setSalary(long salary) {
		this.salary = salary;
	}

	public void details() {
		System.out.println("Employee Id : " + empId);
		System.out.println("Employee Name : " + empName);
		System.out.println("Employee salary : " + salary);
	}
}

PartTimeEmployee.java -

package inheritance;

class PartTimeEmployee extends Employee{
	String jobType="Part Time";
	public void display(){
		System.out.println("Job Type : "+jobType);
	}
}

FullTimeEmployee.java -

package inheritance;

class FullTimeEmployee extends Employee {
	String jobType = "Full Time";

	public void display() {
		System.out.println("Job Type : " + jobType);
	}
}

InheritanceMain.java -

package inheritance;

class InheritanceMain {
	public static void main(String[] args) {
		PartTimeEmployee employee1 = new PartTimeEmployee();
		employee1.setEmpId(1);
		employee1.setEmpName("Jackson");
		employee1.setSalary(20000);
		employee1.details();
		employee1.display();
		FullTimeEmployee employee2 = new FullTimeEmployee();
		employee2.setEmpId(2);
		employee2.setEmpName("Coraline");
		employee2.setSalary(40000);
		System.out.println("-------------------------");
		employee2.details();
		employee2.display();
	}
}

Output :

Employee Id : 1
Employee Name : Jackson
Employee salary : 20000
Job Type : Part Time
-------------------------
Employee Id : 2
Employee Name : Coraline
Employee salary : 40000
Job Type : Full Time