Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Jdk7 Inheritance in Java 7

Related Tutorials


 
 

Share on Google+Share on Google+

Inheritance in Java 7

Advertisement
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 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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: December 13, 2012

Related Tutorials

Discuss: Inheritance in Java 7  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com