Here is the detail with code for inheritance
for super class
we name it Employee
as
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaemployee;
/**
*
* @author jawadmahmood
*/
public class Employee {
protected String name;
protected int id;
/// creating the parameterized constructor
public Employee(int id , String name )
{
this.name = name ;
this.id = id;
}
// defautl constructor
public Employee()
{
this(100 , "NOt defined");
}
// set id
public void setId(int id)
{
this.id = id;
}
// get id
public int getId()
{
return id ;
}
// set name
public void setName(String name )
{
this.name = name;
}
// get name
public String getName()
{
return name;
}
public void dislpay()
{
System.out.println("In Employee Display method");
System.out.println("Employee id " + id + " and name is " + name);
}
public String toString()
{
System.out.println("In employee the record is ");
return "id " + id + "name: " + name;
}
}
for super class
we name it Employee
as
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaemployee;
/**
*
* @author jawadmahmood
*/
public class Employee {
protected String name;
protected int id;
/// creating the parameterized constructor
public Employee(int id , String name )
{
this.name = name ;
this.id = id;
}
// defautl constructor
public Employee()
{
this(100 , "NOt defined");
}
// set id
public void setId(int id)
{
this.id = id;
}
// get id
public int getId()
{
return id ;
}
// set name
public void setName(String name )
{
this.name = name;
}
// get name
public String getName()
{
return name;
}
public void dislpay()
{
System.out.println("In Employee Display method");
System.out.println("Employee id " + id + " and name is " + name);
}
public String toString()
{
System.out.println("In employee the record is ");
return "id " + id + "name: " + name;
}
}
and for sub class we developed with the name of Teacher as
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaemployee;
/**
*
* @author jawadmahmood
*/
public class Teacher extends Employee{
private String qual;
// default constructor
public Teacher()
{
/// implicit call to superclass default constructor
qual = "";
}
// parameterized constructor
public Teacher(int i , String n , String q)
{
super(i , n);
qual = q;
}
public void setId( int id)
{
this.id = id;
}
public int getId()
{
return id;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
/// display the teacher information
public void display()
{
System.out.println("In teacher the ");
super.dislpay();
System.out.println("The teacher qualificationn " + qual );
}
public String toSting()
{
System.out.println("In teacher class");
String emp = super.toString();
return emp + "The qualifaction is " + qual ;
}
}
the we will it to main function
and the main function is this
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaemployee;
/**
*
* @author jawadmahmood
*/
public class JavaEmployee {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Making object ");
Employee e = new Employee (88 , "jawad");
System.out.println("Making object of teacher");
Teacher t = new Teacher(9 , " hamza khan ", "Phd");
/// display the method of employee
e.dislpay();
// display teacher methed
t.dislpay();
}
}
and the out put will as
No comments:
Post a Comment