Thanks,
Sudhanshu(OCPJP,OCEJPAD)
Thanks,
Sudhanshu(OCPJP,OCEJPAD)
Sudhanshu Mishra wrote:
........ how come a method got invoked just based on reference, while the actual object is something else.
The method invoked was of superclass, while we have object of subclass only.
...... all the Java primitive types have well-defined standard sizes, so all primitive values can be stored in a fixed amount of memory (between one and eight bytes, depending on the type). But classes and array types are composite types; objects and arrays contain other values, so they do not have a standard size, and they often require quite a bit more memory than eight bytes. For this reason, Java does not manipulate objects and arrays directly. Instead, it manipulates references to objects and arrays. Because Java handles objects and arrays by reference, classes and array types are known as reference types. In contrast, Java handles values of the primitive types directly, or by value.
OCPJP 6 (91%)
Sudhanshu Mishra wrote:Hey Matthew,
Thanks for your reply,but I think you did not get clearly the question.Let me rephrase it.
First of all here is no inheritance because the method is private in superclass.
Thanks,
Sudhanshu(OCPJP,OCEJPAD)
Sudhanshu Mishra wrote:
... at compile time the refernce's type is checked but is it not true that at runtime ...
OCPJP 6 (91%)
Thanks,
Sudhanshu(OCPJP,OCEJPAD)
OCPJP 6 (91%)
Matthew Brown wrote:
Private methods are inherited. But they aren't visible in the subclass.
A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
It is not inherited by subclasses
Thanks,
Sudhanshu(OCPJP,OCEJPAD)
Sudhanshu Mishra wrote:Hey Ankit,
Thanks for reply
But have a look at code below
Now, as you can see that child does not have the method display(), but still the object created is of type child only, as is confirmed by the printed results.I know that at compile time the refernce's type is checked but is it not true that at runtime, the method of actual object is invoked, and if it is not there, a runtime exception must be thrown(remember the ClassCastException!)?
So in this case, no inheritance of method, and no object of parent, still method invoked.At complile time it was fine, as parent class has method display(). and compiler only checked the refrence's type,not the actual object, but waht about runtime?
Sudhanshu Mishra wrote:Hi Seetharaman,
I would also request you to correct my understanding.I have always read that we always require an instance to invoke an instance method.In the code I posted above, I dont have an instance at all ,adn the type of reference just describes that what method is invoked,but the actual method invocation is on the real object at runtime.
Then why did the code I posted earlier worked?
Thanks,
Sudhanshu(OCPJP,OCEJPAD)
Sudhanshu Mishra wrote:
But , let me ask it this way.In the snippet above, we have object of child class, not parent class.We just have reference of parent type, but the actual object is of child type.So at runtime, will the JVM not serach for the real object to which the reference is pointing?And if the JVM does invokes the method on the real object,why did it invoke the method in this case.We do not have a parent object, we have object of child.
Thanks,
Sudhanshu(OCPJP,OCEJPAD)
Sudhanshu Mishra wrote:
I guess you are missing that the child class is not having the method display() at all.It is private in parent class.Does this bring any change in your answer?I am repeatedly trying to say that child instance is not having method display(), yet the method is invoked on child instance at run time.
Please get me corrected if wrong.
Seetharaman Venkatasamy wrote:Private members are not inherited . from JLS:
A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
It is not inherited by subclasses
Sudhanshu Mishra wrote:
I guess you are missing that the child class is not having the method display() at all.It is private in parent class.
Henry Wong wrote:Another point (and probably the main reason for your mistake) ....
Sudhanshu Mishra wrote:
I guess you are missing that the child class is not having the method display() at all.It is private in parent class.
The "private" (in private method) is referring to the accessibility by the caller -- meaning can the method be called by the calling code? In this case, it is being called by the main() method of the parent class. And since that code is part of the class that it is private to, it can be called.
It seems that you have your own definition of what is a private method -- with the meaning that private methods can't be called (using child instances) at all.
Anyway, as an alternative, if the main() method was part of the child class, then the compiler would have complained. Try it. Move the main method to the child class, and you will see that it doesn't work.
Henry
sharma ishu wrote:@Henry : At runtime , there is a child object. Child class must have have the function display() so that it can be called at runtime.
sharma ishu wrote:But Child class does not inherit neither define the display() function.
sharma ishu wrote:So shouldn't the JVM through a RUNTIME Exception inspite of calling Parent class display() function?
sharma ishu wrote:So shouldn't the JVM through a RUNTIME Exception inspite of calling Parent class display() function?
Remember you have not inherited earth from your ancestor,you only borrowed it from your descendants.
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|