POLYMORPHISM EXAMPLE IN JAVA

 class Employee  
 {  
      Employee(String n,double s)  
           {  
                name =n;   
                salary=s;   
           }  
      public void setSalary(double s)  
           {  
                salary=s;   
           }  
      public String getName()  
           {  
                return name;   
           }  
      public double getSalary()  
           {  
                return salary;   
           }  
      private String name;   
      private double salary;   
 }  
 class Manager extends Employee  
 {  
      Manager(String s, double d, double b)  
           {  
                super(s,d);  
                bonus=b;   
           }  
 // manager salary is salary+bonus;   
 public void setBonus(double b)  
      {  
           bonus=b;   
      }  
 public double getBonus()  
      {  
           return bonus;   
      }  
 public double getSalary()// method over riding.   
      {  
      double basesalary = super.getSalary(); // calls getSalary method.  
      return basesalary+bonus;            
      }  
      private double bonus;   
 }  
 class PolymorphismTest  
 {  
      public static void main(String[]args)  
      {  
           Employee e = new Employee("Programmer",20000);        
           Manager m = new Manager("Boss",20000,5000);  
           e = new Manager("Boss",20000,5000);// e = m;   
           m = new Employee("Programmer",20000);        
           System.out.println(e.getName());   
      }  
 }  

Post a Comment

3 Comments

  1. put comment

    to // m = new Employee("Programmer",20000);

    and run the program ...............

    ReplyDelete
  2. This program fail because you don't use new Employee and new Manager .

    ReplyDelete
  3. I can't understand..plz give m...easy program...that I can easily understand..

    ReplyDelete