• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
Sudharsan Ashwin
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please put your code between code tags so people feel like reading it ;-).
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
ExamLab Creator
Marshal
Pie
Posts: 4600
306
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sudharsan Ashwin
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've learnt that memory will be allocated in heap.
Will this happen?
 
Sudharsan Ashwin
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
ExamLab Creator
Marshal
Pie
Posts: 4600
306
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudharsan Ashwin wrote:Sorry. Guys. ...... Please don't hate me

Oh, you broke another rule: NoNeedToSaySorry
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better...

http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
 
Sudharsan Ashwin
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allright
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic