Introduction into Java interfaces

Java, defines an interface as :

a group a related methods and constants which specifies a contract for a class to implement.

The definition of an interface starts with the keyword interface. In Java 7, an interface can define only abstract methods and constants. Interfaces methods cannot contain any implementation and are implicitly public. The variables of an interface are implicitly public, final, and static.

“All members of an interface are implicitly public.”

Even if, Java does not allow a class to inherit from more than one class, it allows a class to implement multiple interfaces. A class uses the keyword implements to implement an interface.

Keep in mind : When an interface extends another interface, it inherits the methods defined in the base interface.

“Take away : When you implement an interface, you must implement all its methods by using the access modifier public. (i.e, A class that implements an interface can’t make the interface’s methods more restrictive)!”

Exam Tip : The method signatures of a method defined in an interface and the class that implements the interface must match. Otherwise, the class won’t compile!

Add a Comment

Your email address will not be published.