0

Super class is not functioning, as when the Steelfactory class try's to get the data from the super class, the output shows that there is noting there. Looking to solves as to why this might be happening. The Factory called null with 0makes cars at a rate of 0since the year 1998 I am wondering if it is due to, having called my super class wrong, but I did not gain any errors when writing it. Or could there there be another issue in the code logic wise? The code:

package finalsproject;

static abstract class Factory {
    //Atributes: 
    String factoryName; //The name of the factory
    int employeeAmount; // number of workers in the factory
    int producedAmount; // number of products made
    // Constructor: 
    public Factory (String ifactoryName,int iemployeeAmount,int iproducedAmount) {// Prameterized consrtuctor
        factoryName = ifactoryName;
        employeeAmount = iemployeeAmount;
        producedAmount = iproducedAmount;
    }
    //Methods: 
    public abstract String getFactoryName();
    public abstract int getEmployeeAmount();
    public abstract int getProducedAmount();
        
}
       
//The class SteelFactory must have the following specifications
//-It must implement the abstract class Factory
//Make these two classes implement the abstract class above
    
static class SteelFactory extends Factory {
    //Attributes:
    String factoryName; // Name of the factory
    int employeeAmount; // Number of workers
    int producedAmount; // number of products
    int yearCreated; // the year the factory was made
    
    //Constructor: 
    public SteelFactory (String ifactoryName,int iemployeeAmount,int iproducedAmount,int iyearCreated) {
        super ( ifactoryName, iemployeeAmount,iproducedAmount);
        yearCreated = iyearCreated;
    }
    // Methods
    public String getFactoryName() {
        return (factoryName);
    }
    public int getEmployeeAmount() {
        return (employeeAmount);
        
    }
    public int getProducedAmount() {
        return (producedAmount);
    }
    public String toString () {
        return ("The Factory called " + factoryName + " with " + employeeAmount + "makes cars at a rate of " + producedAmount + "since the year "+ yearCreated);
    }
    
}   
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Tusk
  • 7
  • 1

1 Answers1

0

You can resolve this by simply removing the duplicate variables factoryName, employeeAmount and producedAmount from your SteelFactory class, otherwise the class will use the local variables that were never initialized instead of the correct variables from the super class. One big reason for extending a class is so that we don't have to re-use/re-type the same variables and methods in multiple classes.

Also, don't forget to use @Override annotation, it helps you keep track of which methods have been extended and helps prevent common mistakes.

Working code as follows:

    static class SteelFactory extends Factory {
        //Attributes:
        int yearCreated; // the year the factory was made
        
        //Constructor: 
        public SteelFactory (String ifactoryName,int iemployeeAmount,int iproducedAmount,int iyearCreated) {
            super ( ifactoryName, iemployeeAmount,iproducedAmount);
            yearCreated = iyearCreated;
        }
        // Methods
        @Override
        public String getFactoryName() {
            return (factoryName);
        }
        @Override
        public int getEmployeeAmount() {
            return (employeeAmount);
        }
        @Override
        public int getProducedAmount() {
            return (producedAmount);
        }
        @Override
        public String toString () {
            return ("The Factory called " + factoryName + " with " + employeeAmount + "makes cars at a rate of " + producedAmount + "since the year "+ yearCreated);
        }
    }   

Then simply use:

SteelFactory test = new SteelFactory("Test", 78, 26, 2022);
System.out.println(test.toString());

Will correctly print the following result (You'll need to fix your formatting to include spaces):

The Factory called Test with 78makes cars at a rate of 26since the year 2022
sorifiend
  • 5,927
  • 1
  • 28
  • 45