Abhi vijay wrote:If I override a private method in a sub-class, then that method will be considered as an independent method.
And If I override a method, marked private+final, its the same as above right?
Abhi vijay wrote:
Yes, I know private methods are not inherited so they are not overridden.
Final methods if overridden give compilation error.
IF I replace line with private+final, then the result is the same that method() is treated as an independent method?
SCJP 6
Punit Singh wrote:Yes, same way.
First conside private modifier then consider about final.
private is not visible, than private+final is also not visible.
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Stephen Davies wrote:Dont forget if you apply static your are declaring a class attribute not an instance attribute.
Final + static = constant (good convention to provide refrence name in capital letters)
When you redeclare a private method in a subclass, I believe this is known as shadowing? I'm not sure, but this is not reccomended for readability?
SCJP 6
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Stephen wrote:
If static methods are class methods, and not instance methods, how may you redeclare them in a subclass. For Example, how could you redeclare methods in the Math class? (static class)?
SCJP 6
SCJP 6
Stephen wrote:
In my limited understanding, you may change a reference to an instance member but you may not change the type of that member, so how may you redeclare inherited instance member, again a code example would be greatfully appreciated
SCJP 6
When both a parent class and its subclass have a field with the same name, this technique is called variable shadowing
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
SCJP 6
Stephen Davies wrote:Thanks for that code, however I still cannot understand the shadowing concept, as creating a static method in a new class, is simply creating a new static method for that class, and not shadowing the super class. Even though in this case the subclass is extending the superclass, it is not inheriting the static method is it not, as a static method is tied to the class and not any instances of that class?
The subclass would still compile and run if you removed the extends declaration, as the increment method of the StaticMethodHiding class is a method of the class and not of any instance of either its own class nor any super class. As I understood to use the SuperStatic static increment method you would declare SuperStatic.increment() as you have done, so how can this truly be shadowing, if static methods, are not involved in subclassing(i.e no parent child relationship)
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
be a well encapsulated person, don't expose your privates, unless you public void getWife()!