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 ~ $

8 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
  3. Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.
    java training in chennai | java training in velachery

    ReplyDelete
  4. Inheritance is unidirectional, it is expressed using “is a “ relationship. The car is a Vehicle, but all the vehicles are not a car.
    Very informative ..i suggest this blog to my friends..Thank you for sharingjava training in chennai | chennai's no.1 java training in chennai | best java institute in chennai

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...