I am currently learning interfaces, classes and generics.
My current assignment is to create a class which mimics "ArrayLists" in Java. (probably java 7)
I have created a class: public class StudentArrayList implements SimpleArrayList<E> {
but I am getting this error: I am getting this error
Error: The type StudentArrayList must implement the inherited abstract method SimpleArrayList<E>.set(int, E)Java(67109264)
I have already implemented all the function signatures in the class, but it is still me asking to implement again and again. How do I fix this issue? please let me know. Thanks!
StudentArrayList.java:
public class StudentArrayList implements SimpleArrayList<E> {
public static void main(String[] args) {
System.out.println("Success");
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean contains(E o) {
// TODO Auto-generated method stub
return false;
}
@Override
public E[] toArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public void add(E e) {
// TODO Auto-generated method stub
}
@Override
public void remove(E o) {
// TODO Auto-generated method stub
}
@Override
public boolean containsAll(SimpleArrayList<E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(SimpleArrayList<E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(int index, SimpleArrayList<E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(SimpleArrayList<E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(SimpleArrayList<E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
@Override
public E get(int index) {
// TODO Auto-generated method stub
return null;
}
@Override
public void set(int index, E element) {
// TODO Auto-generated method stub
}
@Override
public void add(int index, E element) {
// TODO Auto-generated method stub
}
@Override
public void remove(int index) {
// TODO Auto-generated method stub
}
@Override
public int indexOf(E o) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int lastIndexOf(E o) {
// TODO Auto-generated method stub
return 0;
}
@Override
public SimpleArrayList subList(int fromIndex, int toIndex) {
// TODO Auto-generated method stub
return null;
}
}`
You appear to be trying to write a generic class that implements an generic interface. The problem is that you have not actually declared the class as generic. And the compiler therefore things that E
is a class name.
The declaration:
public class StudentArrayList implements SimpleArrayList<E> {
should be:
public class StudentArrayList<E> implements SimpleArrayList<E> {
The <E>
after StudentArrayList
declares E
as a type parameter of StudentArrayList
.
The compilation errors were probably a result of the compiler thinking that E
was a class name (rather than a type parameter) and not being to find any definition for it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.