2

I have an interface like below.

I got this default code in Eclipse. Confused, Why the "@Override" attribute is coming?

Is there any other TOP level default interface is available where we have declared all these methods and later implemented in Object class?

  public interface IRecord {
        @Override
        public String toString();

        public void showName(String name);
    }

And its one implementation like below

public class Record implements IRecord{

    @Override
    public void showName(String name) {
          //Doing something
    }
}

It's getting complied very well as expected but I am little surprised why its not asking me to implement toString() method's implementation?

It's there in Object class but Object class is not implementing my interface.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
Jaikrat
  • 1,124
  • 3
  • 24
  • 45
  • `I got this default code in Eclipse` - how did you create this code? Which version of Eclipse are you using? – Andreas Fester May 24 '13 at 12:05
  • Eclipse SDK HELIOS Version: 3.6.1 Build id: M20100909-0800 – Jaikrat May 24 '13 at 12:06
  • And how did you create the interface? With Eclipse Juno, when selecting "New => Interface" from the project browser, enter the interface name and press "Finish", all I get is an empty interface declaration – Andreas Fester May 24 '13 at 12:10
  • 1
    Yes but if you press Ctrl+Space it suggest you some methods. You can select anyone afterwards. – Jaikrat May 24 '13 at 12:13

6 Answers6

6

All classes implicitly extends Object, which already has implemented toString(). And since your Record class implements your interface, and inherits the toString() method from Object, the contract is fulfilled.

NilsH
  • 13,705
  • 4
  • 41
  • 59
2

Is there any other TOP level default interface is available where we have declared all these methods and later implemented in Object class?

From the Java Language Specification:

If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.

In other words, even an empty interface implicitly inherits all the public methods from Object. This is the reason why Eclipse (correctly) adds the @Override annotation. It will most likely never fail due to any of the methods being removed from Object, but it can be helpful to avoid typos:

interface IRecord {
   @Override
   public String toSTring();
}

Error: "The method toSTring() of type IRecord must override or implement a supertype method"

See the other answers for the second part of the question.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • Sorry, Could not reply...had to go for one call. Yes, exactly. Nice explanation with Ex. Today I learnt one thing that annotations are to avoid typos. Earlier, I used to delete those extra words (i.e annotations) :) – Jaikrat May 24 '13 at 13:30
1

Interfaces do not inherit from Object, but any class that would be implementing your interface will, so it is implemented by default.

You may be wondering why Eclipse provides you with default overridden toString in your interface template: I think of it as a reminder to implement toString to something meaningful later on.

moonwave99
  • 21,957
  • 3
  • 43
  • 64
  • `Interfaces do not inherit from Object` - not in the usual sense, but a top level interface implicitly declares all public methods from `Object` – Andreas Fester May 24 '13 at 12:40
1

Each and every class extends java.lang.Object in java, if you are not extending one then implicitly Object is considered.

Look at your code only:

public interface IRecord {
    @Override
    public String toString();

    public void showName(String name);
}

How come you have an @Override annotation in toString() method, you must be inherting it from somewhere,

As metioned in JLS: If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object.

That's why you could see an override annotation above toString() method.

Now when you implement IRecord class, your implementation class also extends the Object class too, which has the toString() method. Hence, its not required to implement it here.

Himanshu Bhardwaj
  • 4,038
  • 3
  • 17
  • 36
  • 1
    Yeah...I think, Interfaces do not inherit methods from Object – Jaikrat May 24 '13 at 12:07
  • @Jai I was thinking the same before (hence the +1 ;-) ), but actually interfaces **do** implicitly inherit all public methods from `Object`. See my answer. – Andreas Fester May 24 '13 at 12:33
  • Thanks I too got confused, and cleared now by reading again, as an addon, http://stackoverflow.com/questions/6056124/do-interfaces-inherit-from-object-class-in-java – Himanshu Bhardwaj May 24 '13 at 15:33
0

The @Override attribute is for compiler safety. It is just to make sure that you really did mean to override a method and that you haven't done it by mistake

uriDium
  • 13,110
  • 20
  • 78
  • 138
0

Because Object is base class of all other classes and toString method defined in Object class, it is already implemented.

mmdemirbas
  • 9,060
  • 5
  • 45
  • 53