Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java- Getters Returning Null

I am working on an abstract superclass exercise for class, and I am stuck on why my getters are returning null. According to how my instructor demonstrated, the setters are overridden in the subclasses, and the getters are to be final methods in the superclass. The getters work when I move them to the subclass, so I am guessing that it has something to do with the fact that they are in separate classes, but as I am brand new to object oriented programming, I have no idea if that is why and/or how to go about correcting this.

Abstract Superclass:

public abstract class ProgrammingCourse {

    private double credits;
    private  String courseName;

    public final double getCredits() {
        return credits;
    }

    public abstract void setCredits(double credits);

    public final String getCourseName() {
        return courseName;
    }

    public abstract void setCourseName(String courseName);

Sample Subclass:

  public class AdvancedJavaCourse extends ProgrammingCourse {

    private double credits;
    private String courseName;

    @Override
    public void setCourseName(String courseName) {
        if (courseName == null || courseName.length() == 0) {
            JOptionPane.showMessageDialog(null,
                    "Error: courseName cannot be null or empty");
            System.exit(0);
        }
        this.courseName = courseName;
    }

    @Override
    public void setCredits(double credits) {
        this.credits = credits;
    }

How I am trying to work with this in my main:

     ProgrammingCourse[] courses = {new AdvancedJavaCourse(), 
     new IntroJavaCourse(), new IntroToProgrammingCourse()};

    courses[0].setCourseName("adv Java");
    courses[1].setCourseName("Intro Java");
    courses[2].setCourseName("Intro Programming");
    for (ProgrammingCourse pc : courses) {
        System.out.println(pc.getCourseName());
    }

    AdvancedJavaCourse advJava = new AdvancedJavaCourse();
    advJava.setCourseName("Advanced Java");

    System.out.println(advJava.getCourseName());
like image 756
TeamNormal Avatar asked Aug 05 '26 10:08

TeamNormal


2 Answers

There's a few issues with your code. Starting with AdvancedJavaCourse you should remove the fields credits and courseName. These are accessible from ProgrammingCourse.

Now that you only have one declaration for each of those fields, you should either mark the fields as protected inside of ProgrammingCourse or create some getters/setters, so that you can alter their value from AdvancedJavaCourse.

like image 191
DominicEU Avatar answered Aug 08 '26 00:08

DominicEU


You must avoid declaring fields with the same name in super and sub-classes.

This is what is called variable/field shadowing. Even if this is allowed by the language it should be avoided.

What is going on here is that instance of AdcanceJavaCourse have a total of 4 fields two pairs of credit and courseName, the ones declared by ProgrammingCourse and the ones declared in in AdvanceJavaCourse itself.

The code in ProgrammingCourse will read and write in the variables declared in that class whereas the code in AdvanceJavaCourse will access the fields declared in that class.

Probably what you want to do here is to remove the field declaration in AdvanceJavaCourse and declared the fields in ProgrammingCourse as protected.

Another possibility is to declare those fields as private in ProgramingCourse, give a simple body to the setters in ProgramingCourse:

  public void setField(Type value) {
     field = value;
  }

Any specialized overrides in those setter or getter is sub-classes can access the private fields by using the parent setter getters with the super. prefix:

@Override 
public void setField(Type value) {
    ...
    super.setField(processedValue);
    ...
}
like image 21
Valentin Ruano Avatar answered Aug 07 '26 23:08

Valentin Ruano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!