This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

private and protected  RSS feed

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23590
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23590
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
Eclipse IDE Mac OS X Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S This Is Called Redefining Not Overriding. Redefining is allowed.

Thanks & Regards
Jeetendra..!!
 
Ranch Hand
Posts: 281
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • 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:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!