I am Utpal Vishwas from Uttar Pradesh. Have completed my B. Tech. course from MNNIT campus Prayagraj in 2022. I have good knowledge of computer networking.
A java interface is a blueprint for a class. Defines the methods that the class must implement. Interfaces cannot be instantiated, but can be implemented by classes.
Implementing an interface in Java, requires using the implements keyword. For example, the following code shows how the Runnable interface implemented:
Java
public class MyThread implements Runnable
{
public void run()
{
// Do something.
}
}
The Runnable interface defines a single method called run(). The
MyThread class implements the run() method by providing its own implementation.
Here's another example of how to implementing an interface in Java:
Java
public interface Shape
{
void draw();
}
public class Circle implements Shape
{
public void draw()
{
// Draw a circle.
}
}
The Shape interface defines a single method called draw(). The
Circle class implements the draw() method by providing its own implementation.
If a class implements an interface, it must provide implementations for all the methods defined in that interface. A class is said to be an abstract class if it foes not provide implementations for all the methods defined in the interface, then the class is said to be abstract.
Liked By
Write Answer
How to implement interface in java? Please provide a sample.
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
06-Sep-2023A java interface is a blueprint for a class. Defines the methods that the class must implement. Interfaces cannot be instantiated, but can be implemented by classes.
Implementing an interface in Java, requires using the
implements
keyword. For example, the following code shows how the Runnable interface implemented:Java
The
Runnable
interface defines a single method calledrun()
. TheMyThread
class implements therun()
method by providing its own implementation.Here's another example of how to implementing an interface in Java:
Java
The
Shape
interface defines a single method calleddraw()
. TheCircle
class implements thedraw()
method by providing its own implementation.If a class implements an interface, it must provide implementations for all the methods defined in that interface. A class is said to be an abstract class if it foes not provide implementations for all the methods defined in the interface, then the class is said to be
abstract
.