when implementing interfaces get same interface as superclass - java

in java i am using generics and in class i want to use implements interface(? extends class) and this interface is generic interface<T> but i get message as
same interface as superclass
code example:
public interface ISomething<T>
{
string Name { get; set; }
string GetType(T t);
}
public class SomeClass implements ISomething<T extends SomeClass2>
is this possible?

You cannot use a generic specifier that is not defined. In your example for SomeClass, T is not declared.
This is invalid:
public class SomeClass implements ISomething<T extends SomeClass2>
Either of these are valid
public class SomeClass<T extends SomeClass2> implements ISomething<T>
or
public class SomeClass implements ISomething<SomeClass2>

Related

How to implement a generic interface where type param is Enum and some other type

I'm working with an interface that accepts a type parameter:
public interface Container<T>
Now I can have a class as such that implements it:
public class EnumContainer implements Container<Enum>
But now suppose I want a Container for enums that implements a interface called Position:
public interface Position {
String getAbbreviation();
String getDescription();
}
How can I define PositionEnumContainer? I've tried this but it's a compile time error:
public class PositionEnumContainer implements Container<Enum & Position>
I do not want to make PositionEnumContainer generic such as:
public class PositionEnumContainer <T extends Enum<T> & Position> implements Container<T>
You can make PositionEnumContainer implement Container<Enum<? extends Position>>.
If a class extends Enum<? extends Position> then it's an enum class that also implements Position, therefore, PositionEnumContainer will only accept objects that are both of type Enum and of type Position. But unfortunately, Java doesn't know about it, so PositionEnumContainer's producing methods will return Enum<? extends Position>, that can't be implicitly cast back to Position, and you will have to cast it yourself or use safe Enums#narrow method.
public class Enums {
#SuppressWarnings("unchecked") // only class E can extend Enum<E>
public static <E extends Enum<E>> E narrow(Enum<E> obj) {
return (E) obj;
}
}
Alternatively, you can create the PositionEnum interface that extends Position and can only be implemented by enums and then make PositionEnumContainer implement Container<PositionEnum>. In this case, each ConcretePositionEnum will have to implement PositionEnum<ConcretePositionEnum> instead of Position.
// should only be implemented by enums
// you can verify it with annotation processor
public interface IEnum<E extends Enum<E>> extends Comparable<E> {
String name();
int ordinal();
Class<E> getDeclaringClass();
#SuppressWarnings("unchecked")
default E self() { return (E) this; }
}
public interface PositionEnum<E extends Enum<E> & Position> extends IEnum<E>, Position {}

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

How to generalize this concrete class?

The below #Override annotation indicates that I am not overriding the method defined in the interface. How do I use generics with my concrete class so that it overrides the interface method?
public interface AInterface<T extends MyType> {
void do(T thing)
}
public abstract class BaseMyClass implments AInterface {
// other stuff
}
// AType extends MyType
public class MyClass extends BaseMyClass <AType> {
#Overide
public void doThing(AType atype) {
}
}
BaseMyClass is implementing the rawtype of AInterface, you either need to extend the generic to the abstract class itself or define it:
public abstract class BaseMyClass implments AInterface<AType>
public abstract class BaseMyClass<E extends MyType> implments AInterface<E>
BaseClass<AType> (assuming you meant BaseMyClass) isn't actually giving you a AInterface<AType>

Type check parameter is a (sub)class and also implements an interface

This answer no doubt exists on SO, but I haven't found the right combination of search terms to come up with it.
I have a method that I want to take a parameter that is of class A, but also implements interface B. How do I do it?
e.g.
public class MySubclassWithInterface extends MyClass implements MyInterface { }
public class MySubclass extends MyClass { }
public class MyInterfaceClass implements MyInterface { }
public class MyOtherSubclassWithInterface extends MyClass implements MyInterface { }
Out of the three classes above, I only want my method to accept an object that is MyClass and implements MyInterface, in other words, either MySubclassWithInterface or MyOthersubclassWithInterface but not MySubclass or MyIntefaceClass
I very sheepishly tried the following which obviously failed:
public void myMethod( (MyClass MyInterface) parameterName) {
...
}
Thanks for your help in advance.
You can express this with a generic type as in the following signature:
<T extends MyClass & MyInterface> void m(T p)
The rule is that the first type must be a class or an interface and any following parameter must be an interface type.

How to declare class that extends generic that extends generic

I have a class that extends generic class that also extends (another) generic class.
class B<TypeB> extends C{}
class C<TypeC>{}
and now my problems is how to specify the TypeC when creating class A
should be something like:
class A extends B<Type1><C<Type2>>
but the above actually does not compile.
Your decl of B should be:
class B<TB, TC> extends C<TC> {
}
and your target will be
class A extends B<ConcreteB, ConcreteC> {
}

Categories

Resources