In java, interfaces are used to create a blueprint for child classes that illustrate what the class is supposed to do. Interfaces in java are similar to inheritance, however contrary to inheritance, classes can apply more than one interface but inheritance is limited to one super class, and this is known as multiple inheritance.
What is an interface in Java and why would you, as a software developer, use interfaces?
An interface is a template or blueprint that which specifies what a class has to do. Interfaces give programmers a more competitive advantage when it comes to Object-Oriented Programming. It could, for example, enable programmers to create classes of similar traits(variables and methods) whilst also giving them the ability to have their own unique functionality.
Interfaces allow us to declare variables and methods which do not have a body so classes that implement the interface and inherit these methods can define what these methods are going to do. This is what allows us to create similar methods in different classes with unique functionality.
Let's think of an interface to serve as a template for various animal classes. The interface class could hold variables for different but common traits of the animals and methods for certain behaviors. The classes implementing the interface will each have these traits but they will be unique for every class e.g a method for motion() declared in the interface class, if a class Fish implements the interface, the motion() method in the class Fish can contain statements for how the fish swims or a bird class where the method holds statements for how the bird flies.
here's a code example:
public interface Animal{
motion()
}
public class Fish implements Animal{
motion() {
System.out.println("This fish swims!");
}
}
public class Bird implements Animal{
motion() {
System.out.println("This bird flies!");
}
}
What is the difference between an abstract class and an interface?
Although similar, there very distinct differences between a abstract class and an interface.
Both an abstract and an interface class allow child classes to inherit certain variables and methods (without bodies), however, the abstract class cannot be instantiated or referenced by other classes but they can have subclasses/ child classes that can implement them. This provides a layer of security to the program for programmers if the abstract class contains methods that are too vague or if they don't want them to be altered or accessed as easily as an interface class. Interfaces can't be instantiated either but they can be referenced when instantiating their child classes.
Another difference is that an interface cannot implement an abstract class but an abstract class can implement an interface. The interface class is declared using the 'interface' keyword and the abstract class uses the 'abstract' key word. Abstract classes are implemented with the 'extends' keyword whereas interfaces use the 'implement' keyword.
Why is abstraction an important concept in software development and what role do interfaces play in abstraction?
Abstraction allows us to only show basic information or details about a program to the user without showing the complexity of it's implementation, making it easier to understand. It is one of the most important concepts of Object-Oriented Programming and for software design because it allows for better organization, makes applications easily extendable, allows for easier application maintenance and refactoring due to the evident connection between subclasses inheriting from the abstract class.
Interfaces have an important role in abstraction as well, because they provide a way to carry details of classes that implement the interface without having to repeatedly declare variables or methods for different classes and another major advantage is that they also allow classes to inherit multiple interfaces.
What must a class do in order to implement an interface?
To implement an interface, a class only has to include the 'implements' keyword followed by the interface name when declaring the class name e.g. 'public class Fish extends Animal{|}' where animal is the interface class and Fish is the implementing class. This will allow the implementing subclass to inherit all the methods or variables of the interface class as well as implement them.
What is an abstract method?
An abstract methods are methods that are declared without implementation (i.e without a body) and contain the keyword 'abstract' in the method signature.
Can you instantiate an interface?
We cannot instantiate an interface in java, however we can have a reference to the interface but you don't create an object of interface. If you do something like this.
Can you declare a constructor inside an interface? If not, why?
No! We cannot have a constructor within an interface. In java, interfaces are public abstract by default and cannot be implemented by the interface. This can only be done by the subclass implementing the interface. Thus, interface constructors are not necessary because an interface object(If they could be instantiated) would not be able to call their own methods in the interface class.
Can we override an interface method with visibility that is not public?
The answer is No! We cannot override an interface method if its visibility is not set to public. However if the interface is set to public, any subclass that implements the interface could override the method could override the method if it has the same public access modifier in it's signature.
Conclusion
This article has covered a great deal on interfaces and the difference between interface classes and abstract classes and how both of these have an important role in the Java Object-Oriented Programming by enabling programmers to work more securely and efficiently with the use of these classes.
I hope you enjoyed the article, Till next time!
References:
Paul J. 2021, Difference between Abstract class and Interface in Java 8?, Java67, viewed 26 October 2021,
https://www.java67.com/2017/08/difference-between-abstract-class-and-interface-in-java8.html
Golan E. 2014, Why Abstraction is Really Important, dzone, viewed 16 October 2021, https://dzone.com/articles/why-abstraction-really
Singh C. 2021,Java – Constructor in Interface?, Beginner Books, viewed 26 October 2021, https://beginnersbook.com/2013/12/java-constructor-in-interface/