• 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

methond can be overridden not attributes? why( javaranch roundsup question)

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it prints "Outer" .. why it doesnt print "Sub"..

thanks
Aruna
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Four answers:

1. That's just the way it is.
2. Think of the syntax and semantic rules necessary so that a superclass method can always access a subclass field without errors. Think of the compiler code necessary to enforce those rules.
3. Field references are much more frequent than method calls. The overhead of dynamically binding field references at run time would be enormous.
4. You can achieve the same effect by using polymorphism with getter and setter methods instead of directly referencing the fields.
 
Aruna Agrawal
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
using getter and setter also doesnt work.

now if you copy and paste the getter and setter in Class B also it prints "Sub". Still awaiting a design related answer.. thanks mike for the efforts
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

now if you copy and paste the getter and setter in Class B also it prints "Sub".





This is how it is supposed to work. Methods are bound at run time. Fields are bound at compile time. Accessor methods give you run time binding of access to fields.

You seem to have trouble with the idea that language design decisions are made for practical reasons, but I took a course in Programming Languages from a real language designer and I can assure you that practical considerations are central to these choices.

Think through the implications of referencing a dispatch table every time every field is accessed. I realize that Java execution seems instantaneous when you are learning Java and doing exercises, but in a high volume application, efficiency equals money.
 
Aruna Agrawal
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mike..

i guess 1 liner is enought " Static methods are binded compile time and non-static methods at runtime"

thanks onces again
aruna
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic