Interface vs Abstract Class | How Interface is Different From Abstract Class

                                                                              
1.       When you create an interface, you are defining a contract for what a class can do, without saying anything about how the class will do it. An interface is a contract.
Ex:
Interface Bounceable
Having two methods :
                bounce();
                setBounceFactor();
2.       Any class implementing the above interface needs to define both the methods of the above methods.
3.       Interface can be implemented by any class, from any inheritance tree.
This lets you take radically different classes and give them a common characteristic.
                                                        Ex:
                                                        Two classes:   Ball{}          and        Tier{}
                                        Both of them is not sharing any inheritance relationship; that is
                                        Ball extends Toy
                                        Tier extends only java.lang.Object.
But by making both Ball and Tier implement Bounceable, you are saying that Ball and Tier can be treated as, “Things that can bounce”.
Ex:
Interface Bounceable
{
        public abstract void bounce();
        Public abstract  void setBounceFactor();    // no need to write it like this .//
}
Class Tier implements Bounceable
        {
        public void bounce();   //you may write like this //
        public void setBouncFactor();
        }
4.       Abstract class can define both abstract and non-abstract methods, an  interface can have only abstract methods.
5.       Another way interfaces differ from abstract classes is that interfaces have very little flexibility in how the methods and variables defined in the interface are declared.
6.       All interface methods are implicitly public and abstract. In other words , you do not need to actually type the public or abstract modifier in the method declaration, but the method is still always public and abstract.
7.       All variables defined in an interface must be public, static, and final-in other words, interface can declare only constants, not instance variables.
8.       Interface methods must not be static.
9.       Because interface methods are abstract, they cannot be marked final, strictfp, or native
10.   An interface can extend one or more other interface
11.   An interface cannot extend anything but another interface.
12.   An  interface cannot implement another interface or class
13.   An interface must be declared with the keyword interface.
14.   Interface types can be used polymorphically.
Public abstract interface Rollable() // is a legal interface//
Public interface Rollable() //  is also a legal interface//
The public modifier is required if you want the interface to have public rather than default access.
15.   Methods within the interface
public abstract void bounce();
public  void setBounceFactor(int bf); // both are legal but the setBounceFactor  is the relevant one //
** you must remember that all interface methods are public and abstract regardless of what you see in the interface definition.
** but the methods won’t be final because both abstract and final is opposite of each other ,abstract need to be defined while final can’t be defined in the subclass.

                                Interface Constants
You can define constants but not instance variables,  with
                                        The specifier
                                                                        Public static final
                        Ex;          public static final int i=27;
It cannot be mandatory  to write it as mentioned you need to write it like
                                        Int i=27;

** you cannot change the value of a constant(marked as final and static).
Share on Google Plus

About Bittu choudhary

A Software Engineer and SEO Expert.Self-Employed and Making Money through google.