Results 1 to 3 of 3
  1. #1
    thomason93 is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Problem with method invocation

    I have the following code. I am trying to invoke some methods in a for loop in order to print some info stored in a List. But for some reason, compiler pops a message saying "cannot find symbol - method getEmpID(). You are using a symbol here (a name for a variable, method or class) that has not been declared in any visible scope." But I am pretty sure that method getEmpID (as also getName(), getAfm(), and payment() ) have been declared as public. I would really appreciated if someone could delight me about what is wrong in this case.

    Note: My List contains objects of different type (SalariedEmployee, HourlyEmployee). I hope this is not the factor causing this problem.

    Java Code:
    import java.util.*;
    
    
    abstract class Employee{
        
        private String name = "";
        private String afm = "";
        private long EmpID;
        static long count=0;
        
        public void setName(String n){name=n;}
        public String getName(){return name;}         //THERE IS getName()
        public void setAfm(String a){afm=a;}
        public String getAfm(){return afm;}              //THERE IS getAfm()
        public void setEmpID(long id){EmpID=id;}
        public long getEmpID(){return EmpID;}         //THERE IS getEmpID()
        abstract int payment();
    }
    
    class SalariedEmployee extends Employee{
        
        private int salary=0;
        
        public SalariedEmployee(){
            count=count+1;
            this.setEmpID(count);
            
        }
        
        public void setSalary(int s){salary=s;}
        public int payment(){return salary;}                   //THERE IS payment()
        
    }
    
    class HourlyEmployee extends Employee{
        
        private int hoursWorked=0;
        private int hourlyPayment=0;
        
        public HourlyEmployee(){
            count=count+1;
            this.setEmpID(count);
            
        }
        
        public void setHoursWorked(int hw){hoursWorked=hw;}
        public int getHoursWorked(){return hoursWorked;}
        public void setHourlyPayment(int hp){hourlyPayment=hp;}
        public int getHourlyPayment(){return hourlyPayment;}
        public int payment(){return hoursWorked*hourlyPayment;}           //THERE IS payment()
    }
        
    
    public class Main{
        
        public static void main(String args[]){                        //MAIN IS HERE
            
            SalariedEmployee emp1 = new SalariedEmployee();              //
            HourlyEmployee emp2 = new HourlyEmployee();                  //  HERE I
            ArrayList<Object> empList = new ArrayList<Object>();        //  CREATE
            empList.add(emp1);                                          //  MY OBJECTS
            empList.add(emp2);                                                    
            
            emp1.setName("First Employee");
            emp1.setAfm("7777777");                                 //HERE I GIVE 
            emp1.setSalary(20000);                                 //INITIAL VALUES
            emp2.setName("Second Employee");                       //TO OBJECTS
            emp2.setAfm("5555555");                                            
            emp2.setHoursWorked(1000);
            emp2.setHourlyPayment(40);
            
            for(Object obj: empList){
                
                System.out.println("Employee ID: "+obj.getEmpID());            // THESE 
                System.out.println("Employee Name: "+obj.getName());          // 4 LINES
                System.out.println("Employee AFM: "+obj.getAfm());            //  HAVE
                System.out.println("Employee Payment: "+obj.payment());      //  THE PROBLEM
            }
        
            
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Problem with method invocation

    Instead of creating a List<Object> create a List<Employee>. You can add any subclass of Employee to the list. But you will be constrained to use only the methods defined in Employee. If you override the Employee methods in your subclass, then the subclass' version will be used.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    thomason93 is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Problem with method invocation

    Quote Originally Posted by jim829 View Post
    Instead of creating a List<Object> create a List<Employee>. You can add any subclass of Employee to the list. But you will be constrained to use only the methods defined in Employee. If you override the Employee methods in your subclass, then the subclass' version will be used.

    Regards,
    Jim
    I replaced List<Object> with List<Employee> as you said and now everything is working fine! No non-overriden methods of Employee's subclasses required in for loop, so it's not causing a problem. Thank you very much for your help :)

Similar Threads

  1. Java Remote Method Invocation (RMI) with Secure Socket Layer (SSL)
    By smhumayun in forum Reviews / Advertising
    Replies: 3
    Last Post: 07-04-2013, 09:14 PM
  2. Generic method invocation Clarification required
    By muzu1232005 in forum Advanced Java
    Replies: 7
    Last Post: 04-24-2011, 10:59 PM
  3. RMI (remote method invocation) Java
    By Stijn_vdd in forum Networking
    Replies: 1
    Last Post: 03-03-2011, 05:14 PM
  4. HELP: Method Invocation
    By rjuyal in forum Advanced Java
    Replies: 4
    Last Post: 04-07-2008, 11:07 AM
  5. Replies: 2
    Last Post: 06-12-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •