Interface

Interface

A interface is like a class where non of the methods have a body(called a abstract method) so a common analogy that is used for interfaces is that it’s like the blueprint of a class. Which basically means that tells what a class has to do but not how.So the main purpose of a interface is either to use polymorphism more effectively or to have interaction between to classes be based on the interfaces abstract methods which one of the classes implements since if the class implements the interface then it must define the methods given otherwise it becomes a abstract class and Java doesn’t support objects of abstract classes. (So by implementing the interface, the class is essentially promising that it will define the methods in the interface in that class or a subclass of that class)

A interface is made as shown below :

public interface interfaceName

{

returntype methodName(type parametername);

}

You can also make variable constants, but this isn’t recommended because the constants of different interfaces if given the same name and implemented by the same class could cause the program not to know which to use and cause a error.

In JDK8 and JDK9 there have been some updates to interface for more info on that you can look at the geek for geek article on interfaces( in case you didn’t realize it I’m not a professional so I do actually try to double check that my info is right before posting a article.)

Or you can wait till I add that information to here.

Some related topics are:

inheritance:

classes(not done yet):

One thought on “Interface

Comments are closed.