javasubclasstostringsuperclassabstract-methods

Superclass overriding Subclass with default values from constructor in Java


I have an assignment and my superclass default values always override the values I pass in the Test main method. In the debugger, i see the passing of the productNumber(1234) and productTitle("Daughter"), but then it's overridden with the default values. Any thoughts, i keep making minor changes, checking for changes, still the same results.

Product Superclass

public abstract class Product {
    private int productNumber;
    private String productTitle;
   
//Two constructors required
    public Product(){
        productNumber = 0;
        productTitle = "";
    }
    
    public Product(int productNumber, String productTitle) {
        this.productNumber = productNumber;
        this.productTitle = productTitle;
    }
  

    public void setProductNumber(int productNumber) {
        this.productNumber = productNumber;
    }

    public int getProductNumber() {
        return productNumber;
    }

    public void setProductTitle(String productTitle) {
        this.productTitle = productTitle;
    }
        
    public String getProductTitle() {
        return productTitle;
    }

    //Override toString() required
    @Override
    public String toString() {
        return productNumber + " " + productTitle;
    }
    
    // Required Product class declares abstract method with this signature: public String getDisplayText()
    public abstract String getDisplayText();
    
    //Override equals() required
    @Override
    public boolean equals(Object object) {
        if (object instanceof Product)  {
            Product product2 = (Product) object;
            if (productNumber == (product2.getProductNumber()) &&
                productTitle.equals(product2.getProductTitle())){
                    return true;
            }
        }
        return false;
    }  
}

Music Subclass extends Product Superclass

public class Music extends Product {
    private String artist;
    private String style;
    private String medium;

    public Music() {
        super();
        artist = "";
        style = "";
        medium = "";
    }

    public Music(int productNumber, String productTitle, String artist, String style, String medium) {
        super();
        this.artist = artist;
        this.style = style;
        this.medium = medium;
    }


    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }
    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getMedium() {
        return medium;
    }

    public void setMedium(String medium) {
        this.medium = medium;
    }

    @Override
    public String getDisplayText() {
        return super.toString() + " by " + artist + " " + style + " " + medium;
    }
 
    @Override
    public boolean equals(Object object){
        if (object instanceof Music){
            Music m = (Music) object;
            if (artist.equals(m.getArtist()) &&
                style.equals(m.getStyle()) &&
                medium.equals(m.getMedium())){
                    return true;
            }
        }
        return false;
    }
}

Print String

public class Test {

    public static void main(String[] args) {
        // Expected result: 1234 Daughter by Pearljam Alternative online
        Music music1 = new Music(1234,"Daughter", "Pearljam","Alternative","online");
        System.out.println(music1.getDisplayText());
    }
    
}

Solution

  • you are not passing values from subclass to your parentclass

    instead of super() you need to do below -

    super(productNumber,productTitle);
    

    update needed in your code :-

    public Music(int productNumber, String productTitle, String artist, String style, String medium) {
            super(productNumber,productTitle);
            this.artist = artist;
            this.style = style;
            this.medium = medium;
        }