An article about interfaces and their usage in Java language
What is an interface
Interfaces in Java language are used to define a reference type and consist of abstract methods,constant values and other nested reference types.Moreover,interfaces cannot be instsansiated and they can only be implemented by a concrete class or extened by another interface.While a class can extend only one class,it can implement more than one interfaces and in this way we can achieve multiple inheritance in Java Language.What we actually achive with interfaces is to define a certain behavior for the implementing class.In other words with interfaces we define what a class must do without telling anything about how to do it
Working with interfaces
When a class implements an interface must provide a concrete implementation for all the methods contained in the interface.For example, suppose we have the below interface:
public interface TestInterface{
public void testMethodOne();
public void testMethodTwo();
public void testMethodThree;
}
If a class must implement TestInterface then all three methods ( testMethodOne(),testMethodTwo(),testMethodThree()) must be implemented in the class.
Note that in TestInterface we declared its methods as public.We could have omitted the public identifier sice all methods of an interface are abstract and public.Moreover we could have declared TestInterface as abstract and public but,since an interface is by definition abstract,we can omit theabstract identifier.
As it was mentioned earlier ,apart from methods, an interface may contain only contsant variables.In other words, any variable declared in an interface must be public, static and final.Since any fields declared in an interface are contstants we can omitt public static and final modifiers.
For example the below contats declarations are both valid
public interface TestInterface{
public static final int CONSTANT1=1;
int CONSTANT2=2;
public void testMethodOne();
public void testMethodTwo();
public void testMethodThree;
}
It is worth mentioning that when a class will implement the above expanded TestInterface will inherit its constants and use them but cannot alter their values.
When an interface extends an other interface
When an interface extends another interface it inherits all its methods and constants.For example consider the folloing snippet:
public interface TestInterface{
public static final int CONSTANT1=1;
int CONSTANT2=2;
public void testMethodOne();
}
public interface NewTestInterface extends TestInterface{
public void newTestMethod();
}
In the above example NewTestInterface extends TestInterface and declares one new method.Now,when a class will implement NewTestIterface must provide an implementation not only for newTestMethod() but also for testMethodOne() that was declared in TestInterface and of course will have access to TestInterface's constants
When an abstract class implements an interface
When an abstact class implements an interface it can provide implementation for all,some or none of the methods declared in the interface.For example suppose we have an interface and an abstract class that implemenets that interface:
public interface InterfaceToBeImplemented{
public void testMethodOne();
public void testMethodTwo();
public void testMethodThree();
}
abstract class ThisIsAnAbstractClass implements InterfaceTobeImplemented{
public void testMethodOne(){/*implementation code goes here*/};
public void testMethodThree(){/*implementation code goes here*/};
In the above example the abstract class provides an implementation for testMethodOne() and testMethodThree() but not for testMethodTwo().The above snippet will compile normally and without throwing errors.Now, when a concrete class will extend ThisIsAnAbstractClass,it must provide an implementation for testMethodTwo.
Differences between interfaces and abstract classes
The main difference between an interfaces and abstract classes is that abstract classes may have one or more abstract methods but can also have one or more implemented methods.On the other hand an interface consists of only abstract methods
Another difference is that while abstract classes can contain instance and class variables,an interface may contain only constants
Moreover,as it was mentioned above, a class may implement more than one interfaces but can extend only one abstract class.
No comments:
Post a Comment