0

I'm trying to get to grips with inheritance. I have a class structure that consists of an interface (Aeroplane), an abstract class(Aeroplane implementation) which implements methods consistent with both classes, and two subclasses: a passenger jet, and a jumbo jet.

passenger and jumbo jet each have a different fuel capacity and hence contain the following field:

private static final int MAX_FUEL_CAPACITY = 49;

I wrote a method addFuel, which enforces certain restrictions:

I originally had the following in each class, but thought it bad practice because there's a lot of duplication:

      Integer addFuel(int litres) {

    if (litres < 1) {

        throw IndexOutOfBoundsException("entered fuel volume must be greater than zero. Zero litres entered.")
    }

    if (fuelInLitres == MAX_TANK_CAPACITY) {

        throw IndexOutOfBoundsException("tank is at full capacity. Zero litres entered.") 

    }

    if (litres + fuelInLitres > MAX_TANK_CAPACITY) {

        throw IndexOutOfBoundsException("entered fuel volume is too large - tank does not have sufficient capacity. Zero litres entered.")
    }       

    fuelInLitres += litres; 
}

So I ended up with the following in my abstract class:

 Integer addFuel(int litres) {

    if (litres < 1) {

        throw IndexOutOfBoundsException("entered fuel volume must be greater than zero. Zero litres entered.")
    }

}

And this in each subclass, but I can't see that's right either?

     Integer addFuel(int litres) {

    super.addFuel(litres) 

    if (fuelInLitres == MAX_TANK_CAPACITY) {

        throw IndexOutOfBoundsException("tank is at full capacity. Zero litres entered.") 

    }

    if (litres + fuelInLitres > MAX_TANK_CAPACITY) {

        throw IndexOutOfBoundsException("entered fuel volume is too large - tank does not have sufficient capacity. Zero litres entered.")
    }       

    fuelInLitres += litres; 
}
2
  • Will the addFuel method implementation remain the same for the two subclasses? Commented Mar 7, 2015 at 14:41
  • Yes, the logic would never change, and the MAX_TANK_CAPACITY field would always be different for each subclass Commented Mar 7, 2015 at 14:48

3 Answers 3

2

You should add a getMaxFuelCapacity() method in your base class and have two different implementations for it in the two subclasses. Then you'll be able to move all of the remaining logic in addFuel() to the base class.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that makes logical sense. - so something like private static final int MAX_FUEL_CAPACITY = 49; Integer getFuelCapacity() { return MAX_FUEL_CAPACITY; } in each subclass but with different max values?
Yes, exactly. Note that you should use the int type for the numbers, not Integer.
1

keep addFuel() method in your Abstract Class ,

Just Create an additional abstract method in your Abstract Class

public int getMaxFuelCapacity();// Call to this when you need MaxFuelCapacity

Provide required implementation of this method in your subclasses

In addFuel method Call getMaxFuelCapacity()

 Integer addFuel(int litres) {
     int maxCapacity=getMaxFuelCapacity();
     // Do whatever you want to do 
 }

2 Comments

Ah, thank you! Would getMaxFuelCapacity() not need to sit within my subclasses, not my abstract class as that's where I have seperate values for each i.e. private static final int MAX_FUEL_CAPACITY = 49; for passenger jet and private static final int MAX_FUEL_CAPACITY = 60; for jumbo jet? So those values are different...
You need to provide implementation of getMaxFuelCapacity in each of your subclass in the way you want .... I provide it by returning MAX_FUEL_CAPACITY accordingly
0

You should add a method in base class Integer addFuel(int litres, int MaxCapacity) and move whole logic from subclass to the addFuel(int litres, int MaxCapacity) method of base class and in the subclass add new method Example

getMaxFuel(int litres){
 addFuel(litres, MaxCapcity)
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.