11 Replies Latest reply: Jan 6, 2009 11:48 AM by 843785 RSS

    Problem with Inheritance (Cannot find symbol)

    843785
      Hello all,

      I'm having a problem with my subclass. It says

      symbol : constructor Vehicle()
      location: class alpha.Vehicle


      Here is the subclass:
      package alpha;
      
      
      public class Bus extends Vehicle
      {
          private int busCount;
          private String busType;
          
          public Bus(int aBusCount, String aBusType)
          {
              
              setBusCount(aBusCount);
              setBusType(aBusType);
          }
      
          public int getBusCount()
          {return busCount;}
          public String getBusType()
          {return busType;}
          
          public void setBusCount(int newBusCount)
          {busCount = newbusCount;}
          public void setBusType(String newBusType)
          {busType = newBusType;}
      }
      The parent class has a few get and set methods such as setting engine size and registration. Can anyone please help me?

      Thank you for your time
        • 1. Re: Problem with Inheritance (Cannot find symbol)
          843785
          Your constructor
          public Bus(int aBusCount, String aBusType)
              {
                  
                  setBusCount(aBusCount);
                  setBusType(aBusType);
              }
          is implicitly calling a constructor of its subclass, as if you had written:
          public Bus(int aBusCount, String aBusType)
              {
                  super();
                  setBusCount(aBusCount);
                  setBusType(aBusType);
              }
          But super class Vehicle lacks a no-argument constructor.

          Solution: either add that constructor to Vehicle, or call an existing constructor:
          public Bus(int aBusCount, String aBusType)
              {
                  super(arg1, ..., argN); //as appropriate
                  setBusCount(aBusCount);
                  setBusType(aBusType);
              }
          • 2. Re: Problem with Inheritance (Cannot find symbol)
            843785
            Thank you for the reply. Please bare with me I'm not sure what to do. I added:
            package alpha;
             
             
            public class Bus extends Vehicle
            {
                private int busCount;
                private String busType;
                
                public Bus(int aBusCount, String aBusType)
                {
                    super (aVehicleRegistration, aVehicleAvailability, 
                        aVehicleCount, aVehicleEngineSize, aVehicleMileage);
                    setBusCount(aBusCount);
                    setBusType(aBusType);
                }
             
                public int getBusCount()
                {return busCount;}
                public String getBusType()
                {return busType;}
                
                public void setBusCount(int newBusCount)
                {busCount = newbusCount;}
                public void setBusType(String newBusType)
                {busType = newBusType;}
            }
            Am I supposed to add anything to my vehicle class because I'm still getting an error. Thank you again.
            • 3. Re: Problem with Inheritance (Cannot find symbol)
              3004
              ButcherBay wrote:
              Am I supposed to add anything to my vehicle class because I'm still getting an error. Thank you again.
              Please paste in the exact, complete error message, and indicate clearly which line is causing it.

              It would probably also help to be able to see the parent class.
              • 4. Re: Problem with Inheritance (Cannot find symbol)
                843785
                ButcherBay wrote:
                Thank you for the reply. Please bare with me ...
                I tend to clear the room when I do that :-(
                • 5. Re: Problem with Inheritance (Cannot find symbol)
                  796447
                  super (aVehicleRegistration, aVehicleAvailability, aVehicleCount, aVehicleEngineSize, aVehicleMileage);
                  Where are the symbols named 'aVehicleRegistration' etc? I don't see them, and I imagine the compiler doesn't either. It won't wave a magic wand, you need to pass that constructor some data.
                  • 6. Re: Problem with Inheritance (Cannot find symbol)
                    843785
                    OK this is the parent class.
                    package alpha;
                    
                    public class Vehicle 
                    {
                        private String vehicleRegistration;
                        private String vehicleAvailability;
                        private int    vehicleCount;
                        private float  vehicleEngineSize;
                        private int    vehicleMileage;
                        
                        public Vehicle (String aVehicleRegistration, String aVehicleAvailability, 
                                int aVehicleCount, float  aVehicleEngineSize, int aVehicleMileage)
                        {
                            setVehicleRegistration(aVehicleRegistration);
                            setVehicleAvailability(aVehicleAvailability);
                            setVehicleCount(aVehicleCount);
                            setVehicleEngineSize(aVehicleEngineSize);
                            setVehicleMileage(aVehicleMileage);
                        }
                        
                        public String getVehicleRegistration()
                        {return vehicleRegistration;}
                        public String getVehicleAvailability()
                        {return vehicleAvailability;}
                        public int getVehicleCount()
                        {return vehicleCount;}
                        public float getVehicleEngineSize()
                        {return vehicleEngineSize;}
                        public int getVehicleMileage()
                        {return vehicleMileage;}
                        
                        
                        public void setVehicleRegistration(String newVehicleRegistration)
                        {vehicleRegistration = newVehicleRegistration;}
                        
                        public void setVehicleAvailability(String newVehicleAvailability)
                        {vehicleAvailability = newVehicleAvailability;}
                    
                        public void setVehicleCount(int newVehicleCount)
                        {vehicleCount = newVehicleCount;}
                    
                        public void setVehicleEngineSize(float newVehicleEngineSize)
                        {vehicleEngineSize = newVehicleEngineSize;}
                    
                        public void setVehicleMileage(int newVehicleMileage)
                        {vehicleMileage = newVehicleMileage;}
                        
                    }
                    This is the error. It has 13 of them. All of them are at where I added the 'super'.
                    D:\beansdos\RentalService\src\alpha\Bus.java:20: cannot find symbol
                    symbol  : variable aVehicleRegistration
                    location: class rentalservice.Bus
                            super (aVehicleRegistration, aVehicleAvailability,
                    • 7. Re: Problem with Inheritance (Cannot find symbol)
                      843785
                      As mentioned, you need to pass those values to your Bus constructor:
                       public Bus(String aVehicleRegistration, String aVehicleAvailability,  int aVehicleCount, float  aVehicleEngineSize, int aVehicleMileage, int aBusCount, String aBusType)  {
                              super (aVehicleRegistration, aVehicleAvailability,  aVehicleCount, aVehicleEngineSize, aVehicleMileage);
                              setBusCount(aBusCount);
                              setBusType(aBusType);
                      }
                      I'm not a fan of constructors that take so many parameters. It's usually when I get pass four that I give up and just call setters in the client code.
                      • 8. Re: Problem with Inheritance (Cannot find symbol)
                        3004
                        ButcherBay wrote:
                        D:\beansdos\RentalService\src\alpha\Bus.java:20: cannot find symbol
                        symbol  : variable aVehicleRegistration
                        location: class rentalservice.Bus
                        super (aVehicleRegistration, aVehicleAvailability,
                        As warnerja surmised, you're using variables that don't exist. Tell me, what value do you thing aVehicleRegistration has there, and where did it get it from?
                        • 9. Re: Problem with Inheritance (Cannot find symbol)
                          843785
                          Sorry I jumped the gun and provided the code solution, but it's chilly standing here, naked.
                          • 10. Re: Problem with Inheritance (Cannot find symbol)
                            843785
                            haha thank you non the less. You got it working for me :)
                            • 11. Re: Problem with Inheritance (Cannot find symbol)
                              796447
                              ButcherBay wrote:
                              haha thank you non the less. You got it working for me :)
                              You like those oopsies don't you. First you're baring yourself, and now you're giving BDLH a non thank you. Ah, so that's why your username has 'butcher' in it.