• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Final Methods !!

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello
I worte a piece of code in which i tried to override a final method of the base class in the child class. Complier complains that i cant override a final method.
When i tried to overload the final method it seems perfectly okay...
How the final methods are treated in java. This question is asked from the memory management prespective. Will the memory reference created to the method be passed to the child class or will the child be having the copy of the method.
I am pretty much confused with this....:-(
Explanation needed !
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the definetion for the final keyword.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the rules for overriding and overloading are different
please clarify yourself on it
 
krish chandru
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sona,
Please tell me what will happen when u override a method in the child class...will it be having a copy of the method with different memory reference or is the reference of the method be passed to the child class ?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods are not kept with the instance of the object. The first time that a class is mentioned in code, the classloader looks for the class and loads in into the "Method Area" which is technically on the heap, but not usually garbage collected. So the methods are just loaded one time per JVM, not a copy per instance. Only variables, things that hold "state", are created per instance.
When it comes time to do late binding (that would be what happens when you override a method) the JVM looks for the method (with the signature that is used, if the method is overloaded) in the location of the sub class, and if it does not have that method then it looks for that method signature in the super class and uses that one instead.
From the JVM Specification


3.5.4 Method Area

It stores per-class structures such as the runtime constant pool, field and method data, and the code for methods and constructors, including the special methods (�3.9) used in class and instance initialization and interface type initialization.



If you use the final keyword for a method, then the JVM never looks for the method in the child class, it goes straight to the super class that has the final keyword. Of course the compiler prevents you from overriding a final method so that you don't get into trouble.
From the JLS Specification


8.4.3.3 final Methods
A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method.


Notice that the hiding refers to final static methods, which are resolved at compile time, and therefore do not do late binding. This means that they are never overriden but can be hidden instead.

 
krish chandru
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy,
This is of much help from u. i really feel comfortable now...
Could u tell me how Overloading is allowed for a final object as u have mentioned that the JVM will look into the baseclass directlty, how can it refer to the child class overloaded method ?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A final object? Do you mean a final method?
The JVM considers overloaded methods to be distict from each other. If a class with a "no-argument" final method is overloaded to have a one parameter method - that is a DIFFERENT method, with a different signature.
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic