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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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
implementskeyword. For example, the following code shows how the Runnable interface implemented:Java
The
Runnableinterface defines a single method calledrun(). TheMyThreadclass implements therun()method by providing its own implementation.Here's another example of how to implementing an interface in Java:
Java
The
Shapeinterface defines a single method calleddraw(). TheCircleclass 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.