UrbanPro
true

Take BCA Tuition from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Default Methods in Java 8

A
Arundhati Pathak
09/12/2016 0 0

Java 8 was released in early 2014 (18th March 2014). Java 8 is a product version. It's development version is JDK1.8 ( Java Development Kit ). Java 8 has some new features that are most awaited in the development environment.

Let us discuss today one of the features of Java 8 that is:

Default Methods -
We know that interfaces does not have method body. Classes which implement the interface has to define the method. Java 8 comes with the feature where interface methods could have a method body. Methods implemented in interfaces  use default keyword. 

For Example : 

interface A
{
double calculate(int a);
default double cube(int a){
return (a*a*a);
}
}


class Myclass
{

public static void main(String str[])
{


A a = new A(){

public double calculate (int a)
{
return cube(a);
}
};

double di = a.calculate(6);
System.out.println(di);
}

In this example, method calculate is the abstract method of the interface and cube method is default method. Abstract method has to implement by the class but the default method can be used directly.

The default keyword suggests that the class which implements the interface does not need to implement the method. It can use the default definition of the method provided by the interface. If we want to provide different implementation for the default method, we have to implement the method in class.

For Example -

interface printable{
default void printString(String s){
System.out.println("Interface default method");
}
}

class Test1{
public static void main(String args[]){
printable p =new printable(){
public void printString(String str)
{
System.out.println(str);}
};
p.printString("Method implemented to provide other definiition ");
}
}

In this example, interface printable have one default method printString. The class implements the method to provide different definition.

In java, interface is introduced to overcome the problem of multiple inheritence. When there are common methods in extended classes then it creates an ambiguity. to avoid this , java does not support multiple inheritance. But with the introduction of default method, the same problem can arrise when class implements more than one interfaces having common methods.

For example - 

interface interface1{
void method1();
default void printStr(String str)
{
System.out.println("Interface1 print string method" + str);
}
}

interface interface2{
void method2();
default void printStr(String str)
{
System.out.println("Interface2 print string method" + str);
}
}

class myClass1 implements interface1,interface2
{
@Override
public void method1(){
}

@Override
public void method2(){
}

@Override
public void printStr(String str){
System.out.println("Myclass print string method " + str);
}

public static void main(String str[]){
myClass1 m = new myClass1();
m.printStr("HELLO");
}

}

In above example, interface1 and interface2 both have common method printStr. Class myClass1 is implementing both interfaces. In this situation, compiler will trow an error. It is difficult for compiler to decide which method to call. 

To solve this problem, java 8 makes it mandatory to implement the common method. In our example , we have implemented the common method in class myClass1. So by impementing the common method we can solve the problem occured by implementing more than one interfaces.

If you want to use the default implementation provided by one of the interfaces, you can use the super keyword. In this case you don't have to provide the different implementation you can just use the default method provided by one of the interface.

For example - 

class myClass1 implements interface1,interface2
{
@Override
public void method1(){
}

@Override
public void method2(){
}

@Override
public void printStr(String str){
interface1.super.printStr(str);
}

public static void main(String str[]){
myClass1 m = new myClass1();
m.printStr("HELLO");
}

}

In this example we just change the printStr method. The method calls the printStr method of interface1 using the super keyword.

The main advantage of default method is that it provides backward compatibility. In earlier versions , interfaces are abstract and concrete. If we have to add any method in the interface, we have to change all classes that have impelmented the interface. This is a very difficult task. But default method solves this problem. Now we can add methods in interfaces and use them directly without affecting the existing classes. classes which already had implemented the interface does not have to implement the new method if it is a default method. 

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Some interview questions and answers for fresher level on Pointers
What is a void pointer? Void pointer is a special type of pointer which can reference or point to any data type. This is why it is also called as Generic Pointer. As a void pointer can point to...


Key Tips On How To Study Smart
The study is a method in which we gain knowledge which in turn leads to the broadening of our mindset and the significance of it is that we implement this knowledge in our daily life which is called common...

Finding a Majority Element
Problem Description Task. The goal in this code problem is to check whether an input sequence contains a majority element. Input Format. The first line contains an integer, the next one contains a sequence...

Java Interview Questions & Answers
Java Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Java Programming Language. As per my...

Looking for BCA Tuition ?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for BCA Tuition Classes?

The best tutors for BCA Tuition Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Take BCA Tuition with the Best Tutors

The best Tutors for BCA Tuition Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more