| Author |
Inheritance
|
Sudharsan Ashwin
Greenhorn
Joined: Mar 09, 2010
Posts: 18
|
|
In this line B b = new B(); ,
Memory will be allocated in heap for print() method and that will be referenced by " b" .
What about the next line?
A a = b;
Memory is not allocated for method in class A i.e, display(), only 'a' reference is pointing to the heap.
Now, can we access the display() method using a.display(); ???
If so, can anyone please explain the logic behind this?
[devaka: added code tags]
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
|
Please put your code between code tags so people feel like reading it ;-).
|
JDBCSupport - An easy to use, light-weight JDBC framework -
|
 |
Chinna Eranna
Ranch Hand
Joined: Dec 08, 2009
Posts: 174
|
|
Sudharsan Ashwin wrote:
In this line B b = new B(); ,
Memory will be allocated in heap for print() method and that will be referenced by " b" .
There is no memory allocated for methods during object creation. Classes and instructions for their methods are loaded whenever the class is referenced for the first time.
When you say "B b = new B()", if this is the first time B is referenced, then Classloader loads B. Also, it loads the classes referenced by B.
i.e., As Class B extends A, and it references Class A, class A is loaded at the same time B is loaded.
when you do "A a = b", you can access all methods declared in Class A.
If you have overridden any of class A method, in class B, then it will invoke overridden method in class B.
|
- Chinna
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
Note that it's better to really write out code, e.g. System.out.println instead of s.o.p. or public static void main instead of psvm ;-)
It's easier for some other folks reading those posts.
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
Sebastian Janisch wrote:Note that it's better to really write out code, e.g. System.out.println instead of s.o.p. or public static void main instead of psvm ;-)
It's easier for some other folks reading those posts.
Yes, please PostRealCode
|
Author of ExamLab (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
|
 |
Sudharsan Ashwin
Greenhorn
Joined: Mar 09, 2010
Posts: 18
|
|
I've learnt that memory will be allocated in heap.
Will this happen?
|
 |
Sudharsan Ashwin
Greenhorn
Joined: Mar 09, 2010
Posts: 18
|
|
Sorry. Guys. I am totally new to this.
Will learn as I participate in discussions :-).
Kindly bare with me.
Please don't hate me
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
Sudharsan Ashwin wrote:Sorry. Guys. ...... Please don't hate me 
Oh, you broke another rule: NoNeedToSaySorry
|
 |
Chinna Eranna
Ranch Hand
Joined: Dec 08, 2009
Posts: 174
|
|
Better...
http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
|
 |
Sudharsan Ashwin
Greenhorn
Joined: Mar 09, 2010
Posts: 18
|
|
Allright
|
 |
Larry Chung
Ranch Hand
Joined: Feb 02, 2010
Posts: 245
|
|
Sudharsan Ashwin wrote:
Kindly bare with me.
Please don't hate me 
One more tiny thought. The correct expression is "Kindly bear with me" because "Kindly bare with me" suggests taking off clothing, which probably breaks another JavaRanch rule.
|
SCJP 6
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
Use full code rather then using short-cuts.
In the first place i was stuck what does p s v m means?
Use proper conventions.
Thanks,
|
Prithvi/Beenish,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Inheritance
|
|
|