An interface can extend other interface but cannot implement any interface. This makes sense because interface cannot have any implementation. An interface can only contain abstract methods that are implemented by the class implementing that interface.
Lets do this with an example.
Create a package named myinterfaces. Using conventions, package name should be in small case. Now create an interface named InterfaceA with 2 methods. Java Code: package myinterfaces;
ActionListener interface is used to perform actions on a performed event. In this post, I will write about its importance and usage.
ActionListener interface is part of java.awt.event package. It has only one abstract method that has to be implemented in the class implementing this interface.
void actionPerformed(ActionEvent e)
Consider the following scenario: you developed an GUI form for an application comprising of text fields,
Interfaces are used to define a contract for the classes. It has its own importance when it comes to defining standards.
Consider the following example:
Cars have some standard and their basic operations are the same. Each car should have operations like startCar, moveCar, stopCar etc. Toyota, Mazda and Honda are cars and each has its own way of starting, moving and stoping. So each has to have its own startCar, moveCar and stopCar method.
How to enforce
Interface is very much similar to the Abstract class in Java but the difference is that in interfaces, members (methods) cannot be implemented, member(fields) defined will be treated as constants.
A class becomes more formal about its behavior after implementing an interface. Interfaces are actually a contract between the class and the outside world. While implementing an interface in a concrete class, you have to implement all the methods defined by that interface. If you miss any method,
This post lists some facts about factory methods.
Factory methods are static methods that return an instance of the native class. Some examples from JDK include : LogManager.getLogManagerPattern.compileCollections.unmodifiableCollectionCollections.synchronizeCollectionCalendar.getInstance
Factory methods do not have names like constructors and do not need to create a new object upon each invocation. Objects can be cached and reused, if
When you are designing your API, you might want to use interface since this seems a common practice. There exist a style in Java programming where everything is expressed in terms of Java interfaces instead of classes.
Using interfaces provides benefits but its not a good idea for an entire API to be expressed in terms of them.
Use interface as a type if you have a good reason for it to be. Following are some important facts to be considered: