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

method overriding with different access specifier

 
akhil lalwani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If superclass and subclass have a method that has same signature, same return type, but only difference in their declaration is access specifier, is method-overriding possible?
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but there would be restrictions on the how the access modifier change.

Only more lenient modifier can be assigned.
 
akhil lalwani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the superclass method is declared private, then can overriding be done? I read that private members are synonymous with final, and cannot be overridden. Why is it so?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you can do that but the visibility of method or variable must not get reduce otherwise compiler give error
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akhil lalwani wrote:If the superclass method is declared private, then can overriding be done? I read that private members are synonymous with final, and cannot be overridden. Why is it so?


why donot you think what private means.
it means the method should not be access outside the class.so how can we override it.

and also static method are not overridden
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private,static and final method are called by their reference type only.
that means at compile it is decided which method seems to be called
 
Campbell Ritchie
Sheriff
Pie
Posts: 50240
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shanky sohar wrote:private,static and final method are called by their reference type only. . . .
What about an instance method which is overridden by a final method in a subclass?

No, private is not synonymous with final. Neither a final method nor a private method can be overridden, but the two words are not synonyms.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
shanky sohar wrote:private,static and final method are called by their reference type only. . . .
What about an instance method which is overridden by a final method in a subclass?

No, private is not synonymous with final. Neither a final method nor a private method can be overridden, but the two words are not synonyms.


that correct they are not synonymous.but its true that private methods are being called at compile time only.
IF not then could you please give me a example to prove this.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shanky sohar wrote:it its true that private methods are being called at compile time only.

What do you mean by that?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:
shanky sohar wrote:it its true that private methods are being called at compile time only.

What do you mean by that?


my above statement is little bit confusing so i am reiterating it

in case of overriding,as we all know that at compile time refernce type of the class point to a method to which it will called but actual method will be called at runtime..
but when we say private to any method then it is not available outside the class.
then how can we override it.

so in that case refernce type of the class will point to that private method and calls it at runtime
so no overiding is possible.
and if the method is not available in the class then it shows compile time error
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay; I figured that was what you meant, just wanted clarification for people reading the thread (because obviously the method isn't called at compile time :) Thanks for clearing it up.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, private is not synonymous with final. Neither a final method nor a private method can be overridden, but the two words are not synonyms.

Very true indeed.

There is a very clear distinction. A final method cannot be overridden, period. A private method cannot be overridden, but it can still be shadowed. A sub class can have a method with the same name and parameter types, and because it's not overriding the return type can even be completely different. With final this is not allowed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic