Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Interfaces are collection of final static fields and abstract methods (Newly Java 8 added support of having static methods in an interface).</p> <p>Interfaces are made in situations when we know that some task must be done, but how it should be done can vary. In other words we can say we implement interfaces so that our class starts behaving in a particular way.</p> <p>Let me explain with an example, we all know what animals are. Like Lion is an animal, monkey is an animal, elephant is an animal, cow is an animal and so on. Now we know all animals do eat something and sleep. But the way each animal can eat something or sleep may differ. Like Lion eats by hunting other animals where as cow eats grass. But both eat. So we can have some pseudo code like this,</p> <pre><code>interface Animal { public void eat(); public void sleep(); } class Lion implements Animal { public void eat() { // Lion's way to eat } public void sleep(){ // Lion's way to sleep } } class Monkey implements Animal { public void eat() { // Monkey's way to eat } public void sleep() { // Monkey's way to sleep } } </code></pre> <p>As per the pseudo code mentioned above, anything that is capable of eating or sleeping will be called an animal or we can say it is must for all animals to eat and sleep but the way to eat and sleep depends on the animal.</p> <p>In case of interfaces we inherit only the behaviour, not the actual code as in case of classes' inheritance.</p> <blockquote> <p>Q1. As interfaces are having only abstract methods (no code) so how can we say that if we are implementing any interface then it is inheritance ? We are not using its code.</p> </blockquote> <p>Implementing interfaces is other kind of inheritance. It is not similar to the inheritance of classes as in that inheritance child class gets the real code to reuse from the base class.</p> <blockquote> <p>Q2. If implementing an interface is not inheritance then How interfaces are used to achieve multiple inheritance ?</p> </blockquote> <p>It is said because one class can implement more than one interfaces. But we need to understand that this inheritance is different than classes' inheritance.</p> <blockquote> <p>Q3. Anyhow what is the benefit of using Interfaces ? They are not having any code. We need to write code again and again in all classes we implement it. </p> </blockquote> <p>Implementing an interface puts compulsion on the class that it must override its all abstract methods.</p> <p>Read more in my book <a href="http://www.laxmipublications.com/servlet/lpgetbiblio?bno=B000369&amp;pageName=Keywords" rel="noreferrer">here</a> and <a href="http://www.amazon.in/Way-Core-Java-Gurparsad-Singh/dp/9386035553/ref=sr_1_1?ie=UTF8&amp;qid=1466577389&amp;sr=8-1&amp;keywords=the+way+to+core+of+java" rel="noreferrer">here</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload