• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Inheritance

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please put your code between code tags so people feel like reading it ;-).
 
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 7402
1423
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 7402
1423
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better...

http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
 
Sudharsan Ashwin
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allright
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic