syntax error on token extends expected - java

I have created following classes using generics
public interface FacilityMasterService extends AbstractAssessmentLevelService<E extends AbstractAssessmentLevelBean> {
}
public interface AbstractAssessmentLevelService extends AbstractAssessmentService<E extends AbstractAssessmentBean> {}
public interface AbstractAssessmentService extends GenericService<E extends GenericBean> {}
public interface GenericService<E> {}
public class AbstractAssessmentLevelBean extends AbstractAssessmentBean {
}
public class AbstractAssessmentBean extends GenericBean {
}
public class GenericBean{
}
But in interfaces I am getting following error Syntax error on token "extends", , expected
how to resolve this.
please help.

This is not valid:
... extends AbstractAssessmentLevelService<E extends AbstractAssessmentLevelBean>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
(You can't extend a class without specifying a concrete type argument.)
Perhaps you're after this:
public interface FacilityMasterService<E extends AbstractAssessmentLevelBean>
extends AbstractAssessmentLevelService<E> {
}

Related

Bound mismatch with Generic Types

After one day searching I decided to ask your helps guys :-)
here is my issue:
I need to write coverter for some pojos.
public abstract class Answer<T extends Serializable> implements Serializable {//some code}
public class BooleanAnswer extends Answer<Boolean> {//some code}
public abstract class AnswerDMO<T extends Serializable> implements Serializable {//some code}
public class BooleanAnswerDMO extends AnswerDMO<Boolean> {//some code}
public interface Converter<I, O> {
O convert(I input);
}
public abstract class AnswerConverter<A extends Answer<Serializable>, J extends AnswerDMO<Serializable>>
implements Converter<A, J>, Serializable {
#Override
public J convert(A input) {
// some code
}
}
public class BooleanAnswerConverter extends AnswerConverter<BooleanAnswer, BooleanAnswerDMO>
{
#Override
public BooleanAnswerDMO convert(BooleanAnswer input) {
// some code
}
}
I'm getting an error on the BooleanAnswerConverter, the parameter BooleanAnswer is not within its bound, should extend Answer
I tried many combination but couldnt get it right.
How to fix it?
Since A and J extends Answer and AnswerDMO, and they have the type, you must change the AnswerConverter from:
public abstract class AnswerConverter<A extends Answer<Serializable>, J extends AnswerDMO<Serializable>>
To:
public abstract class AnswerConverter<A extends Answer, J extends AnswerDMO>
Answer and AnswerDMO has the type that is forcing the Serializable. Boolean in you example. Answer<Serializable> will try to ensure that the final implementation is this one, not a generic Answer.
This change result in correct compile of class:
public class BooleanAnswerConverter extends AnswerConverter<BooleanAnswer, BooleanAnswerDMO> {
#Override
public BooleanAnswerDMO convert(BooleanAnswer input) {
return null;
}
}
I don't know if this is what you want but a working solution would be
abstract class AnswerConverter<A extends Answer<? extends Serializable>,
J extends AnswerDMO<? extends Serializable>> implements Converter<A, J>, Serializable {

JAVA: generic type class inheritance and generic type inheritance

let me state my situation first:
I've, in a 3rd party SDK, a base class with generic type:
public abstract class BaseClass<T extends BaseDataType> {
public abstract class BaseDataType {xxx}
public abstract int getCount();
public abstract someMethod(T t);
}
in my business environment, it extends BaseClass as follows:
public abstract class MyGeneralBaseClas<`how to write this???`> extends BaseClass {
#Override public int getCount() {return some_list.size()};
#Override public abstract someMethod(`how to write this???`);
}
in the real business, I write my class:
public class MyClass extends MyGeneralBaseClass<MyDataType> {
#Override public someMethod(MyDataType type) {xxx}
}
public class MyDataType extends BaseClass.BaseDataType {
xxx
}
but the code failed to compile. sorry I'm travelling and I don't have my IDE and development tools, so I cannot paste the error. but I think if you write this down in an IDE such IntelliJ Idea, it would give the error I'm encountering.
so how to write this case: inheritance of generic type, and inheritance of the class using the generic type.
what i want is, in MyClass uses the concrete class MyDataType instead of the abstract BaseDataType.
It seems MyGeneralBaseClas should have a generic type parameter with the same type bound as BaseClass:
public abstract class MyGeneralBaseClas<T extends BaseDataType> extends BaseClass<T> {
#Override public int getCount() {return some_list.size()};
#Override public abstract someMethod(T t);
}
You can do it in two ways:
By using BaseDataType class as is, e.g.:
abstract class MyGeneralBaseClas<T extends BaseDataType> extends BaseClass<T> {
#Override public void someMethod(T t){}
}
By extending BaseDataType class, e.g.:
class DerivedDataType extends BaseDataType{
//Some code
}
abstract class MyAnotherBAseClass<DerivedDataType> extends BaseClass{
#Override
public void someMethod(com.gamesys.application.BaseDataType t) {}
}

Java Generic how to change parameterized subclass

I have such structure:
public class Record{}
public class ParsedRecord extends Record {}
public class RecordsList extends ParsedRecord {}
public abstract class Processor<T extends Record >{ private BlockingQueue<T> queue;}
public class Dispatcher extends Processor<ParsedRecord> {
List<ParsedRecord> list;
public Dispatcher() {
}
public void process(ParsedRecord record){
//blabla
}
}
I want to use Dispatcher class with parameters that are ParsedRecord or any type that extends from ParsedRecord class.
Could some please help me to understand how to properly change Dispatcher class definition?
Could be as simple as changing your class definition to:
public class <T extends ParsedRecord> Dispatcher extends AbstractProcessor<T> {
and then: use that T as type for your list, or for the parameter given to process().
But the real answer here is: study the concept. Don't try to go with trial and error.
You declare a Processor class but you extend AbstractProcessor.
You have probably do a naming mistake in the question.
So I suppose you have only an AbstractProcessor class to give a concrete answer.
In your case, if you want related the type declaration of the class with the parameter of the method, you have to declare the method in the parent class first and specify the parameter of it with the declared type :
public abstract void process(T record){
You would have a parent class :
public abstract class AbstractProcessor<T extends Record >{
...
public abstract void process(T record);
...
}
And in the subclass you get this symmetric declaration :
public class Dispatcher extends AbstractProcessor<ParsedRecord> {
...
public void process(ParsedRecord record){
//blabla
}
}

Unexpected bound

I'm trying to compile this new class :
public class WindowedGame
extends GameContainer<GameType extends Game<Graphics2D>> {
...
}
This class extends the class :
public abstract class GameContainer<GameType extends Game<?>> {
....
}
Can you suggest me a correction or explain to me why I get the error :
Unexpected bounds
Thanks!
GameType is the generic type parameter name, so it cannot be in the extends clause.
If WindowedGame should be generic, define it as
public class WindowedGame<GameType extends Game<Graphics2D>>
extends GameContainer<GameType> {
...
}
If WindowedGame shouldn't be generic, perhaps you meant to define it as
public class WindowedGame
extends GameContainer<Game<Graphics2D>> {
...
}
BTW, the naming convention for generic type parameter names is often a single upper case character (T, E, etc...). It would be less confusing if instead of GameType you write T.
public class WindowedGame<T extends Game<Graphics2D>>
extends GameContainer<T> {
...
}
public abstract class GameContainer<T extends Game<?>> {
....
}

Bound mismatch: type is not a valid substitute for the bounded parameter

I am getting the following error Bound mismatch: The type FFTWTask is not a valid substitute for the bounded parameter <T extends AbsTask<T>> of the type AbsStage<T,U>
I am representing an abstract stage:
public abstract class AbsStage<T extends AbsTask<T>, U extends AbsTask<U>> { }
Where an AbsTask is:
public abstract class AbsTask <T> { }
Then I create an AbsTask called FFTWTask:
public class FFTWTask extends AbsTask<Double>{ }
Finally, I create an AbsStage called FFTWStage:
public class FFTWStage extends AbsStage<FFTWTask, FFTWTask>{ } // Error occurs here
What am I missing with my understanding of generics. I have found numerous posts that feature the same error message, but I cant seem to decipher them.
Here is a simple class that achieves the error:
public class Test {
public abstract class AbsTask <T> { }
public class FFTWTask extends AbsTask<Double>{ }
public abstract class AbsStage<T extends AbsTask<T>, U extends AbsTask<U>> { }
public class FFTWStage extends AbsStage<FFTWTask, FFTWTask>{ } // Error here <-
}
If the type parameter T in AbsStage must extend AbsTask<T>, then FFTWTask must extend AbsTask<FFTWTask> in order to be used in FFTWStage:
public class FFTWTask extends AbsTask<FFTWTask>{ }

Categories

Resources