Monday 20 March 2017

Inheritance -Manager is a Employee and Employee is a Person

import java.util.*;
class Person
{
String name;
String address;
int age;
Person()
{
name=" ";
address=" ";
age=0;
}
Person(String name,String address,int age)
{
this.name=name;
this.address=address;
this.age=age;

}
void display()
{
System.out.println(name+"\t"+age+"\t"+address+"\t");
}

}
class Employee extends Person
{
int id;
static int count=0;
float sal;
Employee()
{
super();
id=0;
sal=0.0f;
}
Employee(String name,String address,int age,float sal)
{
super(name,address,age);
this.sal=sal;id=++count;
}
void display()
{
super.display();
System.out.println(id+"  "+sal);
}
}
class Manager extends Employee
{
float totalsal;
float incentive;
Manager()
{
super();
incentive=0.0f;
}
Manager(String name,String address,int age,float sal,float incentive)
{
super(name,address,age,sal);
this.incentive=incentive;
}
void display()
{
super.display();
System.out.println(incentive+"\t"+totalsal+"\t");
}
void totalsalary()
{
totalsal=sal+incentive;
}
}
class Demo
{
public static void main(String args[])
{
Person p=new Person();
Employee e=new Employee();

Scanner sc=new Scanner(System.in);
System.out.println("enter name ,address,age,sal,incentive");
Manager m=new Manager(sc.nextLine(),sc.nextLine(),sc.nextInt(),sc.nextFloat(),sc.nextFloat());
m.totalsalary();
m.display();
}
}

No comments:

Post a Comment