My intention is to set create a class with this type of inheritance:
public class BaseActivity<T> extends <T extends Activity>
but of course this inheritance syntax doesn't compile. Any alternative suggestion where I can arbitrarily select a Tab or Map Activity to be the base of other Activity classes whose override behavior is necessary?
You can't do that, but you can do:
public class BaseActivity<T extends Activity> extends Activity
That's not exactly what I think you mean to express, but perhaps close?
Related
I need to parameterise my class by Enum with some concrete methods, something like:
class K<E extends Enum<E> implements SomeInterface>
But Eclipse prohibit me to use "implements" word in "<>". What can I do to solve my problem? Is there any sensible reason why can't I use "implements" in definition of generic type?
Generic constraints use the word extends for both base classes and interfaces.
To constrain on multiple types, use an &:
class K<E extends Enum<E> & SomeInterface>
You would use extends in this case:
class K<E extends Enum<E>&SomeInterface>
Please note that if it needs to also extend a class it must come before any interfaces.
I'd like to implement the following classes with the following hierarchy:
public class DwellingPropertySetter
extends AbstractPropertySetter<Dwelling>
public class HousePropertySetter
extends DwellingPropertySetter<House>
public class SkyscrapperPropertySetter
extends HousePropertySetter<Skyscrapper>
Unfortunately this code won't compile. A way to do it would be this:
public class DwellingPropertySetter<T extends Dwelling>
extends AbstractPropertySetter<T>
public class HousePropertySetter<T extends House>
extends DwellingPropertySetter<T>
public class SkyscrapperPropertySetter<T extends Skyscrapper>
extends HousePropertySetter<T>
But for me those extends keywords are unnecessary.
Whenever I want to use a SkyscrapperPropertySetter I'd have to specify a type parameter. This is useless and would then look like SkyscrapperPropertySetter<Skyscraper>.
Do you know a way out for me? The only other way I know to realise my first hierarchy would be using interfaces and delegate methods.
I think you have correctly recognized it is pointless to have something like ChickenArrayList<Chicken>, so you can either create something like
class ChickenArrayList extends ArrayList<Chicken>
OR, if you want to reuse some functionality in ChickenArrayList, you may have to make it abstract (and generic) and put another concrete, non-generic class on top of it:
class AbsChickenArrayList<T extends Chicken> extends ArrayList<T>
// and
class ChickenArrayList extends AbsChickenArrayList<Chicken>
class HenArrayList extends AbsChickenArrayList<Hen>
I know this is quite verbose, but this is the best you can do with this ~18 year old language.
I think you can achieve what is reasonable. If you have
public class DwellingPropertySetter
extends AbstractPropertySetter<Dwelling>
That means you have already made DwellingPropertySetter non-generic, all the method signatures will use Dwelling. If you say
public class DwellingPropertySetter<T extends Dwelling>
extends AbstractPropertySetter<T>
that means there can be various DwellingPropertySetters -- and you really would like to have different ones, so I think this is what you really want to keep. Or is it your complaint that you want both a DwellingPropertySetter with no args, where Dwelling is assumed, AND a subclass extending DwellingPropertySetter<T> with a different T? That cannot be had with Java Generics.
I am not getting meaning by
public abstract class SampleAdapter <T extends Adapter> extends ViewGroup
What does it do? In general we perform normal inheritance, but here something extended form.
can any body suggest me?
thanks
This declares a class SampleAdapter that:
has public visibility
is abstract; it cannot be instantiated directly; it must be be used as a base class.
is generic; it must be parameterised by some type T that is a sub-class of Adapter.
inherits from ViewGroup.
T extends Adapter:
The class SampleAdapter takes a class parameter T that should extend Adapter
example:
ArrayList A = new ArrayList<String>();
String is parameter for ArrayList.
SampleAdapter A = new SampleAdapter<parameterClass> ();
parameterClass is parameter for SampleAdapter (which is T) and it must extend Adapter:
public class parameterClass extends Adapter{//must extend Adapter
As for abstract, it says that class SampleAdapteris a class that can't be instantiated directly but rather must be extended by another class.
The thing inside the < > section is associated a concept called "generics". It's a feature introduced in Java from Java 5 and forward. You can read more here.
abstract is referring to a class that can't be instantiated directly, usually because of some methods that need to be implemented in an extending class.
You'll have to implement those abstract methods in the subclass, otherwise your code won't compile.
abstract means that the SampleAdapter class cannot have any objects, <T extends Adapter> means, that the SampleAdapter class is used as a container for objects of any class, that extends Adapter. Hope this helps.
I've got a class Foo<T>. How can I say that I want T to be some class implementing BarInterface? Writing simply class Foo<T implements BarInterface> doesn't compile.
Use extends instead of implements.
I want to create a class that takes two parameters. One should be typed simply as T. The other should be typed as something that extends both T and SomeInterface<T>. When I attempt this with
public class SomeClass<T, S extends SomeInterface<T> & T>
then Java complains with
"The type T is not an interface; it cannot be specified as a bounded parameter"
and if instead I attempt to create an interface for S with
public interface TandSomeInterface<T> extends SomeInterface<T>, T
then Java complains with
"Cannot refer to the type parameter T as a supertype"
Is there any way to do this in Java? I think you can do it in C++...?
You can't create an interface that extends the type parameter T since there's no contract that would guarantee T to be an interface. And of course interface extending a class is not allowed.
this works if you extend an interface as well:
public class SomeClass<T extends I, S extends SomeInterface<T> & I>
but maybe it's not exactly what you want ...