wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface member declaration Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface member declaration Issue" Watch "Interface member declaration Issue" New topic
Author

Interface member declaration Issue

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
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 !!!


SCJP 6.0 96%

(Connecting the Dots ....)
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

Take a look at toString() method return type you have declared it as int
while it should be String.

Thanks & Regards
Sumit Kothalikar
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
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

Joined: Mar 07, 2010
Posts: 438

the access modifier of the method is protected
did you get the hint?

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
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

Joined: Jan 14, 2010
Posts: 317

Interfaces also extends Object class implicitly.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Ankit, we were waiting for your reply for a long time.

[EDIT] replay - reply.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Abimaran Kugathasan wrote:your replay

Actually I don't like to replay, I play a game only once
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.
 
jQuery in Action, 2nd edition
 
subject: Interface member declaration Issue
 
Similar Threads
Reg. abstract class and method
Inner class
Use of pubilc methods in package class
error question: toString() in java.lang.Object is not defined in a public class
my notes on JLS for any1 who needs them !!