简体   繁体   中英

java polymorphism code. level newbie

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();

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();
    }

    // ...
}

You have mistake in your code. Not:

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

but:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM