The following article is mainly focused at beginners who are not much familiar to Java. It can also be a quick recap tool for Java Developers.
What are Interfaces?
Interfaces are 100 percent abstract classes i.e it defines only abstract methods and constants. But while an abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods. Interfaces can be implemented by any class from any inheritance tree.
Why do we need Interfaces?
Interfaces help in implementing “Multiple Inheritance” the Java Way. Now what exactly does it mean by “Multiple Inheritance”? The Multiple Inheritance, one in C++, is allowing a particular class to extend/inherit from more than one class. Multiple Inheirtance can be quite messy and lead to deadly situation at times.
Say suppose two classes A and B inherit from same super class Base and some other class C inherits from both classes A and B- This is what Multiple Inheirtance is all about (Not in case of java, C++ supports this kind of multiple Inheritance) Now suppose a method getName() present in class Base, then it is inherited by both classes A and B and also by class C. Now if we invoke the getName() method using the instance of class C, then there would be a confusion on which version of getName() to invoke- the one inherited from class A or from class B. This issue can lead to a scenario known as “Deadly Diamond of Death”.
This situation can be avoided by use of Interfaces. Java restricts a class to extending only one class but gives the flexibility of implementing multiple interfaces. I had earlier mentioned Multiple Inheritance the Java way. Its nothing but extending a single class and implementing multiple interfaces.
How exactly to go about dealing with Interfaces?
The Following is the definition of Interface Bounceable.
Notice the Bold words. I will get to them later
public interface Bounceable
public void setBounce(int bf);
The following is the Concrete class Ball that implements Bounceable
All objects that can bounce can implement Bounceable
public class Bounce implements Bounceable
int bounceFactor ;
The Method inherited from the Interface has to be implemented.
In other words the programmer has to define the method unless its another Interface or an abstract class
public void setBounce(int bfactor)
To define an interface “interface” key word is used in the class definition header. And once a concrete class implements the interface, its responsibility is to implement all the methods inherited from the Interface, its a contract between the Interface and the implementing concrete class.
Some key points one has to remember regarding Interfaces
1. All interface methods are implicitly public and abstract. One can use any combination of public or abstract or no modifiers.
public void setBounce();
abstract void setBounce();
public abstract setBounce();
abstract public setBounce();
2. All variable defined in the interface must be public, static and final. Any combination of public, static, final or no modifiers is legal.
public static final int BOUNCE_FACTOR=10;
public int BOUNCE_FACTOR=10;
3. Interface methods must not be static because interface defines instant methods.
4. As interface methods are abstract they cannot be marked as final, strictfp or native. If they are marked final they cannot be overridden and methods without a body would make no sense.
5. An interface can extend one or more interfaces. Note this is the only place where extending more than one class is possible. But it cannot implement other interfaces or extend other concrete or abstract classes.
6. An interface has to be declared with the keyword Implements and the keyword abstract is optional and rather considered redundant.
7. Interface types can participate in polymorphism i.e they can be used to declare a reference type and those objects which implement that interface can be assigned to the declared type. (More on Polymorphism in my coming posts)
8. When a concrete class implements a interface all the methods must be implemented and must be marked public.
9. Abstract classes can implement interfaces and it need not provide the implementation for the methods obtained from the interface.
Read about Interfaces Vs Abstract Classes here.
Java Keywords: Green
Class and Interface names: Blue