How a class implements an interface in Java.

Explain with an example how a class implements an interface.

First ,the interface is to be defined. Defining an interface is placing the method signatures. Then these methods are to be overridden in the class that implements the interface.

Example
interface QuestionAnswer
{
   String answerForQuestion(String qtn);
}
class AnswerForQuestion implements QuestionAnswer
{
   String answerForQuestion(String qtn)
   {
       System.out.println(“The answer is “ + qtn);
   }
}
Create an object of the class AnswerForQuestion and invoke the answerForQuestion() method.
AnswerForQuestion afq = newAnswerForQuestion();
afq.answerForQuestion(“Java is a programming language”);

When a class implements an interface, it has to implement the methods defined inside that interface.

This is enforced at build time by the compiler.
interface interfaceA
{
   void methodA(int x);
}

class classA implements interfaceA
{
   // implementation for methodA and the rest of the class code if any.
}
Concrete class vs. Abstract class vs. Interface in Java
A concrete class has concrete methods, i.e., with code and other functionality...
How do I design a class so that it supports cloning in Java?
Deep cloning builds a new reference and a new object to refer to a new object...
Explain the complete syntax for using the Applet tag.
The Applet tag has the following attributes...
Post your comment