Java default method

Veröffentlicht am: 01 Mai 2017
auf dem Kanal: kudvenkat
6,625
50

In this tutorial we will discuss default methods in Java Interfaces. With Java 8, Oracle introduced several new features like functional programming, lambda expressions etc. There was need to extend already existing interfaces like List and Collection. However, one could not simply add new methods to Java interfaces as most of the existing implementations would break. Hence, default methods were introduced for backward compatibility.

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
   / @aarvikitchen5572  

Here we learn about the benefits of default methods in Java, and what rules are applicable on them when such interfaces are extended or implemented. Here is the code used in tutorial

- public modifier indicates that interface can be used in any other interface or class. default modifier is package

public interface ITimeService {
void printTime(int hour, int minute, int seconds);
void printDate(int day, int month, int year);
}

public class SimpleTimeService implements ITimeService {

@Override
public void printTime(int hour, int minute, int seconds) {
System.out.println("Time =] " + hour + ":" + minute + ":" + seconds);
}

@Override
public void printDate(int day, int month, int year) {
System.out.println("Date =] " + day + "/" + month + "/" + year);
}

}


public class Program {

public static void main(String[] args) {
ITimeService timeService = new SimpleTimeService();
timeService.printTime(12, 30, 10);
timeService.printDate(10, 10, 2017);
}
}


default void printDateTime(int hour, int minute, int seconds, int day, int month, int year) {
System.out.println("DateTime =] " + day + "/" + month + "/" + year + " "
+ hour + ":" + minute + ":" + seconds);
}

public class AnotherTimeService implements ITimeService {

@Override
public void printTime(int hour, int minute, int seconds) {
System.out.println("Time =] " + hour + "-" + minute + "-" + seconds);
}

@Override
public void printDate(int day, int month, int year) {
System.out.println("Date =] " + day + "-" + month + "-" + year);
}

@Override
public void printDateTime(int hour, int minute, int seconds, int day, int month, int year) {
System.out.println("DateTime =] " + day + "-" + month + "-" + year + " "
+ hour + "-" + minute + "-" + seconds);
}

}

ITimeService timeService = new SimpleTimeService();
timeService.printTime(12, 30, 10);
timeService.printDate(10, 10, 2017);
timeService.printDateTime(12, 30, 10, 10, 10, 2017);

ITimeService anotherTimeService = new AnotherTimeService();
anotherTimeService.printTime(12, 30, 10);
anotherTimeService.printDate(10, 10, 2017);
anotherTimeService.printDateTime(12, 30, 10, 10, 10, 2017);


Auf dieser Seite können Sie das Online-Video Java default method mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer kudvenkat 01 Mai 2017 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 6,625 Mal angesehen und es wurde von 50 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!