Why do we allow interface to be used as a type in Java? -


for example, see this:

queue<string> q = new linkedlist<string>(); 

i totally understand point declaring type of queue, class implements queue interface can used instantiate object.

what bothers me see like

q.size()

to size of queue.

this compiles , works fine, me, doing defeats concept of "interface", because size() method in linkedlist class.

essentially need aware of class used instantiate queue object. if later decided use different class instantiate queue object, may run trouble

e.g. if decide not use linkedlist class , decide use other class instantiate queue object, we need make sure new class has method size(), otherwise need change it.

note: using size() method as example. potentially, method linkedlist has can called on queue object right? if later decided use other class instantiate queue object? need make sure new class has same method, right? seems defeating purpose of interface. if declare variable interface type, should using methods specific interface.

am missing points here?

any queue implementation have size() method, since size() method of collection interface, queue interface extends. therefore, size() belongs queue interface indirectly.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo