Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private+Final

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, same way.

First conside private modifier then consider about final.

private is not visible, than private+final is also not visible.
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variable member hiding:

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read Java Language Specification here:

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8.2
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic