0

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??

Cœur
39k25 gold badges207 silver badges282 bronze badges
asked Jul 19, 2013 at 13:31
5
  • 2
    Elaborate. Give code example. Explain us what exactly you don't understand. If you get compiler or runtime errors, copy them entirely and paste them. As is, I can't figure what you're asking. Commented Jul 19, 2013 at 13:34
  • This question is extremely unclear, but if I understand correctly, you're looking for an explanation of object-oriented design. This is not the site for that. Commented Jul 19, 2013 at 13:37
  • @ JB Nizet - can u please explain now.Thanks Commented Jul 19, 2013 at 13:38
  • @ Jon Kiparsky - I m beginner in java so please explain it Commented Jul 19, 2013 at 13:39
  • docs.oracle.com/javase/tutorial/java/IandI/createinterface.html Commented Jul 19, 2013 at 13:49

6 Answers 6

4

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.

answered Jul 19, 2013 at 13:34
Sign up to request clarification or add additional context in comments.

1 Comment

Just to add: an abstract class can ignore the implementation of an interface method.
3

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.

answered Jul 19, 2013 at 13:39

1 Comment

@ silverback - you said correctly, but in this example program, do we need the interface?
3

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.

answered Jul 19, 2013 at 13:35

Comments

3

@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.

answered Jul 19, 2013 at 13:49

1 Comment

so i need to assume Animal is interface.
3

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.

answered Jul 19, 2013 at 13:52

Comments

2

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.

answered Jul 19, 2013 at 13:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.