Studying Java 8 (StaticIF and Default methods)

It's my first post, so it's easy to get used to. Even though Java 9 has been released, I think it's Java 8 now.

First, create an IF with StaticIF and Default method.

package java8.staticIfAndDefaultIf;

/**
 *For testing StaticIF and default methods (studying Java8)
 * @author komikcomik
 *
 */
public interface StaticIF {

	/**This is the default method*/
	public default void helloDefault() {
		System.out.println("hello Default");
	}

	/**It's a normal method*/
	public void hello();

	/**Static method*/
	public static void helloStatic() {
		System.out.println("hello static");
	}

}

Well, it seems that static methods can be implemented in IF like this. The following is the implementation of the above Static IF interface.

package java8.staticIfAndDefaultIf;

/**
 *StaticIF implementation class.
 * @author komikcomik
 *
 */
public class StaticIFImpl implements StaticIF{

	@Override
	public void hello() {
		System.out.println("hello in Impl");
	}

}

By the way, another one. I overridden the default method.

package java8.staticIfAndDefaultIf;

/**
 *StaticIF implementation class.
 *I overridden the default method.
 * @author komikcomik
 *
 */
public class StaticIFImplDefaultOverride implements StaticIF {
	@Override
	public void hello() {
		System.out.println("hello in ImplDefaultOverride");
	}

	public void helloDefault() {
		System.out.println("override default");

	}
}

Finally, I created a class to call these two classes.

package java8.staticIfAndDefaultIf;

/**
 *A person who calls two classes that implement StaticIF.
 * @author komikcomik
 *
 */
public class StaticIFCaller {

	public static void main(String[] args) {
		StaticIF staticIF = new StaticIFImpl();
		staticIF.hello(); //It comes out that it was implemented with StaticIFImpl.
		staticIF.helloDefault(); //The staticIF dafault method is displayed.
		StaticIF.helloStatic(); //The static method of StaticIF appears.

		StaticIF staticIF2 = new StaticIFImplDefaultOverride();
		staticIF2.hello(); //It comes out that it was implemented with StaticIFImplDefaultOverride
		staticIF2.helloDefault(); //The contents of StaticIF have been overridden and implemented with StaticIFImplDefaultOverride.

	}
}

Below is the execution result. That's as expected.

hello in Impl
hello Default
hello static
hello in ImplDefaultOverride
override default

Isn't it possible that the default method cannot be used in the case where the same process is reluctantly written in a subclass that inherits IF? StaticIF didn't seem to be very useful.

Recommended Posts

Studying Java 8 (StaticIF and Default methods)
[Java] Generics classes and generics methods
Java methods and method overloads
Java abstract methods and classes
Studying Java ―― 3
About Java static and non-static methods
Studying Java ―― 9
Java methods
Studying Java ―― 4
Studying Java ―― 1
Java methods
Studying Java ―― 8
Studying Java ②
Java generics (defines classes and methods)
Studying Java ―― 7
Studying Java 8 (String Joiner and join)
Studying Java ―― 2
Studying Java ①
Studying java9 (dropWhile, takeWhile and Stream.ofNullable)
Studying Java -10
Java programming (classes and instances, main methods)
JAVA learning history abstract classes and methods
Java class methods
Studying Java 8 (Optional)
Functions and methods
Studying java9 (jShell)
Java and JavaScript
XXE and Java
Studying Java 8 (Stream)
Check static and public behavior in Java methods
Mixin test cases with JUnit 5 and default methods
How to access Java Private methods and fields
Getters and setters (Java)
Studying Java 8 (Collector / Collectors)
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
Studying Java 8 (see method)
Studying Java 8 (see constructor)
Java --Serialization and Deserialization
[Java] Arguments and parameters
Ruby variables and methods
Studying Java ~ Part 8 ~ Cast
timedatectl and Java TimeZone
[Java] Branch and repeat
Studying Java 8 (lambda expression)
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
Define abstract methods in Java enum and write each behavior
Think about the differences between functions and methods (in Java)
Study Java # 2 (\ mark and operator)
Java version 8 and later features
[Java] Difference between == and equals
[Java] Stack area and static area
(Note) Java classes / variables / methods
Java programming (variables and data)
Java encryption and decryption PDF
rails path and url methods
Check Java9 Interface private methods
Java class definition and instantiation