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);


Watch video Java default method online without registration, duration hours minute second in high quality. This video was added by user kudvenkat 01 May 2017, don't forget to share it with your friends and acquaintances, it has been viewed on our site 6,625 once and liked it 50 people.