简体   繁体   中英

Abstract Class and toString() method

I am a little confused on how to set up the TestHomework method so that it prints properly when using the toString() method. Right now when I run the main method it prints "null - 0" but what I would like it to say is "Math - 6". This program is supposed to extend an abstract class. It is supposed to say how many pages there are for homework and for what subject.

public abstract class Homework {

    private int pagesToRead;
    private String typeHomework;

    {
        // initialise instance variables
        pagesToRead = 0;
        typeHomework = "none";
    }

    public Homework(int pages, String hw) {

        this.pagesToRead = pages;
        this.typeHomework = hw;

    }

    public abstract void createAssignment(int p);

    public int getPages() {
        return pagesToRead;
    }

    public void setPagesToRead(int p) {
        pagesToRead = p;
    }

    public String getTypeHomework() {
        return typeHomework;
    }

    public void setTypeHomework(String hw) {
        typeHomework = hw;
    }

}

public class MyMath extends Homework {

    private int pagesRead;
    private String typeHomework;

    public MyMath(int pages, String hw) {
        super(pages,hw);
    }

    public void createAssignment(int p) {
        setTypeHomework("Math");
        setPagesToRead(p);
    }

    public String toString() {
        return typeHomework + " - " + pagesRead;
    }
}

public class TestHomework {

    public static void main(String[] args) {
        MyMath one = new MyMath(6, "Math");
        one.createAssignment(6);
        System.out.println(one);
    }

}

That's because you are defining the 2 properties (that one of them happen to have the same name as one of the abstract class's) but you are not initializing them, you are initializing those of the abstract class. (So their values is always set to their type's default)

You need to drop those from the MyMath class, & define the toString method in your abstract class: it's the one to be used by default by its inheriting classes.

public abstract class Homework {

    private int pagesToRead;
    private String typeHomework;

    // Same code

    // Define the toString here
    @Override
    public String toString() {
        return typeHomework + " - " + pagesToRead;
    }
}

public class MyMath extends Homework {

    // You don't need to define any extra attributes

    public MyMath(int pages, String hw) {
        super(pages,hw);
    }

    public void createAssignment(int p) {
        setTypeHomework("Math");
        setPagesToRead(p);
    }
}


public static void main(String[] args) {
    // Calls the constructor of the MyMath class, which in turn
    // invokes the constructor of its superclass, the 'Homework' class
    MyMath one = new MyMath(6, "Math");
    one.createAssignment(6);

    // Invokes the toString of the MyMath class. Since it does not have one,
    // The toString method of its superclass (Homework) is called.
    System.out.println(one);
}

Your derived class has its own typeHomework and pagesRead fields, which are never set (even though the base class happens to have fields with the same names). Therefore, they stay null and 0 .

You should delete those fields and use the data from the base class, via the public getter methods.

Why it doesn't work:

Be careful you redeclared the attribute typeHomework of you parent class. Attributes are automatically added to your extending class so you don't have to write them again.
By redeclaring it you confused the compiler, viewing your code in debug shows, that your one object contains your typeHomework twice:

typeHomework = null // The one from the super class
typeHomework = "Math" // The one from your child class

Your method now uses the typeHomework from your super-class therefor the output is null!

pagesRead is 0 because you are setting the pagesToRead of your super-class to 6(not pagesRead !) when calling setPagesToRead(p); .


Some style tips

Use the @Override annotation when overriding methods like this:

@Override
public void createAssignment(int p) {
    setTypeHomework("Math");
    setPagesToRead(p);
}

It's not really needed but it's good practice (readers of your code know that it overrides something).

When referring to attributes of your class it's also good practice to use the this statement so it's clear, that you're referring to an attribute and not a local variable:

@Override
public String toString() {
    return this.typeHomework + " - " + this.pagesRead;
}

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