Inheritance Example 1- Employee - Java

Program:
class employe
{
    protected int emp_no;
    protected String name;
    protected int salary;
    public employe(int empno,String nam,int sal)
    {
        emp_no=empno;
        name=nam;
        salary=sal;
    }
    public void emplo_data()
    {
        System.out.println("\nEmploy no.="+emp_no); 
        System.out.println("Name="+name);
        System.out.println("Salary="+salary);
    }
}
class manager extends employe
{
    int reward;
    public manager(int empno,String nam,int sal,int p)
    {
        super(empno,nam,sal);
        reward=p;
    }
    public void managerdata()
    {
        System.out.println("\nEmploy no.="+emp_no);
        System.out.println("Name="+name);
        System.out.println("Salary="+salary);
        System.out.println("Reward="+reward);
    }
}
class scientist extends employe
{
    int perks;
    public scientist(int empno,String nam,int sal,int s)
    {
        super(empno,nam,sal);
        perks=s;
    }
    public void scientistdata()
    {
        System.out.println("\nEmploy no.="+emp_no);
        System.out.println("Name="+name);
        System.out.println("Salary="+salary);
        System.out.println("Perks="+perks);
    }
}
class inheritance
{
public static void main(String args[])
{
    employe emp= new employe(1,"Varun",20000);
    emp.emplo_data();
    manager man= new manager(2,"Arun",50000,1000);
    man.managerdata();
    scientist scient= new scientist(3,"Nithin",60000,5000);
    scient.scientistdata();
}
}

Output:
nn@linuxmint ~ $ javac inheritance.java
nn@linuxmint ~ $ java inheritance

Employ no.=1
Name=Varun
Salary=20000

Employ no.=2
Name=Arun
Salary=50000
Reward=1000

Employ no.=3
Name=Nithin
Salary=60000
Perks=5000
nn@linuxmint ~ $

6 comments:

  1. by using same employee class how to get particular employee details suppose i want nithin details only please provide code

    ReplyDelete
  2. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand inheritance program concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...