• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private and protected

 
Shrinath M Aithal
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..
 
Henry Wong
author
Marshal
Pie
Posts: 21427
84
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
Marshal
Pie
Posts: 21427
84
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" ?
 
Chitta Ranjan Mahato
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..
 
jeetendra Choudhary
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..!!
 
Michael Angstadt
Ranch Hand
Posts: 277
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic