Home Java Java Interface and Various Types of Interfaces

Java Interface and Various Types of Interfaces

Published: Last Updated on 0 comment

Hey, Tea lovers! before talking about Java Interface, Have you ever heard the phrase, “Man Of Many Faces”? if not, it means a person can be of everything or anything when it comes to portraying something. It can be one thing and a minute after another without losing its strength.

Java’s interface fits perfectly with this title. It is an integral part of java and has become so powerful over the years, that it shifted the way a Java programmer thinks. It has added various ways of doing things in Java. Depending on the number of methods only, an interface’s definition as well as the object-creating process changes.


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


Usual or Normal Java Interface

By definition, it is a template for classes through which you tell classes what you need to do and NOT how to. These contain abstract functions or methods that, when implemented, need to be overridden. 

//usual java interface
 interface NormalInterface {
   int overrideThisFunction();
 }
 class ImplementNormalInterface implements NormalInterface {
   @Override
   public int overrideThisFunction() {
     //Do something
     return 0;
   }
 }Code language: PHP (php)

Default Function in Java Interface

Now, as I said, It is a template that tells you what to do and not how to. Well, sometimes you need to tell someone how to do things, don’t you? A typical scenario would be when an interface is already published or it is being used in a project far away and you need to add one more function to it. Now if you add one more abstract function to it then boom, now classes can’t be compiled since they need to override this new function. If we use the default function, it lets us define the default behavior for the function in case classes can’t override them. However, you can override them if you want to.

Look at newly added functions on  Collection classes such as stream(), parallelStream() ,  spliterator()  and many more. It provided you with the functionality without actually implementing it in the classes.

interface WithDefaultFunction {
   int overrideThisFunction();
   default int noNeedToOverride() {
     return 1 + 1;
   }
 }
 class ImplementWithDefaultFunction implements WithDefaultFunction {
   @Override
   public int overrideThisFunction() {
     return 0;
   }
 }
 class ImplementWithDefaultFunction2 implements WithDefaultFunction {
   @Override
   public int overrideThisFunction() {
     return 0;
   }
   //There is no need to override
   //Just in case you want to change the default behaviour
   @Override
   public int noNeedToOverride() {
     //just changed default behaviour
     return 2 + 2;
   }
 }Code language: PHP (php)

Static Function

Static functions in the interface are somewhat different. They have a method body but can’t be overridden.  After all, static in Java means, it remains the same for all the instances of the class.  You can have a static method of the same name and parameters, but it will not override the interface’s function. Static functions are called by interface only and no need to create instances of them.

interface StaticFunction {
   static String getAwesomeWebsite() {
     return "coderstea.in";
   }
   void overrideThis();
 }
 class ImplementStaticFunc implements StaticFunction {
   //there is no option of overriding static function
   //this is not the overridden function
   static String getAwesomeWebsite() {
     return "https://coderstea.in";
   }
   @Override
   public void overrideThis() {
     /<em>do something</em>/ }
   public static void main(String[] args) {
     System.out.println("The best website is :" + StaticFunction.getAwesomeWebsite());
   }
 }Code language: PHP (php)

Functional Interface of Java

You can add the above functions in a single interface, it doesn’t matter how many default or static functions it has. However, the number of abstract methods (without a body) affects the interface. More than one is a normal interface, but with only one abstract method, it is a Functional Interface. A functional interface must have only one abstract method. Only the functional interfaces can be used by lambdas. You can have multiple defaults and static methods in a functional interface, with no issues. It will be a functional interface as long as the abstract method count is one.

In Java 8, adding lambda support on these functional interfaces has given rise to a new approach in Java, and that is Functional Programming. It surely  makes the code less cluttered and with the Stream, it changed the way a Java programmer thinks. 

But keep in mind, Lambda only works with the functional interface, and a functional interface only has one abstract method. Java has an annotation, @java.lang.FunctionalInterface which at compile-time checks for these conditions for the functional interface, otherwise throws the error. It has to be put on the interface only.

@FunctionalInterface
 interface FunctionalInterfaceDemo {
   void iAmTheOnlyOne();
   default void iAmAlsoHereButNotAnAbstractMethod() {}
 }
 class ImplementFunctionalInterface implements FunctionalInterfaceDemo {
   @Override
   public void iAmTheOnlyOne() {
     //normal overriding
   }
   public static void main(String[] args) {
     FunctionalInterfaceDemo anonymous = new FunctionalInterfaceDemo() {
       @Override
       public void iAmTheOnlyOne() {
         System.out.println("inside lambda");
       }
     };
     FunctionalInterfaceDemo useLambda = () -> System.out.println("inside lambda");
     useLambda.iAmTheOnlyOne();
     anonymous.iAmTheOnlyOne();
   }
 }Code language: PHP (php)

Java has inbuilt functional interfaces, primarily used in Stream APIs, such as Function, BiFunction, Predicate and many more which you can learn more about in my other post “Be More Functional with Java’s Functional Interfaces“. And to learn more about the Stream API, which I like to call “The Hero Without a Cape”, here

Conclusion

So these were the many faces or simply put, an interface’s various parts in general. How a single interface changes its behavior with just a certain number or keyword attached to a method? I hope you liked the post and please do comment if I have missed something. Any help would be appreciated.

You can find the code on GitHub here or the full project here. See you at your next tea time.

See you in the next post. HAKUNA MATATA!!!


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Ads
Ads
Ads

@2023 All Right Reserved. Designed and Developed by CodersTea

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More