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> { }
Related
Is there possible to add a new custom method to the well-known interface List in a way in Java? I tried to implement List interface to "public abstract class MyList", however, I would like to achieve it remaining the same name of the interface "List". For example, later implememnt in the following way:
List mylist = new ArrayList();
mylist.newmethod();
No, Java does not support such a feature. Use composition.
You could extend it to another interface
interface MyList<E> extends List<E> {
//add new methods here
}
Here's what you want to do. I wouldn't recommend it though, it can be confusing to have two List classes.
public abstract class List implements java.util.List {
public void myMethod() {
...
}
}
So I'm creating an implementation of a priority queue using generics. I have this interface which I am trying to implement in my PriorityQueue class:
public interface PriorityQueueInterface<Item extends Comparable<Item>> { }
but I'm not sure what the proper syntax is to correctly implement the PriorityQueueInterface. Here is what I currently have:
public class PriorityQueue<Item extends Comparable<Item>> implements PriorityQueueInterface<Item extends Comparable<Item>>{ }
but it's throwing multiple errors. What would be the correct way to implement the interface? Any help would be appreciated.
You've already declared Item to be Comparable<Item> with the class definition of PriorityQueue. You only need to reference it in the implements clause, where you don't need to repeat that it's Comparable<Item>. You reference a generic type parameter in the implements or extends clause just as you would for any other part of the class body where the generic type parameter is in scope.
Try
public class PriorityQueue<Item extends Comparable<Item>>
implements PriorityQueueInterface<Item>{ /* implement here */ }
I would like to make a class extend another class among several like this:
class Special extends < Class1<type1> , Class2<Type2>> {
// ToDo
}
I tried something like:
class Special<T1, T2, T3, T4> extends < T1<T2> , T3<T4>> {
// ToDo
}
Of course that syntax does not compile. How can I do that?
EDIT 1
Let's be clear here: I do NOT want multiple inheritance. I would like my class to extend EITHER one super class, EITHER another one. For that I am asking if there is a possibility doing so using generics.
The same way one can use generics for this special map of array:
class MapArray<T_KEY, T_VALUE> extends LinkedHashMap<T_KEY, ArrayList<T_VALUE>> {
}
This code works an allows the user to put any type for T_KEY and T_VALUE.
I would like to know whether is would be possible to use generics with classes.
To put it more simply I would like to do that:
class Special extends < Either_this_class, Either_this_one > {
}
You can do (staying withing the limits of Java's syntax):
class SubAndImp extends Super implements Interface {
}
or
class ImpAndImp implements InterOne, InterTwo {
}
All or some of the extended and implemented types can be generic:
class SubAndImp<X,Y> extends Super<X> implements Interface<Y> {
}
or
class ImpAndImp<X,Y> implements InterOne<X>, InterTwo<Y> {
}
Your subclass can also stay out of being generic, by instantiating a generic parameter:
class SubAndImp extends Super<Integer> implements Interface<String> {
}
Java supports only multiple interface inheritance. So you cannot extend multiple classes but you can implement any number of interfaces.
I'm having difficulty using generics for a redesign/refactoring I'm doing on an existing design.
public interface DataDto {
// some data here
}
public interface SetDto<MyDataDto extends DataDto> {
List<MyDataDto> getData();
}
public interface Results<MySetDto extends SetDto<DataDto>> {
MySetDto getResults();
}
public interface MyProblemInterface<MyDataDto extends DataDto,
MySetDto extends SetDto<MyDataDto>,
MyResults extends Results<MySetDto>> {
// some stuff here
}
My problem is that I get the following error for MyProblemInterface:
Bound mismatch: The type MySetDto is not a valid substitute for the
bounded parameter <MySetDto extends SetDto<DataDto>> of the type
Results<MySetDto>
I admit my experience with generics is somewhat limited, but basically I'm trying to enforce that all three of the types in MyProblemInterface are the same "type". For example, if I have ADataDto, BDataDto, ASetDto<ADataDto>, BSetDto<BDataDto>, AResults<ASetDto>, BResults<BSetDto>, I want to ensure a class can't implement MyProblemInterface in a manner like AMyProblemInterface<ADataDto, ASetDto, BResults>. I would think that since MySetDto extends SetDto<MyDataDto> just fine, I could continue to take that further, but I'm apparently wrong.
Thank you for any help.
You want too much from Java generics.
It would be simpler to declare your interface as following:
public interface MyProblemInterface<MyDataDto extends DataDto>
And then force method to use SetDto<MyDataDto> and Results<MySetDto>.
By using generics in class/interface declaration you specify some kind of variety which is determined later in definition. But in your case you said that SetDto and Results will always have MyDataDto as parameter, so there is no variety.
Shouldn't it be something like this instead, and you add the actual classes only when implementing the interfaces.
Updated the code, because I forgot to add the right Results definition. This should work.
public interface DataDto {
// some data here
}
public interface SetDto<T extends DataDto> {
List<T> getData();
}
public interface Results<T extends SetDto<? extends DataDto>> {
T getResults();
}
public interface MyProblemInterface<T extends DataDto, E extends SetDto<T>, K extends Results<E>> {
// some stuff here
}
I just wonder what usage the following code has:
public class Sub extends java.util.ArrayList<String> {...}
There is no any compiling restriction on the generic constraint java.util.ArrayList<String>.
The compiler does place restrictions on other code based on the type parameter in this case.
This will compile
public class Sub extends java.util.ArrayList<String> {
void addTwice(String s) { this.add(s); this.add(s); }
}
but this will not
public class Sub extends java.util.ArrayList<String> {
void addTwice(Object x) { this.add(x); this.add(x); }
}
Let's say you were making an index for a book, but you don't know how many indices you will need. You could make a class BookIndex extends ArrayList<String> or if you want to get really picky: BookIndex extends ArrayList<IndexEntry>.
/e1
Also, when a one Class extends a generic Class like ArrayList<String> you can grab the String out from the generic declaration, unlike if you had a class ArrayList<T>. In ArrayList<T> you would never be able to figure out what the T is.
You can extend class ArrayList, but it is not something that you should normally do.
Only ever say "extends" when you can truthfully say "this class IS-A that class."
Remember, Its not a good practise to extend the standard classes
Why not use like this ?
public class Sub {
List<String> s = new ArrayList<String>();
// ..
// ...
}
If you do that you can add to the basic functionality of an ArrayList or even change its normal functionality.
For example, you can override the add() method so that it will only add emails to the list.