Error in Question enthuware.ocajp.i.v7.2.899 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

Error in Question enthuware.ocajp.i.v7.2.899 :

Post by ETS User »

Interface in question enthuware.ocajp.i.v7.2.899 must be defined like this:

interface I { String toString(); }


without this definition we have compile error.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by admin »

Are you sure?
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by admin »

Hint: Every object has a toString() method.
If you like our products and services, please help us by posting your review here.

adrian110288
Posts: 12
Joined: Thu Aug 01, 2013 3:44 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by adrian110288 »

OK I can understand that every interface gets all the methods from the Object class. I read that on StackOverflow:
When an interface has no direct SuperInterface, it will create abstract public method for all those public methods present in the Object class.
SO please someone explain me how are we not forced to implement those methods in the class when its clearly said that those methods from Object are public static ??!

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by admin »

I have no idea what stackoverflow says.
But since every object ultimately extends from Object, why would you need to implement any method from Object class in your class? Your class already has those methods because of inheritance.

I don't know what you mean by all methods from Object are public static. Where did you read that?

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

adrian110288
Posts: 12
Joined: Thu Aug 01, 2013 3:44 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by adrian110288 »

I am sorry, my mistake. I meant to say public abstract.
And also interfaces are not really Objects, are they? They dnt extend from Object class. I am sorry if i brought confusion, I just dnt see the reason in that interfaces get Object methods.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by admin »

adrian110288 wrote:I am sorry, my mistake. I meant to say public abstract.
And also interfaces are not really Objects, are they? They dnt extend from Object class.
No, they are not but every class does extend from Object. So it is immaterial what interface it implements. It will always have all the methods of Object class.
I just dnt see the reason in that interfaces get Object methods.
Again, I have no idea what the person at stackoverflow is saying. But an interface contains only the methods that are declared in that interface.
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by admin »

adrian110288 wrote:I am sorry, my mistake. I meant to say public abstract.
That is not correct either.
If you like our products and services, please help us by posting your review here.

adrian110288
Posts: 12
Joined: Thu Aug 01, 2013 3:44 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by adrian110288 »

OK thank you Paul. Not delving deeper into it , I will just stick to the fact that interfaces do have object methods. :)

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by admin »

adrian110288 wrote:OK thank you Paul. Not delving deeper into it , I will just stick to the fact that interfaces do have object methods. :)
There is no such fact. As I mentioned above, an interface has just the method declarations that are declared in that interface.
-Paul.
If you like our products and services, please help us by posting your review here.

mehdihamlili
Posts: 3
Joined: Fri Sep 23, 2016 9:45 am
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by mehdihamlili »

Hello,

I still don't understand , if interface doesn't contains the methods of Object , how did the reference 'i' knows that there's a method called toString() in the object.

Normaly we take the signature from the reference?

Thanks

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by admin »

Because every object in Java ultimately extends from Object class. Object class has toString method. That is why you can always call toString method on any reference.
If you like our products and services, please help us by posting your review here.

mehdihamlili
Posts: 3
Joined: Fri Sep 23, 2016 9:45 am
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by mehdihamlili »

Even if the reference doesn't contain the method?

For example :

interface I {
}

class C implements I{
public void test(){
}

public static void main(String[] args){
I i=new C();
i.test(); // this is valid even if i doesn't declare test method?
}
}

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by admin »

Not sure why do you expect i.test() to work. Do all objects in Java have test() method?

When you call a method on a reference, the compiler has to make sure that it is a valid method call for the type of the reference. Here, i.test() will not compile because the compile knows that the type of i is I and I doesn't have test() method.

I would suggest you to go through a good java book before attempting these mock questions.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

OlgaAG
Posts: 10
Joined: Thu Jan 03, 2019 3:08 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by OlgaAG »

What reason to cast System.out.println ((B) a); ? The heir is placed in the parent variable without cast.
A a = b; System.out.println (a); works fine with the same result

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by admin »

No reason other than testing your knowledge :D
If you like our products and services, please help us by posting your review here.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

Re: Error in Question enthuware.ocajp.i.v7.2.899 :

Post by enthunoob »

This question made me wonder the same as above, if interfaces implement Object methods (such as toString method) as they don't have Object as superclass. It thought this stackoverflow question explained it very well:

https://stackoverflow.com/questions/605 ... ss-in-java

Post Reply

Who is online

Users browsing this forum: No registered users and 126 guests