• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

method overriding with different access specifier

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 74384
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22510
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic