javapalava
javapalava

Reputation: 695

Inheritance and Overriding of methods in java

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; 
}

Upvotes: 0

Views: 75

Answers (3)

Neeraj Jain
Neeraj Jain

Reputation: 7730

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 
 }

Upvotes: 1

Prashant Kumar
Prashant Kumar

Reputation: 69

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)
 }

Upvotes: 0

yole
yole

Reputation: 97133

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.

Upvotes: 2

Related Questions