1

I'm working on a java project where i need to use lists of different types that I create (Student ,school ... for example).

The problem is that I need to use some list methods with this types(or classes) like "containe" for Ex ...

I try to override this methods by creating my own list(arraylist or vector) which is Extended from java list ... but I have many problems because I want to use this new list(my list) with different types .

This how I extends myList from java list:

public class myList extends ArrayList<Object>{

public myList() {
}
    /***methods***/
}

and this how I use it :

public class newclass(){
       .
       .
       .
    myList<student> sl=new myList<student>();
       .
       .

But it does not work. So what is the right way to do this.

and thanks.

Joe's Morgue
  • 173
  • 2
  • 8
M. os.i
  • 153
  • 2
  • 9
  • two sidenodes. Classes do start with capital letters following the java naming convention. second, don´t make this custom `List` a rawtype by defining the generic as `Object`, this will and can only cause troubles later on(which is also your problem). Make it `MyList extends ArrayList`. – SomeJavaGuy Apr 25 '16 at 12:37
  • 1
    Possible duplicate of [Extending Generic Classes](http://stackoverflow.com/questions/7255643/extending-generic-classes) – Rabbit Guy Apr 25 '16 at 12:37
  • I don't know if I understood correctly, but maybe all you need is implement comparable in your object, so sort and other methods work ... reference: http://stackoverflow.com/a/3718515/2101088 – Rodrigo Apr 25 '16 at 12:42

1 Answers1

1

What you are looking for are Generics:

public class myList<T> extends ArrayList<T> {
...
}

This way you can create your own list of whatever you want like this:

myList<student> sl = new myList<>();

Response Update:

how to make get -for example- return the same type in declaration -which is instead of the type Object

Response with your list

myList<student> sl = new myList<>();
sl.add(new student());
student s = sl.get(0);

Response with an ArrayList:

// But it works also with an ArrayList which implicitly mean that you have no need 
// to create your own implementation of an ArrayList
List<student> sl = new ArrayList<>();
sl.add(new student());
student s = sl.get(0);
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • Can you please write the overrided `contain` for a type(object) `student` wich is defined by `String firstName ; String lastName;`. – M. os.i Apr 25 '16 at 14:27
  • could you please clarify I don't understand your question – Nicolas Filotto Apr 25 '16 at 14:33
  • 1
    you have no need to override the contains method, simply implement properly your equals method in your class student – Nicolas Filotto Apr 25 '16 at 15:06
  • i want to override the methods of list (contain,get ...) to work with different types of objects (i.e how to make `get` -for example- return the same type in declaration -which is `` instead of the type `Object`) , so how to do this ?! @NicolasFilotto – M. os.i Apr 25 '16 at 18:30
  • Maybe you want to overload by adding public boolean contains(T element) and then your class definition will be something like public class myList extends ArrayList – Nicolas Filotto Apr 25 '16 at 18:42
  • @M.os.i answer updated, once again you have no need to create your own imp of an ArrayList – Nicolas Filotto Apr 25 '16 at 18:52