Skip to main content

Default and Static methods inside Interface

As we all know before Java 8 the interface is the only thing that was purely abstract. It means you were not able to define concrete methods inside the interface. Now interfaces are no longer as 100% pure abstract. According to the Java 8 interface definition, you can define concrete methods inside the interface. Java 8 allows you to define default and static methods inside the interface.

What are the default and static methods?

The default method declares with the default keyword at the beginning of the method and having an implementation body. Default methods are only allowed inside the interface.
The static method is just like the method present at the class level. A method declares with the static keyword at the beginning of the method and having an implementation body. Below is an example of default and static method.
One thing you should always remember that anything defines inside the interface by default public and variables present inside the interface are implicitly static and final.

Default and Static methods wrt Inheritance

As we know already an interface can extend one or more than one interface at a time. Now in the case of the interface, there are two situations that come up. The first one is when the child interface extends a single interface and the second one is when the child interface extends more than one interface at a time. Let's take an example as the first situation is very simple there are no loopholes.
Now in the second situation when an interface or a class extends more than one interface then there two cases come up. The first case is if both parent interface having an exact same default method and the second one is if both parent interface having different default method. Let's suppose a class or an interface implements or extends two or more interfaces. Now the implemented class or interface would inherit default methods from both parent interfaces. At this point in the first case, multiple inheritance problems would occur and also known as a diamond problem. Here is an example.
Parent interfaces: HorrorMovies.java and ThrillerMovies.java
Child interface: HorrorThrillerMovies.java
In the above example, if you would comment a method getMovies() then the java compiler will show you a compilation error "Duplicate default methods named getMovies with the parameters () and () are inherited from the types ThrillerMovies and HorrorMovies". Hence the java compiler ended up with the ambiguity problem or you can say Diamond problem.
To resolve this problem you must provide the implementation explicitly to the default methods present in both parent interfaces in your implemented class or interface. By providing implementation you are resolving ambiguity problems and overriding the implementation of default methods of parent interfaces.
Now let's talk about static methods inside the interface. The first thing you should know about static methods is that static methods are not part of the inheritance concept. When you declared the exact same static method in the child class or interface with the new implementation then this is called method hiding.
In Java 8, two interfaces can have the exact same static methods. When a child interface or a class extends or implements two or more than two interfaces having the exact same static method then the java compiler does not show any error like it does in case of default methods. Let's take an example.
Parent interfaces: Hollywood.java and Bollywood.java
Child interface: Tollywood.java
In the above example, if you would comment code inside the Tollywood interface then the java compiler will not show any compilation error like it does in case of default methods.

Now I am ending up this post and will update it if I will find something new about default and static methods. Also, you can share your queries and feedback in the comment section below, and if you find something new about this topic do comment below. 
Soon I will come up with the post on the topics inheritance, method hiding in detail. Keep study guys and share with your friends and colleague also. Thank you so much.

Comments

Popular posts from this blog

How to add an existing project to a GitHub Repository

  Add Existing project to a GitHub Repository Step 1: Login into your GitHub account and create a new repository. Step 2: Go to your project folder on your machine and open git bash. After that runs the command “ git init ”. This command initialized the empty git repository on your local machine. Step 3: Create a file “. gitignore ” using the command “ echo > .gitignore ”. Add the file extension or file name inside this file, those you don’t want to commit to your repository. Git will ignore all those files that will be present inside this file. Step 4: Add all the files to the staging area using the command “git add .” This command adds all the files to the staging area and all these files are now ready for the commit. And with the help of the command “git status” , you can check that all the files are inside the staging area. Also, you can see the file extension or the file name mentioned inside the “.gitignore”  are ignored by git. Step 5: Commit the files that are pre...

Lambda Expression

What is Lambda Expression? In java 8, a powerful concept is added named Lambda Expression.  Lambda expression is an anonymous function (a nameless function that does not have a modifier, return type, and even the type of its arguments).  Lambda expression is that you can pass to a method whose argument type is functional interface i.e. an interface that is either annotated by @FunctionalInterface  annotation e.g. interfaces from java.util.function package, or an interface with a single abstract method e.g. Comparable, Comparator, Runnable, Callable, ActionListener, etc.  Lambda expression allows passing customized code to the java method that was not possible before java 8. Although before Java 8 the only option was to wrap your code as an object and pass it using anonymous inner class to the method. Lambda expression defines parameters and code that executed using those parameters. Let's take the simplest example of the lambda expression (String input) -> ...

How to install IntelliJ IDEA on Windows?

System Requirements and steps to install IntelliJ IDEA on Windows System Requirements:  Step 1: Download IntelliJ IDEA. Click here. Step 2: Run the downloaded exe file and click on next. Step 3: Check the checkboxes mentioned in the below image and click on next and then hit the install button. Step 4: Final step is to reboot your system before start using IntelliJ IDEA. Thank you, guys. Please do comment below if there is any query or feedback.