Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why interface methods have no body

To achieve multiple inheritance, we must use interfaces, but why don't interface methods have bodies and why do they have to be overridden in the derived class?

I really want a lucid answer , not involving too much computer jargon , i cant seem to understand this , i have referred various references

like image 630
Nishant Jani Avatar asked Aug 31 '12 05:08

Nishant Jani


People also ask

Why interface has no method body?

Methods do certain things and in a certain way. Interfaces in contrast only state the name and arguments of methods in sort of a contract. How an implementing class implements those methods is up to the class, thus the interface cannot provide behaviour since it doesn't have an implementation of the method.

Can an interface method have a body?

Interface methods do not have a body - the body is provided by the "implement" class. On implementation of an interface, you must override all of its methods. Interface methods are by default abstract and public.

CAN interface have static methods without body?

In short, no.

Which method Cannot have a body in Java?

Abstract methods do not have the body they only have declaration but no definition. The definition is defined by implementing classes.


4 Answers

Because Java, in contrast to languages like C++ or Eiffel, only has multiple inheritance of types (i.e. interfaces as well as one class), not multiple inheritance of state and behaviour. The latter of which add enormous complexity (especially state).

The Java designers (and C#, for that matter) opted to not include it as it presented C++ programmers often with very hard to debug issues. You can solve pretty much most problems that require true multiple inheritance with implementing multiple interfaces, so the tradeoff was deemed worth it.

Note that multiple inheritance of behaviour (not state) might come to Java 8 (unless they postpone it again like one of the many other things) in form of virtual extension methods where an interface can declare a method that delegates to one in another class, which then exists on all types that implement that interface.

like image 184
Joey Avatar answered Oct 28 '22 20:10

Joey


Interfaces declare WHAT services the implementing class provides, not HOW (that's the job of the implementing class). Multiple inheritance is regarded bad, as it leads to complicated code and class hierarchies.

like image 2
esaj Avatar answered Oct 28 '22 19:10

esaj


Interfaces only have constant variables(public + static + final) and abstract methods(public & abstract). These are meant to be used by the classes which implement the interfaces.

Interfaces simply say 'Am a contract', which if you wish to use, should stick to some rules(give implementation to all abstract methods).

Multiple inheritance is omitted in Java by making sure that a class can extend only 1 class, in order to avoid the diamond problem. You can anyways have multiple inheritance of types in Java by using interfaces.

like image 2
Anuj Balan Avatar answered Oct 28 '22 19:10

Anuj Balan


A Java interface contains a list of methods that must be implemented by the class that implements the interface. Thus, the methods have no body: the body of each method is in the implementing class(es).

like image 1
Anderson Green Avatar answered Oct 28 '22 21:10

Anderson Green