Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

- - - - -

Polymorphism help ..!!

downcast polymorphism

  • Please log in to reply
1 reply to this topic

#1 Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guest

Posted 22 January 2010 - 08:41 PM

I have encountered some problem on polymorphism ..~

First code:
public class CommissionEmployee3
{
    private String firstName;
    private String lastName;
    private String socialSecurityNumber;
    private double grossSales;
    private double commissionRate;
    
    public CommissionEmployee3(String first , String last , String ssn , double sales , double rate)
    {
        firstName = first;
        lastName = last;
        socialSecurityNumber = ssn;
        setGrossSales(sales);
        setCommissionRate(rate);
    }
    
    public void setFirstName(String first)
    {
        firstName = first;
    }
    
    public String getFirstName()
    {
        return firstName;
    }
    
    public void setLastName(String last)
    {
        lastName = last;
    }
    
    public String getLastName()
    {
        return lastName;
    }
    
    public void setSocialSecurityNumber(String ssn)
    {
        socialSecurityNumber = ssn;
    }
    
    public String getSocialSecurityNumber()
    {
        return socialSecurityNumber;
    }
    
    public void setGrossSales(double sales)
    {
        grossSales = (sales < 0.0) ? 0.0 : sales;
    }
    
    public double getGrossSales()
    {
        return grossSales;
    }
    
    public void setCommissionRate(double rate)
    {
        commissionRate = (rate > 0.0 && rate < 1.0) ? rate : 0.0;
    }
    
    public double getCommissionRate()
    {
        return commissionRate;
    }
    
    public double earnings()
    {
        return getCommissionRate() * getGrossSales();
    }
    
    public String toString()
    {
        return String.format("%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f" , "commission employee" , getFirstName() , getLastName() , "social security number" , getSocialSecurityNumber() , "gross sales" , getGrossSales() , "commission rate" , getCommissionRate());
    }
}

public class BasePlusCommissionEmployee4 extends CommissionEmployee3
{
    private double baseSalary;
    
    public BasePlusCommissionEmployee4(String first , String last , String ssn , double sales , double rate , double salary)
    {
        super(first ,last , ssn , sales , rate);
        setBaseSalary(salary);
    }
    
    public void setBaseSalary(double salary)
    {
        baseSalary = (salary < 0.0) ? 0.0 : salary;
    }
    
    public double getBaseSalary()
    {
        return baseSalary;
    }
    
    public double earnings()
    {
        return getBaseSalary() + super.earnings();
    }
    
    public String toString()
    {
        return String.format("%s %s\n%s: %.2f" , "base-salaried" , super.toString() , "base salary" , getBaseSalary());
    }
}
public class PolymorphismTest
{
    public static void main(String args[])
    {
        CommissionEmployee3 commissionEmployee = new CommissionEmployee3("Sue" , "Jones" , "222-22-2222" , 10000 , .06);
        BasePlusCommissionEmployee4 basePlusCommissionEmployee = new BasePlusCommissionEmployee4("Bob" , "Lewis" , "333-33-3333" , 5000 , .04 , 300);
        System.out.printf("%s %s:\n\n%s\n\n" , "Call CommissionEmployee3's toString with superclass reference" , "to superclass object" , commissionEmployee.toString());
        System.out.printf("%s %s:\n\n%s\n\n" , "Call BasePlusCommissionEmployee4's toString with sybclass" , "reference to subclass object" , basePlusCommissionEmployee.toString());
        
        CommissionEmployee3 commissionEmployee2 = basePlusCommissionEmployee;
        
        System.out.printf("%s %s:\n\n%s\n" , "Call BasePlusCommissionEmployee4's toString with superclass" , "reference to subclass object" , commissionEmployee2.toString());
    }
}

Second code:

public abstract class Employee
{
    private String firstName;
    private String lastName;
    private String socialSecurityNumber;
    
    public Employee(String first , String last , String ssn)
    {
        firstName = first;
        lastName = last;
        socialSecurityNumber = ssn;
    }
    
    public void setFirstName(String first)
    {
        firstName = first;
    }
    
    public String getFirstName()
    {
        return firstName;
    }
    
    public void setLastName(String last)
    {
        lastName = last;
    }
    
    public String getLastName()
    {
        return lastName;
    }
    
    public void setSocialSecurityNumber(String ssn)
    {
        socialSecurityNumber = ssn;
    }
    
    public String getSocialSecurityNumber()
    {
        return socialSecurityNumber;
    }
    
    public String toString()
    {
        return String.format("%s %s\nsocial security number: %s" , getFirstName() , getLastName() , getSocialSecurityNumber());
    }
    
    public abstract double earnings();
    
}
public class SalariedEmployee extends Employee
{
    private double weeklySalary;
    
    public SalariedEmployee(String first , String last , String ssn , double salary)
    {
        super(first , last , ssn);
        setWeeklySalary(salary);
    }
    
    public void setWeeklySalary(double salary)
    {
        weeklySalary = salary < 0.0 ? 0.0 : salary;
    }
    
    public double getWeeklySalary()
    {
        return weeklySalary;
    }
    
    public double earnings()
    {
        return getWeeklySalary();
    }
    
    public String toString()
    {
        return String.format("salaried employee: %s\n%s: $%,.2f" , super.toString() , "weekly salary" , getWeeklySalary());
    }
}
public class HourlyEmployee extends Employee
{
    private double wage;
    private double hours;
    
    public HourlyEmployee(String first , String last , String ssn , double hourlyWage , double hoursWorked)
    {
        super(first , last , ssn);
        setWage(hourlyWage);
        setHours(hoursWorked);
    }
    
