0

I got the follow errors:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    groeße cannot be resolved or is not a field
    geschlaecht cannot be resolved or is not a field
    groeße cannot be resolved or is not a field
public static void main(String[] args) {
    person Emil = new person();
    Emil.name = "Emil";
    Emil.alter = 22;
    Emil.groeße = 18;
    Emil.geschlaecht = "maennlich";
    System.out.println("Emil: " + Emil + "Alter" + Emil.alter + "Name:" + Emil.name + "Größe" + Emil.groeße);
}

public class person{
    public String name;
    public byte alter;
}

public class Eigenschaften extends person {
    public byte groeße;
    public String geschlaecht;
}

I tried to fix it as per other users' comments. Now emil is an Eigenschaften

New error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    No enclosing instance of type QuizFrage is accessible. Must qualify the allocation with an enclosing instance of type QuizFrage (e.g. x.new A() where x is an instance of QuizFrage).

    at QuizFrage.main(QuizFrage.java:5)
public class QuizFrage {

    public static void main(String[] args) {
    Eigenschaften emil = new Eigenschaften();
    emil.name = "Emil";
    emil.alter = 22;
    emil.groeße = 18;
    emil.geschlaecht = "maennlich";
    System.out.println("Emil: " + emil + " Alter" + emil.alter + " Name:" + emil.name + " Größe" + emil.groeße);
    
    }

    class Person{
        public String name;
        public byte alter;
    }
    class Eigenschaften extends Person {
        public byte groeße;
        public String geschlaecht;
    }

}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 1
    An `Eigenschaften` is a `person`, but a `person` is not an `Eigenschaften`, so a `person` doesn't have `Eigenschaften`'s fields. You probably meant to declare `Emil` (which should be `emil`, btw) as a `Eigenschaften` instead of a `person` (which should be `Person` btw) – Federico klez Culloca Jun 02 '21 at 17:07
  • I did what you said, but I got the same errors – Max Duschanek Jun 02 '21 at 17:35
  • Please [edit] your question to show what you did, because you can't be getting the *same* errors if you did what I said. Also, the error you're getting means you run your program even if it didn't compile in the first place. There's almost never a reason to that. Compile first. Then, if it compiles, run. – Federico klez Culloca Jun 02 '21 at 18:00
  • The new error is because those inner classes need to be `static`. See [this other question](https://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible). Or, as suggested in the answers, put them outside the `QuizFrage` class. – Federico klez Culloca Jun 02 '21 at 18:28
  • As an aside, stackoverflow is not a substitute for learning to search for errors on a search engine on your own. Please take a look at [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922) – Federico klez Culloca Jun 02 '21 at 18:30

3 Answers3

1

to be honest, your code would definitely show a compilation error for the code you wrote.

Check out the below code. Make sure to follow the coding standards as the class name Should start with a capital letter.

public class Test {
    public static void main(String[] args) {
        Eigenschaften Emil = new Eigenschaften();
        Emil.name = "Emil";
        Emil.alter = 22;
        Emil.groeße = 18;
        Emil.geschlaecht = "maennlich";
        System.out.println("Emil: " + Emil + " Alter " + Emil.alter + " Name: " + Emil.name + " Größe :" + Emil.groeße);
    }
}
class Person{
    public String name;
    public byte alter;
}
class Eigenschaften extends Person {
    public byte groeße;
    public String geschlaecht;

    Eigenschaften(){
        
    }
}

Jaypal Sodha
  • 2,238
  • 2
  • 10
  • 22
0

In this case, just need to add a constructor for the subclass which is Eigenschaften and create the object from there. My modifications:

  • Also Save these three as separate java files
public class PersonDetails
{
    public static void main(String[] args) {
        Eigenschaften Emil = new Eigenschaften();
        Emil.name = "Emil";
        Emil.alter = 22;
        Emil.groeße = 18;
        Emil.geschlaecht = "maennlich";
        System.out.println(" Emil: " + Emil + " Alter " + Emil.alter + " Name: " + Emil.name + " Größe " + Emil.groeße);
    }
}

class Person {
    public String name;
    public byte alter;
}

class Eigenschaften extends Person {
    public byte groeße;
    public String geschlaecht;
    
    public Eigenschaften(){}

}

After that compile these three classes separately according to the order Person.java,Eigenschaften.java and PersonDetails.java

After compiling Run the main class

java PersonDetails

Then you can the resultant output as

Emil: Eigenschaften@15db9742 Alter 22 Name: Emil Gr????e 18

Kaviranga
  • 576
  • 2
  • 10
  • 24
Antonio
  • 422
  • 4
  • 13
0

You already got some answers, but I thought I would give you some additional tips.

(1) Class names are always with a capital letter. So you do not have a class person but Person.

(2) In my opinion it is a better way to implement one class per file, so I would create one directory containing a file Person.java only containing the class Person and so on. Of course you can also use inner classes and they can make sense, but not for this beginning code.

(3) NEVER program in german. We have these weird letters ä,ö,ü,ß which are not known in english and can give you some ugly question marks or other unknown symbols when printing on terminal. Also this may lead to some weird effects when accessing fields. So please use english language.

(4) I think you got a wrong understanding of OOP at this point. Eigenschaften (for all native-english people on Stackoverflow: Properties) are not a subclass of person, because it does not make sense. OOP is for example: You have a superclass Animal and then you have subclasses Dog, Cat, Bird and so on. Now all types of animals have the same behaviors. For example they can all speak. A dog barks, a cat mious and a bird does something else :D I don't know the english term for that.

Then you would have a structure like this:

public interface Animal {
    public void makeSound();
}

// For example for the dog:
public class Dog implements Animal {
    public void makeSound() {
        System.out.println("Woof");
    }
}

In this case I used an interface. You can also just use a normal or an abstract class. That always depends on your needs.

So in your case you would just have a class person like this:

public class Person {
    public String name;
    public byte age;
    public byte size;
    public String gender;

    //Constructor, getters, setters, toString
}

(5) And the last improvement: You do not need to write something like this: System.out.println("Emil: " + emil + " Alter" + emil.alter + " Name:" + emil.name + " Größe" + emil.groeße);

Just implement a toString-method in class Person. Then you can simplify this command to

System.out.println(emil);

where emil is an object of type Person. The toString method will be used automatically.

F_Schmidt
  • 902
  • 1
  • 11
  • 32