Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending an interface

I downloaded a code that has a function that starts like this:

public class MDP<S, A extends Action> implements MarkovDecisionProcess<S, A> {
//some code...blah blah blah...
}

S and A are supposed to be some kind of type. Action is an interface.

In my code, I want to use the given class MDP. Therefore, I had to define S and A; I defined S to be a certain class, but I don't know how to define A... it isn't a class and it isn't an interface. What's it supposed to be?

Thanks :)

like image 362
Cheshie Avatar asked Apr 20 '26 00:04

Cheshie


2 Answers

"A" type would be a class that implements an interface which extends Action.

like image 155
Avinash Anand Avatar answered Apr 22 '26 14:04

Avinash Anand


For your code

public class MDP<S, A extends Action> implements MarkovDecisionProcess<S, A> {
  //some code...blah blah blah...
}

S and A are types. They can refer to an interface or a Class. The letter designations are arbitrary. They can be any letter. This is just as valid

public class MDP<Q, Z extends Action> implements MarkovDecisionProcess<Q, Z> {
  //some code...blah blah blah...
}

All it says is, "class MDP is a parameterized class, having the parameters S and A where A is some subclass of Action, and it implements the interface MarkovDecisionProcess<S,A>. S and A may or may not be of the same type."

When you give type parameters to the class, you can narrow the specificity of the type. In this case, the S stays the same (in terms of specificity) but you narrow second parameter to some instance of Action.

It could be possible to have those types on the interface declaration. They could then be left off on the instance.

public interface MarkovDecisionProcess<S,V extends Action>{}

public class MDP implements MarkovDecisionProcess{}

You might benefit from reading some of the official documentation.

like image 33
MadConan Avatar answered Apr 22 '26 13:04

MadConan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!