Question

Can Someone Explain how the methods of interface used in classes?

Note: My Doubt is "Methods are already defined in Class then why we should implement it ? "

For Example :

interface printable{
void print();
}

class A implements printable{
public void print(){System.out.println("Hello");}

public static void main(String args[]){
A obj = new A();
obj.print();
 }
}

why print() is declared in interface??

Was it helpful?

Solution

You define a method by giving its implementation. They are the same thing, so you are right that once you define a method, you don't also need to implement it.

An interface declares that anything implementing this interface will defined those methods. This is part of the contract for interfaces. This allows you to call any method of an interface knowing than any concrete implementation will have such a method.

BTW In Java 8, it will support virtual extensions which means an interface can give a default implementation. This has to be defined in terms of other methods provided by the interface.

OTHER TIPS

An Interface is a contract that all classes that implement it, should have a definition for the methods specified in the interface. An interface does not define the method body as such.

An interface defines a set of method which must be implemented. It says nothing on how they are implemented. This is where the class definition comes in, since it defines how these methods are implemented.

Thus, when you call a class which implements a particular interface, then you know, for sure, that you will find whatever set of methods the interface defines.

Interfaces are usually handy when you need to expose some endpoints to your application, without the need to expose the logic.

EDIT: As per your example, the printable interface defines what behaviour should a class which implements it expose, in this case print.

This will allow you to do something along the lines of printable p = new A(); p.print();.

Assuming you have something which yields an object which implements the printable interface, then, whoever is calling that method will not need to bother what is the actual implementation of the print method. The interface makes sure that whatever you are returning, will contain an implementation of that method.

@NarutoUzumaki Welcome to Stack overflow!

I agree with Chris. You can replace the doSomething method with eat() method to get a better understanding. A dog may eat something different than a cat and to a giraffe.

Its up to you how you implement the eat method, and when using it create a reference of the interface Animal and point it to the instance of Dog, Cat or Giraffe which ever eat method you want to use. This makes your class design very extensible.

Hope you get a clear idea now.

Generally Interface based Programming is recommended, Because of the following reasons

1)Interface means rule , you should follow those rules while implementing those methods in Implemented class.

2) Dependency is less between classes while instancing your implemented class then call your methods from another class or some where.

3) You can publish your interface details only no need to disclose the implemented details of your methods to out side the world.

Defining an interface is the difference between:

public void doSomething(Dog d)
{
   d.doSomething();
}

public void doSomething(Cat c)
{
   c.doSomething();
}

public void doSomething(Giraffe g)
{
   g.doSomething();
}

and

public void doSomething(Animal a)
{
   a.doSomething();
}

Why?

Well, if all the classes just implement their own methods, there's no common reference between them. However, if they all implement the method from a common interface, they can be referred to by the same reference type; in this case Animal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top