-1
public class Shop
{
    private static List<Product> products = new List<Product>(){};
}

code is throwing exception:is not abstract and does not override abstract method subList(int,int) in List

  • List is interface you can change it to new ArrayList() – Younes Ouchala Apr 18 '17 at 14:52
  • 3
    1. Its not throwing an Exception it is giving you a compilation error. 2. What about that error do you not understand? You can't instanciate abstract class or interfaces. You can however crate an anonymous subclass **if** you implement all abstract methods. – OH GOD SPIDERS Apr 18 '17 at 14:52
  • What are you trying to do? – Erwin Bolwidt Apr 18 '17 at 14:52
  • @LukasNaruševičius List is an interface and interface is abstract signatures of the necessary methods needed to be implemented for a class implementing the interface. Interface do not have the implementation, its simply abstraction. Refer my answer, Hope it will be helpful. http://stackoverflow.com/a/43476075/504133 – nits.kk Apr 18 '17 at 15:25

3 Answers3

2

You can't instantiate a List, its just an interface. So instantiate an implementation of it e.g. an ArrayList :

public class Shop
{
    private static List<Product> products = new ArrayList<Product>();
}
Markus
  • 1,141
  • 1
  • 9
  • 25
1

As mentioned in other answers here java.lang.List is an interface. I wish to add just a little more to those. List being an interface means it defines the necessary contract which must be respected and fulfilled by any class to qualify as a List. Users of such a class can be sure of the contract being followed so without being worried for the internal source code, they can be sure that it follows all the standard predefined rules for being a list.

Interface being just the contract needs to be implemented by some class for realization. An interface itself can not be instantiated, its only the Abstraction you can not have the object of interface without implementing it in a concrete class.

In java you can either use the keyword implements with a class and then must give implementation for the methods defined by the interface. You can also use annoyonmous class style. But if any class (including such an annoyonmous class also) implementing an interface, you must provide concrete implementation of all the methods defined in the interface. For this you can use

public interface AnyInterface{
    void anyMethodA(); // methods are public and abstract by default
    void anyMethodB(); // see as the methods just have signature here, no body
}

Now you can use annoyonmous style as below using curly braces.

AnyInterface objectOfAnnoyomousClass = new AnyInterface(){
     public void anyMethodA(){
          // curly braces implies a method body, here you can have the method implemented 
     }
     public void anyMethodB(){

     }
 };

java.util classes have the implemetation of java.util.List such as ArrayList, LinkedList.

private static List<Product> products = new LinkedList<Product>();

There is plenty of material available for java collections including the very well java docs.

**P.S. static has actually nothing to do with the compile time issue you are having in the code you showed.

nits.kk
  • 5,204
  • 4
  • 33
  • 55
0

List is an interface. You can't initialize an instance of an interface. You need to instead initialize a class that implements List like ArrayList.

private static List<Product> products = new ArrayList<Product>(){};
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117