#Both have Abstract class and interface?

1 messages ยท Page 1 of 1 (latest)

rain sierraBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
I'm implementing three different robots that are going to go through a maze (2d array with some locations not avaible to go through) in different ways, In my assignment for school they us to use inheritance and Interface, is it therefor reasonable to have

Yes, you can have an abstract class and interface in Java.

An abstract class is a class that cannot be instantiated on its own and is meant to be subclassed. It can have abstract methods, which are declared but not implemented in the abstract class. Subclasses of an abstract class must implement these abstract methods.

An interface in Java is a blueprint of a class that specifies what methods a class must implement without providing the implementation. A class can implement multiple interfaces but can only extend one superclass.

In your case, you could create an abstract class called "Robot" that contains common methods and properties for all robots, such as moving and checking if a location is available. You could also create an interface called "MazeSolver" that defines the method for solving the maze.

Each of your three robot classes could then extend the "Robot" abstract class and implement the "MazeSolver" interface. This way, they would inherit common behavior from the abstract class and define their own unique way of solving the maze through the interface method.

Using both an abstract class and interface allows for code reusability, flexibility, and clear separation of concerns in your maze-solving robots implementation.

rain warren
#

I try to utilize default methods in interfaces as much as possible but you eventually run into needing state. That might mean having to create an abstract class but I try to avoid creating abstract classes because there is no multiple inheritance and if I have to use that 'single inheritance' slot up, it means I have to add more complexity later to express some commonality among related classes. So in a nutshell, embrace an interfaces default methods, and avoid inheritance or abstract common class that implements that interface (until you can't.)

interface A{
   int doStuff(int severity);
}
abstract class Base implements A{
   StuffState stuffState;
   @Override
   public int doStuff(int severity){
      if(stuffState.isAgitated()){
        stuffState.adjustSeverity(severity);
      }
      return stuffState.severity();
   }
}
class Robot1 extends Base{}  
class Robot2 extends Base{}

VS

interface A{
  StuffState getStuffState();
  default int doStuff(int severity){
    StuffState stuffState = getStuffState();
    if(stuffState.isAgitated()){
        stuffState.adjustSeverity(severity);
    }
    return stuffState.severity();
  }

class Robot1 implements A, E, F{
   StuffState stuffState;
   @Override
   StuffState getStuffState(){ return stuffState; }
}
class Robot2 implements A, B, C, D{
   StuffState stuffState;
   @Override 
   StuffState getStuffState(){ return stuffState; }
}