2

I'm a bit confused with subclasses. Here's my code:

public class MedHistory {

    private String grafts;
    private String allergies;
    private String diseases;
    private String surgeries;
    private String medicalTreatment;

    //Constructors (#2)

    public MedHistory(String allergies, String diseases, String grafts,
            String treatments, String surgeries) {
        this.allergies=allergies;
        this.diseases=diseases;
        this.grafts=grafts;
        this.medicalTreatment=treatments;
        this.surgeries=surgeries;
    }

    public MedHistory() {
        this.allergies="";
        this.diseases="";
        this.grafts="";
        this.medicalTreatment="";
        this.surgeries="";
    }

    //Getters 
    public String getGrafts() {
        return grafts;
    }

    public String getAllergies() {
        return allergies;
    }

    public String getDiseases() {
        return diseases;
    }

    public String getSurgeries() {
        return surgeries;
    }

    public String getMedicalTreatment() {
        return medicalTreatment;
    }

    //Setters 

    public void setGrafts(String grafts) {
        this.grafts = grafts;
    }

    public void setAllergies(String allergies) {
        this.allergies = allergies;
    }

    public void setDiseases(String diseases) {
        this.diseases = diseases;
    }

    public void setSurgeries(String surgeries) {
        this.surgeries = surgeries;
    }

    public void setMedicalTreatment(String medicalTreatment) {
        this.medicalTreatment = medicalTreatment;
    }

    public class FemMedHistory extends MedHistory {

        private List<Birth> births = new ArrayList<Birth>();

        //Constructors (#2)

        public FemMedHistory(String allergies, String diseases, String grafts,String treatments, String surgeries, List<Birth> birthlist) {
            super(allergies,allergies,grafts,treatments,surgeries);
            this.births=birthlist;
        }

        public FemMedHistory() {
            super();
            this.births=null;
        }

        //Getter

        public List<Birth> getBirths() {
            return this.births;

        }

        //Setter

        public void setBirths(List<Birth> list) {
            this.births=list;
        }

    }
}

When I try to create an new FemMedHistory object like this:

List<Birth> list = new ArrayList<Birth>();
list.add(new Birth(new GregorianCalendar(2011,4,10),"kaisariki",4));
FemMedHistory female = new FemMedHistory("allergia2","astheneia2","emvolia2","farmekeutiki agwgi2", "xeirourgeia2", list);

I get the error:

No enclosing instance of type MedHistory is accessible. Must qualify the allocation with an enclosing instance of type MedHistory (e.g. x.new A() where x is an instance of MedHistory).

So, which is the right way to use a subclass?

informatik01
  • 16,038
  • 10
  • 74
  • 104
lephleg
  • 1,724
  • 2
  • 21
  • 41

3 Answers3

5

When you declare a nested class it only available through the Outer class.

To access it outside, you will need to either make the FemMedHistory class static.

public static class FemMedHistory extends MedHistory {...}

access it through the MedHistory class

MedHistory.FemMedHistory myMedHistory = ...

or declare it in it's own Java file.

greedybuddha
  • 7,488
  • 3
  • 36
  • 50
  • I cannot change the files, so can you tell me briefly what would be the cons of making FemMedHistory a static class? – lephleg May 29 '13 at 20:02
  • 1
    Not much honestly, it clutters the global namespace a bit, which in this case you want as it's really a class you would normally define in it's own file. Usually if you make a class inside of another class, it's because you don't really care about that inside class outside of that java file. Essentially they are usually utility classes that have no bearing on the rest of the program. But that's obviously not the case here. – greedybuddha May 29 '13 at 20:08
3

You have declared your subclass as an inner class, which means that you can't create an instance of it without first creating an instance of the containing class.

The most common way to solve this is to declare it as a separate class, which would get rid of your error.

Keppil
  • 45,603
  • 8
  • 97
  • 119
3

Long story short: cut all the FemMedHistory code and paste it into FemMedHistory.java. The way it is now you have involved Java concepts which you have not yet mastered. Also, that class really does belong in a separate file.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436