简体   繁体   中英

How to create a List of a certain object type in Java?

I'm trying to get my head around OOP in Java and I came across a common problem that I am unable to understand/solve on my own. I have researched about this topic and I found relevant materials but I'm still unable to understand. (Please forgive my ignorance).

1- I have an interface that contains only one method and this method when implemented should return a List of Strings. (All good here)

2- The abstract class implements the interface and it contains some extra methods for example:

public void setSomething(List<MyInterface> something) { this.something = something; }

public List<MyInterface> getSomething() { return something; }

Now, a new Subclass extends the abstract class and one of the things I am trying to do is to call the method setSomething and create a list of type MyInterface.

Can someone please point me on the right direction on how to achieve this?

Thanks

In the most simply way like

public List<Whatever> generateList() { 
  return new ArrayList<>();
}

OK, seriously: the above returns an empty list. But that should be good enough for some initial testing.

You could then go forward and

public List<Whatever> generateList() { 
  List<Whatever> results = new ArrayList<>();
  results.add(new Whatever());
  return results;
}

... add actual objects that of course, only you know how to create.

Or maybe, going for the really simple solution according to your input:

class DerivedClass ... {

   private final List<YourInterface> someObjects;

   void setSomething(List<YourInterface> otherObjects) {
     this.someObjects = otherObjects;
   }

   List<YourInterface> getSomething() {
     return this.someObjects;
   }

Edit: it seems that your problem is that you don't know how to create objects of an interface . Thing is: that is not possible. You can only instantiate (calling new) for classes . In other words: you need some class that implements MyInterface; you "new" objects of that class; and add those objects to your lists.

Creating the list

Creating a "list of type MyInterface" is as simple as calling the constructor :

List<MyInterface> list = new ArrayList<>();

You can chose between several implementations of the List interface : LinkedList , ArrayList , etc.

You can learn about implementations of the List interface in the Java Tutorials here : https://docs.oracle.com/javase/tutorial/collections/implementations/list.html

Creating objects of interface type

Now if you want to actually create an object of type MyInterface and add it to the list, you need to create an object of a class that implements MyInterface . That will be enough. For example :

class A implements MyInterface {...}
list.add(new A());

You can learn more about instantiating objects with type interface in this post: Creating interface objects in java

Calling the method

Calling the setSomething method is as straightforward as calling it on an object you created :

MySubClass myObj = new MySubClass();
myObj.setSomething(list);

Readings

However, I think here your confusion doesn't come from a lack of understanding of Object Oriented Programming, but more specifically of Generics in Java.

You can read about Generics in the Java Tutorials here: https://docs.oracle.com/javase/tutorial/java/generics/index.html

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM