0

I'd like to separate some polymorphism example from the book into several files, but got an error with main class PoliWithClasses while creating objects emp1 and emp2 of classes Programmer and Manager. Please let me know what is wrong to make run the following code

File Employee.java

package po;

abstract class Employee {
    public void reachOffice() {
        System.out.println("reached Office - India");
    }
public abstract void startProject();
}

File Programmer.java

package po;

class Programmer extends Employee {
public void startProject(){
    defineClasses();
    unitTestCode();
}
private void defineClasses() {System.out.println("define classes");}
private void unitTestCode() {System.out.println("unit Test Code");}
}

File Manager.java

package po;

class Manager extends Employee {
    public void startProject() {
        meetingWithCustomer();
        defineProjectSchedule();
        assignRespToTeam();
    };
    private void meetingWithCustomer() {System.out.println("meet Customer");}
    private void defineProjectSchedule() {System.out.println("define Project Schedule");}
    private void assignRespToTeam() {System.out.println("assign Resp To Team");}
}

File PoliWithClasses.java

package po;

public class PoliWithClasses {
    public static void main(String arg[]) {
    }
Employee emp1=new Programmer();
Employee emp2=new Manager();

emp1.reachOffice();
emp2.reachOffice();

emp1.startProjectWork();
emp2.startProjectWork();
}

Thanks, I have corrected the typo but still looks like these objects are not visible in main class:

emp1.reachOffice(); 
emp2.reachOffice();

emp1.startProject(); 
emp2.startProject();
5
  • What error did you get? Commented Nov 30, 2014 at 13:35
  • 1
    Is your method supposed to be called startProject() or startProjectWork() ? Commented Nov 30, 2014 at 13:36
  • Error:Exception in thread "main" java.lang.Error: Unresolved compilation problem: at po.PoliWithClasses.main(PoliWithClasses.java:4) Commented Nov 30, 2014 at 13:47
  • Also I see these errors with created emp1 and emp2 objects: Syntax error on token "reachOffice", Identifier expected after this token Syntax error on token "startProject", Identifier expected after this token Commented Nov 30, 2014 at 13:48
  • You put your main code outside the main method in that snippet. Commented Nov 30, 2014 at 14:56

2 Answers 2

1

Your comments on the given error message suggests this is the actual issue:

public class PoliWithClasses {
    public static void main(String arg[]) {
    //} <-- your bracket was here
        Employee emp1=new Programmer();
        Employee emp2=new Manager();

        emp1.reachOffice();
        emp2.reachOffice();

        emp1.startProject(); // use consistent names
        emp2.startProject();
    } // the code must be contained in the method
}

To sum it up:

  1. The first issue you found was an improper method call in the main code. This is not an issue related to polymorphism, since the method prototype of startProject in the subclasses matches with the one in Employee.
  2. The actual code you wished to have executed was not inside the scope of main. The declarations of emp1 and emp2 were being interpreted as package-scoped field declarations. However, emp1.reachOffice() and emp2.reachOffice() are both illegal outside of a method's implementation.

I will also add a recommendation: use the @Override annotation every time a method is overridden in a child class (it makes sure that an override took place). For example, in your Programmer class:

class Programmer extends Employee {

    @Override
    public void startProject(){
        defineClasses();
        unitTestCode();
    }

    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have mistake in your code. Not:

emp1.startProjectWork(); emp2.startProjectWork();

but:

emp1.startProject(); emp2.startProject();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.