Extending class with generic parameter - java

I need to derive from the following class:
public abstract class MyTool<VIEW extends MyView>
implements LookupListener, MouseListener, MouseMotionListener, KeyListener {}
The following does not work:
public abstract class MySubTool<VIEW> extends MyTool<VIEW> {}
Thanks !

The compiler in MySubTool as no way of knowing that VIEW in MySubTool is a subclass of MyView, you have to specify it again:
public abstract class MySubTool<VIEW extends MyView> extends MyTool<VIEW> {}

This should:
public abstract class MySubTool<VIEW extends MyView> extends MyTool<VIEW> {}

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

Can I use implements and extends at the same time?

Can i use the implements and the extends at the same time? Because I need to inherit something on the other class while i used implements on the same class.
public class DetailActivity extends AppCompatActivity
implementsView.OnClickListener "extends ListActivity"
How can it be like that?
Yes, you can. But you need to declare extends before implements:
public class DetailActivity extends AppCompatActivity implements Interface1, Interface2 {
// ...
}
Any number of interfaces can be implemented, if more than one then each needs to be separated with a comma.
You can only extend one class but you implements multiple interfaces as your need.
class Human extends Monkey implements BasicAnimals, xyz
in this way you can extend a class as well as implement interfaces.

syntax error on token extends expected

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

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