• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

private and protected

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can a private method be overriden by a protected method?

I thought no.. because private method will not be visible to any subclasses..
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, what happened when you tried it?

Henry
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:BTW, what happened when you tried it?

Henry



i tried it like this :



the question was from free mock test

the question asked if a private method could be overriden by a private method.. i said no.. the software said yes you can and there was no explanation why..

but according what I know, if a method is declared private, then the subclass wont know if it even exists.. tell me the software is wrong here or how am i not understanding the question..
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the question asked if a private method could be overriden by a private method.. i said no.. the software said yes you can and there was no explanation why..



It is valid to have a method (private, protected, or public), in a subclass, with the same signature of a private method of the superclass. However, it is technically not overriding, as any method calls from the superclass to the method will be calling the private method of the super class.

Henry
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:However, it is technically not overriding, as any method calls from the superclass to the method will be calling the private method of the super class.



then i answered as no.. was i right or was i wrong ?

I thought I was right because it is not "overriding" the private method.. no one can override the private method then.. According the exact text in the question, the answer should have been "no".. right?

by the way, will there be such ambiguities in the "real exam" ?
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friend
As far i understood your question i trying to reply you.

When you declare a method private in the base or parent class it is not visible in the derived or child class actually here you defining a totaly new method.There is no concept of overriding here.

i am explaining through a example
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chitta Ranjan Mahato wrote:
When you declare a method private in the base or parent class it is not visible in the derived or child class actually here you defining a totaly new method.There is no concept of overriding here.



thanks for helping we came to that conclusion already in earlier part of this thread..
 
Ranch Hand
Posts: 41
Mac OS X Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S This Is Called Redefining Not Overriding. Redefining is allowed.

Thanks & Regards
Jeetendra..!!
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chitta Ranjan Mahato wrote:When you declare a method private in the base or parent class it is not visible in the derived or child class actually here you defining a totaly new method.There is no concept of overriding here.



The @Override annotation comes in handy here. This code gives a compile error:
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic