Skip to main content

Private Methods in Interfaces

In Java 8 we were able to use method implementation in interfaces as default methods. But the private methods were missing in Java 8 when the default method is added.  So now that both default and private methods can exist within an interface. Before we are going to the details we can check the changes came for the interfaces in the previous versions

Java 7

In Java 7 we can provide only two things in interfaces
  • ·         Constant variables
  • ·         Abstract methods
If we need to use the method implementation, then we should go for the abstract class.

Example:
public interface SampleInterface_7 {
     
      String CONSTANT_1 = "CONSTANT_1";
     
      public void method1();

}

Java 8

In Java 8 we can provide following things in interfaces
  • ·         Constant variables
  • ·         Abstract methods
  • ·         Default methods
  • ·         Static methods

Default Methods

We need to use ‘default’ keyword with the signature to use the default method in interface. If we define a default method in interface, it is not mandatory to provide implementation for the default method.  If two interfaces have same default methods and if a class implementing both interfaces, then compiler will not be able to choose which method is to choose. Hence it’s mandatory to provide the implementation for the default method in this scenario.

Static Methods

Static methods are similar to default methods, but cannot override in the implementation classes.

Example:
public interface SampleInterface_8 {
     
      String CONSTANT_1 = "CONSTANT_1";
     
      public void method1();
     
      default void default_method() {
            System.out.println("This is default method");
      }

      public static void method2() {
            System.out.println("This is a public static method");
      }
}

Java 9

In Java 9 an interface can have six kinds of things
  • ·         Constant variables
  • ·         Abstract methods
  • ·         Default methods
  • ·         Static methods
  • ·         Private methods
  • ·         Private Static methods
‘abstract’ keyword cannot use with private methods. Private methods can be used only inside interfaces. And it can be used inside the static and non-static interface methods.
Example :
public interface SampleInterface_9 {
     
String CONSTANT_1 = "CONSTANT_1";
     
      public void method1();
     
      default void default_method() {
            System.out.println("This is default method");
            /**
             * Private method can be called from default method
             */
            method3();
      }

      public static void method2() {
            System.out.println("This is a public static method");
            /**
             * Static method can be called from static method
             */
            method4();
      }    
     
      private void method3() {
            System.out.println("This is private interface method");
      }
     
      private static void method4() {
            System.out.println("This is private static interface method");
      }

}
Sample Code is available at

Comments

Popular posts from this blog

Visual VM tutorial

VisualVM is a visual tool available with JDK to monitor and analyze the performance of Java applications. Accessing Visual VM  In Windows   /bin/jvisualvm.exe  In Linux  /bin/jvisualvm  By default the Locally executing Java applications will be listed under Local node of Visual VM Visual VM – Remote Monitoring The following steps will help the user to monitor the remote application from windows machine Step 1 : Add following in remote application JVM argument -Dcom.sun.management.jmxremote.port=5555 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false Step 2 : Add Remote Host in local Visual VM Step 3: Step 4:   Step 5 : Enter the JMX port given as JVM argument Step 6 : Double click on the newly added node  This will display the JVM details in the right hand side.

Java Heap Dump Analyzer

Overview Identifying reason for the out of memory error of java applications with larger heap size may be one of the nightmares of a developer, because most of the out of memory situations may not be identified during the testing phase. It may occur only in production after running for a long time. The purpose of this article is to explain the use of Heap analyser tool to identify the memory leakage of larger enterprise java applications, which uses larger size of heaps. Before going to the details we will discuss about following points.          Heap       Garbage Collection           Out of memory What is heap? The space used by the Java Runtime to allocate memory to Objects and JRE Classes is called Heap. The heap space can be configured using the following JVM arguments. o    -Xmx<size> - Setting maximum Java heap size o    -Xms<size> - Setting initial Java heap size The maximum heap size can be configured in a 32 bit JVM is 2GB. If any

How to take thread dump of a running application in Oracle and IBM Java ?

A thread dump will help a developer to identify the application hanging issues and performance issues. To analyze this, please take, a continuous snapshot of thread dumps and analyze using a thread dump analyzing tool. In Oracle Java you can use jstack tool. This executable is available in the JAVA_HOME/bin folder. Use the following command ' jstack -l <PID> >> threaddump ' . This will write the thread dump into a file 'threaddump' in the directory from you have executed the command. <PID> should replaced with actual process id. You can use the following command to find the process id in Unix. ' ps -ef | grep java ' . In IBM Java you can use ' kill -3 <PID> ' . This will write the thread dump in application running console or in nohup file or a thread dump file will be generated in the program running directory.