java method default value code example

Example 1: default method in java

public interface SomeInterface(){
	default void test(){
    	//this will be done when the method is called by default
    }
}
public class SomeInterfaceImpl implements SomeInterface{
	//has test method from SomeInterface, can be overwritten
}
public class SomeClass{
	public void test(){
    	//do something else
    }
}
public class SomeOtherClass extends SomeClass implements SomeInterface{
	//uses the test method from SomeClass
}

Example 2: java default values

No, the structure you found is how Java handles it (that is, with overloading instead of default parameters).

For constructors, See Effective Java: Programming Language Guide's Item 1 tip (Consider static factory methods instead of constructors) if the overloading is getting complicated. For other methods, renaming some cases or using a parameter object can help. This is when you have enough complexity that differentiating is difficult. A definite case is where you have to differentiate using the order of parameters, not just number and type.

Tags:

Java Example