Share on Google+Share on Google+

Inheritance in Java 7

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

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 is class which contains details of employee as id, name and salary. We are inheriting in in another two class which are and which have their own properties too. In main class we are instantiating these two and calling methods of base class. -

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);
} -

package inheritance;

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

package inheritance;

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

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

package inheritance;

class InheritanceMain {
	public static void main(String[] args) {
		PartTimeEmployee employee1 = new PartTimeEmployee();
		FullTimeEmployee employee2 = new FullTimeEmployee();

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


Posted on: December 13, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Inheritance in Java 7  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image