| Author |
Why Compiler eror here?
|
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Ranchers, Consider the code below, Why it gives complier error? Regards, Jothi Shankar Kumar. S ( tags added) [ October 17, 2006: Message edited by: Barry Gaunt ]
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
srini p.
Greenhorn
Joined: Oct 13, 2006
Posts: 3
|
|
hi, even though the object is of type "Derived" class, since you have stored it in "Base" class you can only call methods which are there in "Base" class. -srinivas.
|
 |
yogesh sood
Ranch Hand
Joined: Aug 31, 2000
Posts: 108
|
|
Hi Jothi, Base class reference variable caanot be aware about methods which are added in sub class. Here "added" means new methods defined in subclass which wasnt there in parent class. Compiler does not know what type of object a reference variable will be reffering to. So now if you consider yourself as compiler and do compilation of your program would you allow "b.getDerived();" where "b" is reference variable of Base class and in Base class there is no such method. Moreover what if at run time you assign object of Base class to reference variable "b" what method Java runtime invoke at that time !! [ October 17, 2006: Message edited by: yogesh sood ]
|
If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Above, So, at compile time it looks at the reference rather than at the object type? am I right? But I want to know when the actual memory for the object is created? At run time or compile time? Regards, Jothi Shankar Kumar. S
|
 |
yogesh sood
Ranch Hand
Joined: Aug 31, 2000
Posts: 108
|
|
Hi Jothi, Memory Allocation for Object is done at runtime.
|
 |
rajeswari kannan
author
Ranch Hand
Joined: Oct 01, 2006
Posts: 79
|
|
|
Because it is unable to find "getDerived()" method in Base class. Note that when we call a method by a super class reference, then the method should be present on the left side reference class (super class).
|
Rajeswari Kannan Java Certification | Online Exam Software | OCAJP | OCPJP | OCEJWSD | OCEEJBD
OCEJPAD | SCJD | SCWCD | SCBCD | SCDJWS | OCMJEA | OCEJWCD | SCMAD
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Thanks all for the reply. Regards, Jothi Shankar Kumar. S
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Here I have assigned null to b: The compiler still complains about not knowing about the method getDerived(). So object creation and memory allocation have nothing to do with the issue. Simply: the compiler says "What's a b? Oh, it's a Base. What? You want me to call a method getDerived()? Get lost, a Base has no such method!" [ October 17, 2006: Message edited by: Barry Gaunt ]
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Muhammad Saifuddin
Ranch Hand
Joined: Dec 06, 2005
Posts: 1318
|
|
I do some changes in this code.. Error : Cannot find symbol class Derived Can any one clear me on this.. why the compiler give me error ? Thanks
|
Saifuddin..
[Linkedin] How To Ask Questions On JavaRanch My OpenSource
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Barry, That was a good explanation. But how this differs from overriden methods? If I say Base b = new Derived() where the getDerived method is overriden, then at compile time, the compiler sees that ok there is a method in base class with the same signature but at run time the JVM takes advantage and does the late binding? Is this correct? Regards, Jothi Shankar Kumar. S
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Saif, The error in your code is in the line, Drived d = new Base(); d.getDerived(); You cannot make a sub class reference point to a superclass object. I know this for sure. Can anyone give reasons why? Thanks in advance. Regards, Jothi Shankar Kumar. S
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Originally posted by Saif Uddin: I do some changes in this code.. Error : Cannot find symbol class Derived Can any one clear me on this.. why the compiler give me error ? Thanks
Derived or Drived? Drived d = new Base();
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
But Barry, Is Derived d = new Base() correct way? Regards, Jothi Shankar Kumar. S
|
 |
rajeswari kannan
author
Ranch Hand
Joined: Oct 01, 2006
Posts: 79
|
|
Check the spelling (Drived instead of Derived) Drived d = new Base(); d.getDerived(); Also you cannot assign a super class object into sub class reference. So the code will not compile.
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Rajeshwari Kannan, You quote, Also you cannot assign a super class object into sub class reference. So the code will not compile. Can you please give me reasons with explanation? Regards, Jothi Shankar Kumar. S [ October 17, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
|
 |
Muhammad Saifuddin
Ranch Hand
Joined: Dec 06, 2005
Posts: 1318
|
|
Sorry its a spelling mistake. its Derived; after this changes the error is change with.. Error : Incompatible types
|
 |
Muhammad Saifuddin
Ranch Hand
Joined: Dec 06, 2005
Posts: 1318
|
|
Thanks for the reply Barry Gaunt, Jothi Shankar Kumar Sankararaj and rajeswari kannan its really cool to get the fastest result from you guys even i was late in reply. its just because of my Internet speed. Thanks again,
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Saif, Now you know that you can't assign a subclass reference pointint to a supeclass object but the most important is to know why? Can anyone please explain this why? Regards, Jothi Shankar Kumar. S
|
 |
Muhammad Saifuddin
Ranch Hand
Joined: Dec 06, 2005
Posts: 1318
|
|
Yeah but I prefered to read my self. I thought that google would be the first resource.
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi saif, I'll also try googling it and incase if you find any explanation, please dont forget to post it here. Regards, Jothi Shankar Kumar. S
|
 |
yogesh sood
Ranch Hand
Joined: Aug 31, 2000
Posts: 108
|
|
Hi Jothi, After my first reply to your query about memory allocation , i came across this brief information about memory allocation , i think you would like to have a look at it. http://java.sun.com/docs/white/langenv/Security.doc1.html
|
 |
Aniket Patil
Ranch Hand
Joined: May 02, 2006
Posts: 218
|
|
Assume a child class reference were allowed to refer to the parent object. Derived d = new Base(); Suppose i have the child reference, and since i see the child has a printHi(), i decide to call it through the child. However, the object held by the child reference ( a Base object) has no printHi() method in it. This would fail at run time. The bottom line is: A child can be guaranteed to do things that its parent does, but the reverse is not true. This is why a subclass reference cannot refer to a superclass object. [ October 17, 2006: Message edited by: Aniket Patil ]
|
SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Aniket, Your Quote, This would fail at run time. It would never compile when you assign subclass reference a superclaqss object. Regards, Jothi Shankar Kumar. S
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Originally posted by Jothi Shankar Kumar Sankararaj: Hi Aniket, Your Quote, This would fail at run time. It would never compile when you assign subclass reference a superclaqss object. Regards, Jothi Shankar Kumar. S
I guess Aniket forgot to mention that a cast to the base type would be necessary. Then it will fail at Runtime if it (the object assigned) is not really an object of type Derived. For example: [ October 17, 2006: Message edited by: Barry Gaunt ]
|
 |
Aniket Patil
Ranch Hand
Joined: May 02, 2006
Posts: 218
|
|
Jothi, Please read my post not once but twice before making a comment. I said "Assume a child class reference were allowed to refer to the parent object." Additionally, i have very conspicuously stated that a subclass reference CANNOT refer to a superclass object at the very end. And b.t.w the previous assumption was only because you wanted to know the reason why a subclass reference cannot refer to a superclass object, while the reverse is true. [ October 17, 2006: Message edited by: Aniket Patil ]
|
 |
 |
|
|
subject: Why Compiler eror here?
|
|
|