Is it possible to make something like this? I know that implements cannot be in the <>, but I want to restrict the T to be Serializable somehow.
public class Clazz<T implements Serializable> {
...
}
public class Clazz<T extends Serializable> {
...
}
Just use extends instead of implements.
Yes, just use extends instead of implements.
Related
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.
I 'll get straight to the point. Here is the problem
I have a class ClassA
public class ClassA<GENERICTYPE extends TypeA>
{
...
}
What I want to achieve is make the GENERICTYPE extend a new TypeB where TypeB extends TypeA
One possible solution is
public class ClassB<GENERICTYPE extends TypeB> extends ClassA<GENERICTYPE>
{
//emtpy
}
But doesn't seem to be the right approach.
Any suggestions?
If it makes any difference, I am using Spring 4.
Thanks
Nikos
It's the right idea, but not quite how you specify it:
public class ClassB<T extends TypeB> extends ClassA<T> {
}
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>.
I am a C# programmer and have agreed to help a fried doing Java homework.
In one example I want to create a class that extends a generic List. In C# this looks like
public class MyListClass : List<MyCustomType>
I have tried
public class MyListClass extends List<MyCustomType>
and get the error "no interface expected here". Well, I am not trying to use an interface... Any hints?
java.util.List is a interface. You need to implement it not extend it.
public class MyListClass implements List<MyCustomType>{
}
You can't extend interface. You must implement it.
But you can extend one of implementations (LinkedList for example):
public class MyListClass extends LinkedList<MyCustomType> {
Java ain't C++, so forget all about standard templates.
What you probably want is just a typed List:
List<MyCustomType> myList = new ArrayList<MyCustomType>();
and that's all.
It would be unusual to have "extending a generic class" as a goal for an assignment. It is unusual in the real world too.
You need to declare a generic class / interface
public class MyListClass<T> implements List<T> { }
Or
public interface MyListInterface<T> extends List<T> { }
Or best of all
public class MyListClass<T> extends AbstractList<T> implements List<T> { }
To be brief, I have:
public interface Dictionary<E extends Comparable<E>> extends Iterable<E> {
And
public class TreeDictionary implements Dictionary<TreeDictionary> {
When compiled, caused an error
TreeDictionary.java:4: type parameter TreeDictionary is not within its
bound public class TreeDictionary implements
Dictionary {
I guess the reason is probably because I'm not fully clear with the declaration of the Dictionary interface.
Would really appreciate if somebody could explain me this :)
public interface Dictionary<E extends Comparable<E>>
That says that whichever type you want to use for E, it must implement the Comparable interface.
Given yourTreeDictionary definition:
public class TreeDictionary implements Dictionary<TreeDictionary>
TreeDictionary does not implement Comparable, and so cannot be substituted for E in the generic type of Dictionary.
Try this instead:
public class TreeDictionary implements Dictionary<TreeDictionary>, Comparable<TreeDictionary>
TreeDictionary should now conform to the constraints of E.
Your TreeDictionary class should also implement the Comaparable interface.
You need implement Comparable interface for it to work
public class TreeDictionary implements Dictionary, Comparable
hope it helps