Home > OS >  OOP problem while creating an app HalvingCarousel
OOP problem while creating an app HalvingCarousel

Time:03-10

I have a superclass called DecrementCarousel which has a method that returns an object called CarouselRun. CarouselRun has its own methods which I need to override in HalvingCarousel, but I don't know how. DecrementCarousel:

public class DecrementingCarousel {
    static int [] arr ;
    static int capacity;
    int counter = 0;
    boolean alreadyExecuted = false;
    boolean alreadyRun = false;

    public DecrementingCarousel(int capacity) {
        DecrementingCarousel.capacity = capacity;
        arr = new int[capacity];

    }

    public boolean addElement(int element){
        if (alreadyExecuted) return false;
        if (counter < capacity && element > 0) {
            arr[counter] = element;
            counter  ;
            return true;
        }
        return false;
    }

    public CarouselRun run(){
        alreadyExecuted = true;
        if (alreadyRun) return null;
        alreadyRun = true;
        return new CarouselRun();

    }


    }

Here are methods in CarouselRun:

public class CarouselRun {
    int position = 0;

    public int next() {
        int count = 0;
        while (count < arr.length && arr[position %= arr.length] <= 0) {
            position  ;
            count  ;
        }
        if (count == arr.length) return -1;
        return arr[position  ]--;
    }

    public boolean isFinished() {
        for (int var: arr) {
            if (var > 0) return false;
        }
        return true;
    }

}

How to override these CarouselRun methods in a subclass called HalvingCarousel? According to the task HalvingCarousel can only extend DecrementCarousel

CodePudding user response:

Inheritance

This can be done like any other simple inheritance. You just need to declare the class HavingCarousel as a child of CarouselRun using the extends keyword and the use annotation @Override to declare that you want to override the methods of the parent class.

Parent class:

public class CarouselRun {
    int position = 0;

    public int next() {
        System.out.println("Carousel run implementation of next()");
        return 1;
    }

    public boolean isFinished() {
        System.out.println("Carousel run implementation of isFinished()");
        return true;
    }

}

Child class:

public class HalvingCarousel extends CarouselRun{
    @Override
    public int next() {
        System.out.println("HavingCarousel implementation of next()");
        return 2;
    }

    @Override
    public boolean isFinished() {
        System.out.println("HavingCarousel implementation of isFinished()");
        return true
    }
}

Interfaces

Looking at your code it could also be worth exploring interfaces instead of inheritance. Intefaces are generally more flexible and a class can implement many interfaces while it can only inherit from one parent class so interfaces should be used if there are no strong reasons opposing that like for example a huge portion of shared code which as far as I can see you will not be having.

Interface for a carousel

public interface ICarousel {
    public int next();
    public boolean isFinished();
}

CarouselRun implements the interface:

public class CarouselRun implements ICarousel {
    int position = 0;

    @Override
    public int next() {
        System.out.println("Carousel run implementation of next()");
        return 1;
    }

    @Override
    public boolean isFinished() {
        System.out.println("Carousel run implementation of isFinished()");
        return true;
    }
}

HalvingCarousel implements the interface:

public class HalvingCarousel implements ICarousel{
    @Override
    public int next() {
        System.out.println("HavingCarousel implementation of next()");
        return 2;
    }

    @Override
    public boolean isFinished() {
        System.out.println("HavingCarousel implementation of isFinished()");
        return true;
    }
}

Further Reading

From reading your code I believe that you still need to read more on basic Java concepts like interfaces, classes, inheritance, static vs instance attributes and modifiers. For example I can't see why you would declare your array arr as static but then initialize it in the constructor and then apparently you are trying to access the array arr in a totally unrelated instance of class CarouselRun. This will not work and it does not really make sense, but as these are basic concepts I cannot explain all of this here.

CodePudding user response:

To override behaviour of CarouselRun you need a child class, overriding its' methods. Then you need to override DecrementingCarousel.run() in order to return the subclass of CarouselRun.

public class AnotherCarouselRun extends CarouselRun {

    @Override
    public int next() {
        //override behaviour as needed
        return 0;
    }

    @Override
    public boolean isFinished() {
        //override behaviour as needed
        return false;
    }
}

Override methods in AnotherCarouselRun to suit your needs.

Then override behaviour of DecrementingCatousel.run()

public class HalvingCarousel extends DecrementingCarousel {

    public HalvingCarousel(int capacity) {
        super(capacity);
    }

    @Override
    public CarouselRun run() {
        //do other stuff
        return new AnotherCarouselRun();
    }
}

AnotherCarouselRun is a CarouselRun, so you are abiding to the contract of run(). Another option is to use anonymous class:

public class HalvingCarousel extends DecrementingCarousel {

    public HalvingCarousel(int capacity) {
        super(capacity);
    }

    @Override
    public CarouselRun run() {
        //do other stuff
        return new CarouselRun() {

            //this is anonymous class

            @Override
            public int next() {
                //override behaviour as needed
                return 0;
            }

            @Override
            public boolean isFinished() {
                //override behaviour as needed
                return false;
            }
        };
    }
}

This is virtually the same as first option, but you are subclassing CarouselRun as anonymous class. They are mostly used when you need the class only once.

  • Related