Error in Question enthuware.ocajp.i.v7.2.899 :
Moderator: admin
Error in Question enthuware.ocajp.i.v7.2.899 :
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.
interface I { String toString(); }
without this definition we have compile error.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
Are you sure?
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
Hint: Every object has a toString() method.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Thu Aug 01, 2013 3:44 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
OK I can understand that every interface gets all the methods from the Object class. I read that on StackOverflow:
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 ??!When an interface has no direct SuperInterface, it will create abstract public method for all those public methods present in the Object class.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
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.
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.
-
- Posts: 12
- Joined: Thu Aug 01, 2013 3:44 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
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.
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.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
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.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.
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.I just dnt see the reason in that interfaces get Object methods.
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
That is not correct either.adrian110288 wrote:I am sorry, my mistake. I meant to say public abstract.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Thu Aug 01, 2013 3:44 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
OK thank you Paul. Not delving deeper into it , I will just stick to the fact that interfaces do have object methods.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
There is no such fact. As I mentioned above, an interface has just the method declarations that are declared in that interface.adrian110288 wrote:OK thank you Paul. Not delving deeper into it , I will just stick to the fact that interfaces do have object methods.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Fri Sep 23, 2016 9:45 am
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
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
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
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
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.
-
- Posts: 3
- Joined: Fri Sep 23, 2016 9:45 am
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
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?
}
}
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?
}
}
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
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.
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.
-
- Posts: 10
- Joined: Thu Jan 03, 2019 3:08 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
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
A a = b; System.out.println (a); works fine with the same result
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
No reason other than testing your knowledge
If you like our products and services, please help us by posting your review here.
-
- Posts: 60
- Joined: Thu Apr 15, 2021 12:21 pm
- Contact:
Re: Error in Question enthuware.ocajp.i.v7.2.899 :
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
https://stackoverflow.com/questions/605 ... ss-in-java
Who is online
Users browsing this forum: No registered users and 126 guests