1

I have three different classes:

Woman.java

package human;
public class Woman extends Human{
    private final String pnr;
    private final String namn;

    public Woman(String n, String p){
        namn = n;
        pnr = p;
    }
    public String toString(){
        return "My name is "+namn+" and I am a woman.";
    }

}

Man.java

package human;
public class Man extends Human{
    private final String pnr;
    private final String namn;

    public Man(String n, String p){
        namn = n;
        pnr = p;
    }
    public String toString(){
        return "My name is "+namn+" and I am a man.";
    }   
}

Human.java

package human;
public abstract class Human{
    public static Human create(String namn, String pnr){
        char nastsist_char = pnr.charAt(9); // takes second last char in pnr
        String nastsist_string = Character.toString(nastsist_char);
        float siffra = Float.parseFloat(nastsist_string);   //Converts to float
        if ((siffra % 2) == 0){                 //Checks if even
            return new Man(namn, pnr);          
        }
        else{
            return new Woman(namn, pnr);
        }
    }
}

When I try to compile the file Human.java, I get the error

cannot find symbol: class Man,

and

cannot find symbol: class Woman.

When I try to compile the file Man.java or Woman.java, I get the error

cannot find symbol: class Human.

No changing of public/private visibility of the classes have helped. All the files are located in a directory called human.

What is the problem here?

I should add that the goal I'm trying to achieve is to have an abstract class Human that can not be instantiated, and in that class I want to have a create method that returns instances of either Man or Woman depending on pnr. Also, I do not want Man or Woman to be possible to instantiate in any other way than through the create method.

Thanks

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
Sahand
  • 7,980
  • 23
  • 69
  • 137
  • You're going to need to compile the entire project, rather than just singular classes. – Dragondraikk May 12 '15 at 09:46
  • Compile Human first and then compile the other classes. – Ronald Randon May 12 '15 at 09:49
  • 1
    I understand that you are just starting with Java, but still; the names that you are using for your variables could be dramatically improved. "namn", "pnr" are just confusing; why not calling them "firstName" and "lastName" or something alike. And from a design point of view: having an abstract base class that knows its concrete subclasses is not a good idea; and beyond that: when you are already having an abstract base class that represents humans; why do Man and Wife each have namn and pnr? Put those into Human; and provide getters there! – GhostCat May 12 '15 at 09:49
  • I'm not 100% sure but I think an abstract class cannot have a static method. Because the static method uses a static instance of himself and you can't build an instance of an abstract class. So you have to place your factory method in a Factory class and then compile the hole package. – Rene M. May 12 '15 at 09:49
  • Jägermeister: They are called so because I'm taking a course in swedish. I edited the comments so that you can understand them. – Sahand May 12 '15 at 10:04

4 Answers4

4

There's no problem with your code or packaging, so the problem is elsewhere. How do you compile? I'm guessing you might be doing

javac human/Human.java

which will not work, since Man and Woman is then not on the classpath and thus not known to the Java compiler when working on Human. You should include these when compiling:

javac human/*.java

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
0

Do you compile them by yourself, in a terminal or something? Try to compile all files all together then:

javac Man.java Woman.java Human.java
Quoting Eddie
  • 1,501
  • 1
  • 11
  • 14
0

You have cycles in the classes. So you have two choices. 1) Compile all together 2) Create a new class e.g. Factory and instantiate the objects there

It's never a good idea to create cycles in you code

chokdee
  • 446
  • 4
  • 13
0

Have you tried : javac *.java in your folder ?

Java: How can I compile an entire directory structure of code ?

Community
  • 1
  • 1
Gary SEBASTIANI
  • 302
  • 1
  • 15