Java default method

Published: 01 May 2017
on channel: 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);


On this page of the site you can watch the video online Java default method with a duration of hours minute second in good quality, which was uploaded by the user kudvenkat 01 May 2017, share the link with friends and acquaintances, this video has already been watched 6,625 times on youtube and it was liked by 50 viewers. Enjoy your viewing!