- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Private Methods in Java 9 Interfaces
Following is an example displaying how to use private methods in Java 9 Interfaces −
Example
interface my_int{ public abstract void multiply_vals(int a, int b); public default void add_vals(int a, int b){ sub_vals(a, b); System.out.print("Default method result "); System.out.println(a + b); } private void sub_vals(int a, int b){ System.out.print("Private method result "); System.out.println(a - b); } private static void div(int a, int b){ System.out.print(" Private static method result "); System.out.println(a / b); } } public class my_new_int implements my_int{ @Override public void multiply_vals(int a, int b){ System.out.print("Abstract method result "); System.out.println(a * b); } public static void main(String[] args){ my_int in = new my_new_int(); in.multiply_vals(11, 34); in.add_vals(78, 0); } }
Output
Abstract method result 374 Private method result 78 Default method result 78
An interface named ‘my_int’ is defined, that has an abstract function, without a body. Another default function is defined, that basically adds two numbers. Another function named ‘sub_vals’ is defined, which subtracts the two numbers.
Another static function named div is defined that divides the two values. A class named ‘my_new_int’ implements the previously defined interface. It overrides the function that multiplies values and redefines it. In the main function, an instance of the interface is created, and the ‘multiply_vals’ function is called by passing specific values. Similarly, the ‘add_vals’ function is also called by passing specific values. The respective output is displayed on the console.
- Related Articles
- @SafeVarargs annotation for private methods in Java 9?
- Can we use private methods in an interface in Java 9?
- Why do we need private methods in an interface in Java 9?
- Java 8 default methods in interfaces
- Java 8 static methods in interfaces
- What are the rules for private methods in an interface in Java 9?
- What are the advantages of private methods in an interface in Java 9?
- Can interfaces have Static methods in Java?
- Can we override private methods in Java
- Can I overload private methods in Java?
- Private and final methods in Java Programming
- Private Methods in C#
- Private and final methods in C#
- Can we have a private method or private static method in an interface in Java 9?
- What are the core interfaces of Reactive Streams in Java 9?
