How do default and static methods work in java 8 interfaces? -


i have been trying head around on how default , static methods work in java 8?

consider following interface:

public interface car {    default void drive() {     system.out.println("default driving");   }    static int getwheelcount(){     return wheelcount;   }    int wheelcount = 7; } 

and following implementation:

public class benz implements car { } 

now if go main method , write:

public static void main(string[] args){   car car = new benz();   car.drive();   system.out.println(car.getwheelcount());   system.out.println(car.wheelcount); } 

i know going on under hood:

  1. does default method called upon instance of car in way similar how abstract classes work?
  2. what new features/modifications did language need in order support default , static methods in interfaces?
  3. i know default, fields in interface default public static final, in way related above questions.
  4. with introduction of default methods, have need of abstract classes anymore?

p.s.
please feel free edit question make more useful fellow users.

  1. yes.

  2. java interface default methods in extending interfaces without having fear of breaking implementation classes.

what if computer-controlled car manufacturers add new functionality, such flight, cars? these manufacturers need specify new methods enable other companies (such electronic guidance instrument manufacturers) adapt software flying cars. these car manufacturers declare these new flight-related methods? if add them original interfaces, programmers have implemented interfaces have rewrite implementations. if add them static methods, programmers regard them utility methods, not essential, core methods.

  1. afaik, static methods aren't needed override, being final of static methods coherent. overriding depends on having instance of class. static method not associated instance of class concept not applicable. however, default methods must have overrideable property quote above.

  2. can have default ctor, private fields, instance members in interface in java 8?


i use default methods,

list.sort(ordering); 

instead of

collections.sort(list, ordering); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -