aspose file tools*
The moose likes Beginning Java and the fly likes method overriding with different access specifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method overriding with different access specifier" Watch "method overriding with different access specifier" New topic
Author

method overriding with different access specifier

akhil lalwani
Greenhorn

Joined: Aug 21, 2010
Posts: 5
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

Joined: Aug 06, 2010
Posts: 173
Yes, but there would be restrictions on the how the access modifier change.

Only more lenient modifier can be assigned.


Regards,
Avishkar Nikale
akhil lalwani
Greenhorn

Joined: Aug 21, 2010
Posts: 5
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

Joined: Mar 17, 2010
Posts: 1051

yes you can do that but the visibility of method or variable must not get reduce otherwise compiler give error

SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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

Joined: Mar 17, 2010
Posts: 1051

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

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: Mar 17, 2010
Posts: 1051

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Mar 17, 2010
Posts: 1051

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: method overriding with different access specifier