Java: Interfaces
Interfaces in Java
An interface is a 'blueprint' for the functions available for an object.
Interfaces are used in large-scale java application developments, and the benefits of using interfaces in the developmental phase of an applications are:
- Early establishment of interactions amongst necessary objects without forcing early definition of the supporting objects
- Enables developers focus on their particular development tasks knowing that integration has already been applied.
- Provides flexibility to integrate new interfaces without having to overhaul the entire code.
- Enforcing the contracts between members in the development team to ensure that all objects are interacting as designed to interact.
As a software developer, Interfaces allows for us to implement functions and changes to an existing object without actually changing that particular object. Aside from this, It will allow us to make changes without having to rewrite the entire object with the implemented changes. Thereby making the designing process simpler and more flexible.
It also makes it simpler for a software developer to identify and isolate bugs and implement fixes.
Lets take a look at an example of implanting an interface:
This example was rooted from infoworld.com - you can get more in depth information by clicking on the link
We notice the interface Vehicle has been declared, with the method start(), thereafter we define the class object - in this case, Car, which implements (it will derive its functions from) the Vehicle interface, thereby adopting all methods declared within the interface. The values defined within the parameters of public void start() will be specific to Car.
We could also diversify the vehicle interface by defining a 'Truck' class that implements Vehicle, thereby adopting start(), which we then code specific to all 'Truck' instances and objects.
In order to create an interface, the programmer needs to include the keyword 'implements' when declaring that particular class. (class Car implements Vehicle{ ... })
Abstract Classes
An Abstract Class much like normal classes contain methods, however these methods are abstract methods, and cannot be instantiated.
Abstraction (used in Object Orientated Programming) the process in which a programmer hides all irrelevant data about an object, in order to reduce complexity and improve efficiency, where the 'abstract' object that remains is essentially a representation of the original object.
This is important to developers and programmers because the use of abstraction benefits us in:
- Reduces reading complexity
- Minimises duplication and improves reusability
- Offers security of application (only relevant data is displayed to the user)
- Improve maintainability
- Promotes good modularity
which enables a programmer to easily read, reuse and modify a program while bearing minimum impact on the end-users side.
Below is an extract from Oracle.com library telling us a little more about Abstract Classes.
"An abstract class is a class that is declared
abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, then the class itself must be declared
abstract, as in:public abstract class GraphicObject {
// declare fields
// declare nonabstract methods
abstract void draw();
}
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared
abstract."Abstract Classes VS Interfaces
A summarised view of general differences between abstract classes and interfaced. Sourced from guru99.com.
An interface can be used to define a 'contract' behaviour and it can also act as a 'contract' between two systems to interact where as an abstract class is mainly used to define default behaviour for subclasses, this means that all the classes thereof should have performed the same functionality.
When do we use interfaces?
- If the functionality we are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes.
- Interfaces are a good choice when we think that the API (Application Programming Interface) will not change for a while.
- Interfaces are also good when we want to have something similar to multiple inheritances since we can implement multiple interfaces.
- If we are designing small, concise bits of functionality, use interfaces. If we are designing large functional units, use an abstract class.
When do we use abstract classes?
- If we are using the inheritance concept since it provides a common base class implementation to derived classes.
- If we want to declare non-public members. In an interface, all methods must be public.
- If we want to add new methods in the future, then an abstract class is a better choice. Because if we add new methods to an interface, then all of the classes that already implemented that interface will have to be changed to implement the new methods.
- If we want to create multiple versions of our component, create an abstract class. Abstract classes provide a simple and easy way to version our components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, we must create a whole new interface.
- Abstract classes have the advantage of allowing better forward compatibility. Once clients use an interface, we cannot change it; if they use an abstract class, we can still add behaviour without breaking the existing code.
- If we want to provide common, implemented functionality among all implementations of our component, use an abstract class. Abstract classes allow us to partially implement our class, whereas interfaces contain no implementation for any members.
You can read up more about the difference between abstract classes and interfaces at tutorialspoint.com, who provided the above examples and explanations.
Last Notes on interfaces and abstract classes
- Can a constructor be declared within an interface?
No, an interface in java contains no constructors because all data members in the interface are public static final (by default) and they are constants (they were assigned values at the time of declaring the variables), therefore there are no data members in an interface which would allow them to be initialised through a contractor.
- Can an interface method be overwridden with visibility that is not public?
An interface method cannot be override when its visibility is not public.
If the interface method were public, it can be override by the same method signature(public visibility) whenever the interface is implement to any class.

Comments
Post a Comment