Private Methods in Java 9 Interfaces
Last Updated :
02 Aug, 2022
Java 9 onwards, you can include private methods in interfaces. Before Java 9 it was not possible.
Interfaces till Java 7
In Java SE 7 or earlier versions, an interface can have only two things i.e.
Constant variables and Abstract methods. These interface methods MUST be implemented by classes which choose to implement the interface.
Java
// Java 7 program to illustrate
// private methods in interfaces
public interface TempI {
public abstract void method(int n);
}
class Temp implements TempI {
@Override
public void method(int n)
{
if (n % 2 == 0)
System.out.println("geeksforgeeks");
else
System.out.println("GEEKSFORGEEKS");
}
public static void main(String[] args)
{
TempI in1 = new Temp();
TempI in2 = new Temp();
in1.method(4);
in2.method(3);
}
}
OUTPUT : geeksforgeeks
GEEKSFORGEEKS
Java 8 Interface Changes
Some new features to interface were introduced in Java 8 i.e. Default methods and Static methods feature. In Java 8, an interface can have only four types:
- Constant variables
- Abstract methods
- Default methods
- Static methods
Example
Java
// Java 8 program to illustrate
// static, default and abstract methods in interfaces
public interface TempI {
public abstract void div(int a, int b);
public default void
add(int a, int b)
{
System.out.print("Answer by Default method = ");
System.out.println(a + b);
}
public static void mul(int a, int b)
{
System.out.print("Answer by Static method = ");
System.out.println(a * b);
}
}
class Temp implements TempI {
@Override
public void div(int a, int b)
{
System.out.print("Answer by Abstract method = ");
System.out.println(a / b);
}
public static void main(String[] args)
{
TempI in = new Temp();
in.div(8, 2);
in.add(3, 1);
TempI.mul(4, 1);
}
}
OUTPUT : Answer by Abstract method = 4
Answer by Default method = 4
Answer by Static method = 4
Java 9 Interface Changes
Java 9 introduced private methods and private static method in interfaces. In Java 9 and later versions, an interface can have six different things:
- Constant variables
- Abstract methods
- Default methods
- Static methods
- Private methods
- Private Static methods
These private methods will improve code re-usability inside interfaces and will provide choice to expose only our intended methods implementations to users.These methods are only accessible within that interface only and cannot be accessed or inherited from an interface to another interface or class.
Java
// Java 9 program to illustrate
// private methods in interfaces
public interface TempI {
public abstract void mul(int a, int b);
public default void
add(int a, int b)
{
// private method inside default method
sub(a, b);
// static method inside other non-static method
div(a, b);
System.out.print("Answer by Default method = ");
System.out.println(a + b);
}
public static void mod(int a, int b)
{
div(a, b); // static method inside other static method
System.out.print("Answer by Static method = ");
System.out.println(a % b);
}
private void sub(int a, int b)
{
System.out.print("Answer by Private method = ");
System.out.println(a - b);
}
private static void div(int a, int b)
{
System.out.print("Answer by Private static method = ");
System.out.println(a / b);
}
}
class Temp implements TempI {
@Override
public void mul(int a, int b)
{
System.out.print("Answer by Abstract method = ");
System.out.println(a * b);
}
public static void main(String[] args)
{
TempI in = new Temp();
in.mul(2, 3);
in.add(6, 2);
TempI.mod(5, 3);
}
}
OUTPUT : Answer by Abstract method = 6 // mul(2, 3) = 2*3 = 6
Answer by Private method = 4 // sub(6, 2) = 6-2 = 4
Answer by Private static method = 3 // div(6, 2) = 6/2 = 3
Answer by Default method = 8 // add(6, 2) = 6+2 = 8
Answer by Private static method = 1 // div(5, 3) = 5/3 = 1
Answer by Static method = 2 // mod(5, 3) = 5%3 = 2
Rules For using Private Methods in Interfaces
- Private interface method cannot be abstract and no private and abstract modifiers together.
- Private method can be used only inside interface and other static and non-static interface methods.
- Private non-static methods cannot be used inside private static methods.
- We should use private modifier to define these methods and no lesser accessibility than private modifier.
So, from above it can be concluded that java 9 private interface methods can be static or instance. In both cases, the private method is not inherited by sub-interfaces or implementations. They are mainly there to improve code re-usability within interface only – thus improving encapsulation.
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While
7 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read