    public void setWage(double hourlyWage)
    {
        wage = ( hourlyWage < 0.0) ? 0.0 : hourlyWage;
    }
    
    public double getWage()
    {
        return wage;
    }
    
    public void setHours(double hoursWorked)
    {
        hours = ( (hoursWorked >= 0.0) && (hoursWorked <= 168.0)) ? hoursWorked : 0.0;
    }
    
    public double getHours()
    {
        return hours;
    }
    
    public double earnings()
    {
        if(getHours() <= 40)
        {
            return getWage() * getHours();
        }
        else
        {
            return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;
        }
    }
    
    public String toString()
    {
        return String.format("hourly employee: %s\n%s:$%,.2f; %s: $%,.2f" , super.toString() , "hourly wage" , getWage() , "hours worked" , getHours());
    }
}
public class CommissionEmployee extends Employee
{
    private double grossSales;
    private double commissionRate;
    
    public CommissionEmployee(String first , String last , String ssn , double sales , double rate)
    {
        super(first , last , ssn);
        setGrossSales(sales);
        setCommissionRate(rate);
    }
    
    public void setCommissionRate(double rate)
    {
        commissionRate = ( rate > 0.0 && rate < 1.0) ? rate : 0.0;
    }
    
    public double getCommissionRate()
    {
        return commissionRate;
    }
    
    public void setGrossSales(double sales)
    {
        grossSales = ( sales < 0.0 ) ? 0.0 : sales;
    }
    
    public double getGrossSales()
    {
        return grossSales;
    }
    
    public double earnings()
    {
        return getCommissionRate() * getGrossSales();
    }
    
    public String toString()
    {
        return String.format("%s: %s\n%s: $%,.2f; %s: %.2f" , "commission employee" , super.toString() , "gross sales" , getGrossSales() , "commission rate" , getCommissionRate());
    }
}
public class BasePlusCommissionEmployee extends CommissionEmployee
{
    private double baseSalary;
    
    public BasePlusCommissionEmployee(String first , String last , String ssn , double sales , double rate , double salary)
    {
        super(first , last , ssn , sales , rate);
        setBaseSalary(salary);
    }
    
    public void setBaseSalary(double salary)
    {
        baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
    }
    
    public double getBaseSalary()
    {
        return baseSalary;
    }
    
    public double earnings()
    {
        return getBaseSalary() + super.earnings();
    }
    
    public String toString()
    {
        return String.format("%s %s; %s $%,.2f" , "base-salaried" , super.toString() , "base salary" , getBaseSalary());
    }
}
public class PayrollSystemTest
{
    public static void main(String args[])
    {
        SalariedEmployee salariedEmployee = new SalariedEmployee("John" , "Smith" , "111-11-1111" , 800.00 );
        HourlyEmployee hourlyEmployee = new HourlyEmployee("Karen" , "Price" , "222-22-2222" , 16.75 , 40);
        CommissionEmployee commissionEmployee = new CommissionEmployee("Sue" , "Jones" , "333-33-3333" , 10000 , .06);
        BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee("Bob" , "Lewis" , "444-44-4444" , 5000 , .04 , 300);
        
        System.out.println("Employees processed individually:\n");
        
        System.out.printf("%s\n%s: $%,.2f\n\n" , salariedEmployee , "earned" , salariedEmployee.earnings());
        System.out.printf("%s\n%s: $%,.2f\n\n" , hourlyEmployee , "earned" , hourlyEmployee.earnings());
        System.out.printf("%s\n%s: $%,.2f\n\n" , commissionEmployee , "earned" , commissionEmployee.earnings());
        System.out.printf("%s\n%s: $%,.2f\n\n" , basePlusCommissionEmployee , "earned" , basePlusCommissionEmployee.earnings());
        
        Employee employees[] = new Employee[4];
        
        employees[0] = salariedEmployee;
        employees[1] = hourlyEmployee;
        employees[2] = commissionEmployee;
        employees[3] = basePlusCommissionEmployee; // assign reference of an subclass object to superclass variable
        
        System.out.println("Employees processed polymorphically:\n");
        
        for(Employee currentEmployee : employees)
        {
            System.out.println(currentEmployee);
            
            if(currentEmployee instanceof BasePlusCommissionEmployee)
            {
                BasePlusCommissionEmployee employee = (BasePlusCommissionEmployee) currentEmployee;
                
                double oldBaseSalary = employee.getBaseSalary();
                employee.setBaseSalary(1.10 * oldBaseSalary);
                
                System.out.printf("new base salary with 10%% increase is : $%,.2f\n" , employee.getBaseSalary());
            }
        }
        
        for(int j = 0 ; j < employees.length ; j++)
        {
            System.out.printf("Employee %d is a %s\n" , j , employees[j].getClass().getName());
        }
    }
}


this two examples are found in java programming book

the first code is that : we can use the straight forward method (invoking a method on a subclass object via superclass reference!) CommissionEmployee3 commissionEmployee2 = basePlusCommissionEmployee;

i have been trying this on the second code ! But it failed and i don't know why ! And so , i also trying the second code method by using downcast method on the first code..~it still failed .. struggling about this for few day ~

appreciate any replying ..~
  • 0

#2 Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guest

Posted 22 January 2010 - 09:43 PM

i think i may found the solution ..~ Actual method that is called depends on the object's type at execution time , a variable can be used to invoke only those methods that are members of that variable's type.

I think im trying to invoke the method which is not found in superclass via superclass reference ..~hehe
  • 0





Also tagged with one or more of these keywords: downcast, polymorphism

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download