Click here to Skip to main content
14,970,418 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I know this is a very basic question but what is the point of methods declaration inside an interface when they are not defined. I understand a child class needs to define the method(or override it) but where exactly is the definition of the method stored. The below example might elaborate the question:

Here a compiler does not understand what animal sound is i have implemented it in the child class then what is the point of having this method in interface because without definition it is just a word which compiler does not know. Where is the basic definition of method(in this case animal sound) defined or is it defined already in some class and interface just uses it? Please someone clarify.

What I have tried:

public interface Animal {
public void animalSound(); // interface method (does not have a body)
public void run(); // interface method (does not have a body)
}


public class Pig implements Animal {

public void sleep() {
System.out.println("Zzz");
}

public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
Posted
Updated 31-May-21 1:53am

The key is the word implements in your class. You are telling the compiler that you will provide implementations for all the methods in the interface. Think of it as a contract. Whatever methods are declared in the interface, the implementing class must provide the definitions (i.e full code). And the actual code will be in the classes that do the implementation, the interface contains only the declarations.

See Lesson: Interfaces and Inheritance (The Java™ Tutorials > Learning the Java Language)[^] for further details and sample code.
   
This is done so that, given an Animal collection (containing different subclasses of Animal), the animalSound method can be invoked through the Animal class. It ends up invoking the actual implementation for whatever subclass of Animal is currently being used out of the collection. It also says that, to be an Animal, you have to implement the animalSound method.

EDIT: I see you were also asking how this is implemented. It's done with a table of virtual methods[^] that is associated with each class (not each object, just each class). Each object has a reference to the virtual method table associated with its class, and that's how the correct method for the object is invoked.
   
v4
Comments
Ritika Dhar 31-May-21 8:01am
   
I understand that in this example the animalSound method can be invoked through the Animal class but my question is where is animalSound method defined.In human terms we understand what animalSound is but how would a complier know coz in the interface that method body is empty and when i am implementing it in my child class i would probably define it based on the animal class(i.e. for cat make it meow and so on). Let's take another example and implements interface Car. I have a child class BMW which will call the method start() from car interface but since in the interface the method body was empty how would the complier knows what start() basically does, for bmw i can make it automatic but what exactly is start() needs to be defined. Is it defined separately as a method in java somewhere and interface itself just calls it? is what i am asking here.
Greg Utas 31-May-21 8:07am
   
Please read the article I linked to. When the compiler sees the animalSound method, it creates an entry for that method in the table. The entry references the implementation of that method for whatever subclass of Animal is involved. For the Animal class, the entry is a method that throws an exception, because it is unimplemented. For all other subclasses of Animal, the entry references that subclass' implementation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900