Implementation of methods in Interfaces with static and default keyword
Defining default method in interface - Java 8 allows implementation of methods in interface which are otherwise abstract. This is possible with keyword 'default' before methods. They are added to the interface to provide additional functionality to all implementing classes without requiring them to define the method.
Defining static methods in interface - Java 8 also allows to provide implementation of static methods in interface. Otherwise it is not possible to declare static methods in interface because in interface all the methods are abstract implicitly.Since static methods don’t belong to a particular object, they have to be called by using the interface name.
Please see this with following example:
Please see this with following example:
public interface NewIF {
default public void social() {
System.out.println("I am default method from interface");
}
public void operate();
public void calculate();
public static void enquire() {
System.out.println("I am static enquiry from interface");
}
}
public class ClassOne implements NewIF {
@Override
public void operate() {
System.out.println("operate");
}
public static void main(String[] args) {
new ClassOne().social();
NewIF.enquire();
}
@Override
public void calculate() {
System.out.println("calculate");
}
}
default public void social() {
System.out.println("I am default method from interface");
}
public void operate();
public void calculate();
public static void enquire() {
System.out.println("I am static enquiry from interface");
}
}
public class ClassOne implements NewIF {
@Override
public void operate() {
System.out.println("operate");
}
public static void main(String[] args) {
new ClassOne().social();
NewIF.enquire();
}
@Override
public void calculate() {
System.out.println("calculate");
}
}
***************************************************************************************
QACult Best Software Testing Courses in Chandigarh tricity.- We love to enhance your knowledge.
QACult is the premier institute catering to the requirements of experienced and fresh pass-out that gives leaders like you a new way to experience Quality engineering—while you work and giving you the flexibility to both advance your career.
Our faculty have 12+ years of industrial experience and have developed many automation testing frameworks in java using TestNG or BDD (cucumber) methodology. We expertise in developing automation testing frameworks in java, python, javascript, php, ruby(WATIR-webdriver & Capybara) and Appium.
please subscribe our channel for more such updates:
Our faculty have 12+ years of industrial experience and have developed many automation testing frameworks in java using TestNG or BDD (cucumber) methodology. We expertise in developing automation testing frameworks in java, python, javascript, php, ruby(WATIR-webdriver & Capybara) and Appium.
please subscribe our channel for more such updates:
And visit for website: www.qacult.com for various blogs and Upcoming Events.
Comments
Post a Comment