Win a copy of Hands On Software Engineering with Python this week in the Jython/Python 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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
Posts: 23811
140
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
Posts: 23811
140
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: 282
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:
 
Liar, liar, pants on fire! refreshing plug:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!