How to declare class that extends generic that extends generic - java

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

Related

How to add a base class to a mosby MVPActivity?

I am looking for a way to add a base class to a mosby MVPActivity. Let me explain what i need.
Usually when using mosby we would declare an activity like this:
public class LoginActivity extends MVPActivity<LoginView, LoginPresenter> {
//...
}
but i would like to add a base class so that i can do the calls like this:
public class LoginActivity extends BaseActivity<LoginView, LoginPresenter> {
//...
}
and then BaseActivity would be like this :
abstract class BaseActivity<T, P> extends MvpActivity<T extends MvpView, P> {
}
but this is not working as the IDE at compile time has an error saying that "extends MvpView" has unexpected bounds.
So how can i add a base class to a mosby MVPActivity ?
You have to apply the generic parameters with your extended class definition and define it with the extension.
abstract class BaseActivity<V extends MvpView, P extends MvpPresenter<V>>
extends MvpActivity<V, P> { }

Implementing a Generic Interface In Java

I am new to Java Generics. I have to implement an interface which is of generic type. The syntax is as follows:
public interface A{}
public interface B<T extends A>{
public T methodB(T a) ;
}
Now I have to implement B so Lets say my Class is C
public class C implements B<T extends A>{}
The java compiler is not letting me use it this way. Also I do not want to use raw types. Please help.
It should be
public class C<T extends A> implements B<T>
The type parameter is declared following the class name, and later can be used in the implements clause.
If your implementing class is still a generic type you have to use this syntax:
public class C<T extends A> implements B<T> {}
As explained by Eran.
If C is not generic you simply need to specify the type argument for your interface:
public class C implements B<TypeExtendingA> {}
Where TypeExtendingA implements or extends A (or is A)

Java extending generics

Is this possible?
class A<T extends Service<E extends Entity>>
It's because I want to get the type of Service and Entity. Or any other way around to do it?
Currently I have an abstract method that sets the Service but if I can do it in parameter then much better.
I'm also wondering how can I pass the parameters in a base class:
I have ClassA that extends SubBaseClass1 that extends BaseClass1.
So:
class SubBaseClass1<E extends Entity, P extends Service<E>> { }
In BaseClass, I want to know the type of P and E.
Another question, if I have a method getBaseClass from another class, how will I specify the return type?:
public BaseClass<E extends Entity, T extends Service<E>> getBaseClass() { }
Is not working.
Found the answer:
public BaseClass<? extends IEntity, ? extends Service<?>> getBaseClass() { }
You would declare that like this:
class A<E extends Entity, T extends Service<E>>
Then you could have, for example:
A<Foo, Bar> a;
... where Foo is a subclass of Entity and Bar is a subclass of Service<Foo>.

when implementing interfaces get same interface as superclass

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>

How to extend generic Java class in Scala?

I have Scala class and I would like to extend one of the generic classes of JavaFX to create my custom contro
public abstract class SkinBase <C extends Control, B extends BehaviorBase <C>>
extends StackPane
implements Skin <C>
but I can't. example:
import javafx.scene.control.Control
class MyControl extends Control {
}
import com.sun.javafx.scene.control.skin.SkinBase
class MyControlSkin extends SkinBase[MyControl,MyControlBehavior] {
}
import com.sun.javafx.scene.control.behavior.BehaviorBase
class MyControlBehavior extends BehaviorBase[MyControl] {
}
Any suggestions how can I do that?
This works for me:
class MyControlBehavior(myControl: MyControl) extends BehaviorBase[MyControl](myControl)

Categories

Resources