Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface member declaration Issue

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Pals,

Consider the following code



On compilation the above code gives "Compilation error" that , we have not overridden the toString() method correctly as Object class has toString() method with return type String.
But on second glance i felt that interfaces do not extend Object class then why this error is thrown by a Compiler.

Then after some thoughts i came to a conclusion that even interfaces do not extends Object class, they are ultimately to be implemented by some concrete class and in order to preserve the contract of interfaces and overriding rules of a class Object (as each class inherits from Object class implicitly ), it is mandatory to follow the same overriding rules of method declaration defined in class Object.

If the above hypothesis is true ? Then why i am getting NO COMPILER ERROR in the following ........but i really think it should because ultimately it is going to break the code.




Thanks !!!
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at toString() method return type you have declared it as int
while it should be String.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking forward for Henry wong , ankit garg to answer !!!

@sumit kothalikar : please do have a second look again what i have written below....

Thanks !!!
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the access modifier of the method is protected
did you get the hint?
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad Kharkar wrote:the access modifier of the method is protected
did you get the hint?



You can always broaden the access modifier.....Moreover protected keyword is not allowed inside interface, so we are mandate to use that....

I think no one is reading my full query....I request to read it in full to get it what i am trying to say !!!


Thanks !!!
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces also extends Object class implicitly.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whatever I am writing below is just my assumption and simple logic I don't know the concrete answer
According to me interfaces do not extend Object class as I have not seen any interface that is written in java API
the finalize method in the interface that we are declaring is the method that is not known to the object class
that is.. there is not concept of overriding as Object class is not the parent of the interface that we are declaring (This is just my opinion)
so the compiler does not give any error while writing the method
i.e. the method can act as a separate method because the interface has not any super class
this is like two methods of the same name which are in different classes but they are private... the classes cannot see the private methods
and the declaration of the methods with the same name is allowed
I think similar case has happened here because the declared interface is not knowing the Object class and the finalize method in it
so it is free to declare a method with the same name
I hope I am correct
Happy learning Sahil.. you are going to rock in the exam by the way you are preparing
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the 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, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.

Since toString is public in Object, the interface will have its implicit declaration but since finalize is protected, there will be no implicit declaration of finalize method in an interface...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit, we were waiting for your reply for a long time.

[EDIT] replay - reply.
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:your replay

Actually I don't like to replay, I play a game only once
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
Abimaran Kugathasan wrote:your replay

Actually I don't like to replay, I play a game only once


mm, I've corrected.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic