Thursday, August 15, 2019

Java 8: default methods, static methods, and their differences

Before Java 8, an interface can only have abstract methods. Starting Java 8, you can add default and static methods with executable method bodies. This change allows you to add new methods to an interface without affecting any of the many classes that have already implemented the interface.

Default Method

For example, you have a TestFace interface that are implemented by SportCenter1 and SportCenter2.

public interface TestFace {
      public boolean ableToRun(Person p);
}


public class SportCenter1 implement TestFace {
      private List<Person> memberList = <your member list>;

      public boolean ableToRun(Person p) {
            if (p.getForwardMoveSpeed() > 10) {
                  return true;
            }
            return false;
      }

      public List<Person> getRunnableMembers() {
            public List<Person> runnableMembers = memberList.stream()
                          .filter(p -> ableToRun(p))
                          .sort(Comparator.comparing(Person::getName))
                          .collect(Collectors.toList());

            return runnableMembers;
      }
}


public class SportCenter2 implement TestFace {
      private List<Member> memberList = <your member list>;

      public boolean ableToRun(Member p) {
            if (p.getMaxMoveDistance() > 5) {
                  return true;
            }
            return false;
      }

      public List<Member> getRunnableMembers() {
            public List<Member> runnableMembers = memberList.stream()
                          .filter(p -> ableToRun(p))
                          .sort(Comparator.comparingInt(Person::getAge))
                          .collect(Collectors.toList());

             return runnableMembers;
      }
}

Now, you have a new customer, SportCenter3, who wants to know if their members are able to swim. If you add an abstract method, ableToSwim, to the TestFace interface, you have to modify the code of SportCenter1 and SportCenter2 to make them compatible to the changed interface. Here, the default method came in handy. It provides you the backward compatibility to old code. Your modified interface can look like below, and it will not affect the SportCenter1 and SportCenter2 at all. It is especially convenient if there are a large number of classes using the interface.


public interface TestFace {
      public boolean ableToRun(Person p);

      public default boolean ableToSwim(Person p) {
            return true;
      }
}


public class SportCenter3 implement TestFace {
      private List<Person> memberList = <your member list>;

      public boolean ableToRun(Person p) {
            if (p.ableToRun() && p.ableToClimeMountain()) {
                  return true;
            }
            return false;
      }

     //optional to override the default method in the interface
      public boolean ableToSwim(Person p) {
            if (p.passedSwimmingTest()) {
                  retrun true;
            }
            return false;
      }
}

If your class implementing multiple interfaces and more than one interface have the same default method signature, your implementing class should explicitly specify which default method it is using or override this default method.

Static Method

A static method is another way to add a method to an interface without affecting its implementing children.

public interface TestFace {
      public boolean ableToRun(Person p);

      public default boolean ableToSwim(Person p) {
            return true;
      }

      public static void incrementAge(Person p) {
            p.setAge(p.getAge() + 1);
      }
}

Difference between Default Method and Static Method

1. While a default method can be override by implementing classes, static method cannot be overridden.

2. Static methods belong only to the interface and can only be called from the interface class, while default methods can be invoked from instances of both the interface and implementing classes.

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live




If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.

References:
1. Difference Between static and default methods in interface
2. Default Methods In Java 8
3. Java 8 Interface Changes – default method and static method

No comments:

Post a Comment