aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private+Final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private+Final" Watch "Private+Final" New topic
Author

Private+Final

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
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?
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
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?


Private methods are not inherited. So no overriding concept on these
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


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?
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Yes.

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?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Yes, same way.

First conside private modifier then consider about final.

private is not visible, than private+final is also not visible.


SCJP 6
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102

Yes buddy.

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.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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?


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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?


if you redeclare visible static methods or visible instance members in subclass, that is called shadowing.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
if you redeclare visible static methods or visible instance members in subclass, that is called shadowing.

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)? It is indeed an interesting concept and as I am only SCJA, could you provide a code example, it would be of great help. 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.

Many Thanks
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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)?


Static method hiding example:
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Instance variable member hiding:

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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


I did not get this, could you explore some more, if you write some code then it will be even better.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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)

When both a parent class and its subclass have a field with the same name, this technique is called variable shadowing


http://articles.techrepublic.com.com/5100-10878_11-5031837.html
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Read Java Language Specification here:

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8.2
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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)


I didn't clearly understand what you are saying, but I think this will clear your doubt



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Well, in a way I can see what you are getting, at, but I think my confusion lies in Punit's explanation of shadowing static methods, I think shadowing is not restricted to static members alone. However, I have checked it is certainly not a reccomended practice at any means. As for my earlier point on variables and references, ignore that I was confusd with another issue.

Thanks for you clarification.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private+Final