-3

Before start I have small code to understand question much better.

public interface Shape {

    public void draw();

}


public class Circle implements Shape {

    @Override
    public void draw() {
        System.out.println("Drwaing circle");
    }

    public void kill() {

        System.out.println("Class specific Method");
    }

    public static void main(String[] args) {
        Circle C = new Circle();
        C.draw();   //Compiled
        C.kill();   //Compiled

        Shape S = new Circle();
        S.draw();  //Compiled
        S.kill();  //Not able to Compile     

    }
}

As you can see, If I use Interface type object to call class(Circle) method(kill), I am not allowed. It makes sense because kill method neither defined in interface nor implemented.

My question is if that's a case, When we use List in a manner mentioned below, then why its always said that List uses ArrayList methods...? Technically It should not according to my above example.

List myList = new ArrayList();

If not, then Whose methods List use..? Because List defined methods need to be implemented somewhere in class which implements List. No..?

Check first comment in the answer in below link as well, it also says the same thing. What is the difference between List and ArrayList?

Is anyone Can come up with more clear picture, how its working ? Whose methods List uses while above kind snippet..?

Johib Khan
  • 215
  • 1
  • 2
  • 13
LearnJava
  • 372
  • 1
  • 4
  • 16
  • 3
    ArrayList implements List. Not the other way around – bichito Aug 05 '17 at 03:45
  • @efekctive...Okay..if it implements...Where its defined..Or it gets implemented automatically when we use in this way..? – LearnJava Aug 05 '17 at 03:46
  • The List class is part of the Java language. It is an interface that has methods on it (but don't actually do anything -- they are the contract for any implementing classes). ArrayList is an implementation of the List interface. It has methods that actually do work. – AHungerArtist Aug 05 '17 at 03:48
  • 1
    It isn't always said. I have never seen this statement before, in 20+ years. What are you talking about? – user207421 Aug 05 '17 at 03:49
  • Without trying to put you down: just follow the links in the java api docs. They teach a lot and save you the trouble of getting snarky comments. – bichito Aug 05 '17 at 03:51
  • @EJP, May I know please how it works then...? Might be your experience could help new learner(me) to understand and learn more..? – LearnJava Aug 05 '17 at 03:54

1 Answers1

2

When you store the object of a derived class(In this case ArrayList) in the base class/interface reference object(interface List), You can only access the methods defined in base class/interface.

These methods have to be overridden in the example of an interface.

So when you write

List x = new ArrayList();

You can access methods of List interface that are overridden in ArrayList class but not all the ArrayList methods.

  • @Chandrasekhra, like you said.."You can access methods of List interface that are overridden in ArrayList class ". But when we use "List x = new ArrayList();, we never overrides anywhere in class where we write this code ?It automatically allow to access methods to work What are those methods and how they've got overridden which you are talking about ..? – LearnJava Aug 05 '17 at 04:09
  • 1
    ArrayList class by default overrides it ..You don't need to – Chandrasekhar Raman Aug 05 '17 at 04:10