Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

general question

 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can a private method of superclass be declared within a subclass?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can create a method in a subclass with the same name as the private method in the superclass. This is NOT a case of overriding though, because private methods and variables are not inherited by subclasses. You will just have two methods with the same name. One will not override the other.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can override a private method from a superclass in the subclass. You cannot access the underlaying private method with a call to super it is still private there.
Overridding methods cannot have an access modifier that is more restrictive that the method in the parent so, you can have any access modifier on the method that you declare in the subclass.
Hope this Helps
 
NILESH PATEL
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am sorry but I agree with Carl's comment bu have some doubts about Bodie's comments. The point I would like to raise is just an expansion to Carls comments. Overriding methods, i.e. methods declared in subclasses must follow the table given below.
Superclass methods Subclass method
------------------- ----------------
public public
protected public or protected
default public or protected or default
private public or protected or default or private
The original question is answered by line four in the table. That is, if private method is declared in superclass, then subclass method can specify any acceess modifier,(private,public,protected or default).
I hope this makes it clear.
Lets work together to reach to the conclusion on this issue.
Thanks
Nilesh Patel
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou all for ur help.
I want to quote lines from RHE that states a difference between overloading and overriding.
the return type of an overloaded method may be chosen freely; the return type of an overriding method must be identical to that of the method it overrides .
that has confused me with the reply of nilesh
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carl, can you clear this up for me? If a superclass has a private method, my subclass cannot call that method because it will not inherit the method. If my subclass implements a method of the same name, it can have any access modifier that it wants. Is this because nothing is more restrictive than private? Or is this because the subclass doesn't know about the superclass's private methods?
 
geetha priyadharisini
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends!!
I'm a beginner i java.I want to know what is meant my platform independence.please help find out the answer
 
Brett Knapik
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this will be helpful to any one i am sure of it.
Access Control :
----------------
- Access modifiers are public, protected, private.
If nothing is provided,
default - 'friendly' - package level -
permissions are given for class/members.
public permissions are given for interface members
note : friendly is not a keyword

- Accessibitlity is different in each of the following
- within the class,
- another class in the same package,
- sub class in same package,
- another class in the another package,
- sub class in another package.
- Therefore, there are
- four types of access permissions and
- five places to test the accessibility.
- Totally 20 combinations.
- Accessibility with in the class :
- private - available
- friendly - available
- protected - available
- public - available
- Accessibility with in sub class in same package :
- private - not available
- friendly - available
- protected - available
- public - available
- Accessibility with in another class in same package :
- private - not available
- friendly - available
- protected - available
- public - available
- Accessibility with in sub class in another package :
- private - not available
- friendly - not available
- protected - available
- public - available
- Accessibility with in another class in another package :
- private - not available
- friendly - not available
- protected - not available
- public - available

- Important :
- public is more visible than protected,friendly,private
- proctected is more visible than friendly,private
- friendly is more visible than private
- private is no more visible than anything.
- while overriding,
- narrowing accessibility is not allowed.
- windening accessibility is legal.
- narrowing accessibility is
public -> protected -> friendly -> private
- widening accessibility is
private -> friendly -> protected -> public

- Access Modifier Applicability :
- Access modifiers are only applicable to
classes and class members and not method variables.
- classes :
- package level class should only be public or friendly.
- inner classes can be public, protected, friendly, and private.
- constructors :
public, protected, friendly, and private
- methods
public, protected, friendly, and private
- variables
public, protected, friendly, and private
- Class with only private constructor(s)
- this class can not be instantiated.
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work
 
NILESH PATEL
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry Preeti, but my message appears in straight line format, it actually appears without spaces I provided when writing this message. I actually wrote in a kind of table form. Anyway, I think we are talking about access modifiers and not about return types involved in overloading or overriding. It is a completely different issue. If you look into RHE book, Access modifiers are explained in chapter 3, I think, and OVERLOADING & OVERRIDING is explained in chapter 6.
I am sure brief glance throught hese topics will remove any confusion.
Thanks
Nilesh
[This message has been edited by NILESH PATEL (edited December 01, 2000).]
[This message has been edited by NILESH PATEL (edited December 01, 2000).]
 
NILESH PATEL
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bodie,
to clear your confusion,
Method call is different than method declaration. I think you are right in saying that, subclass method can not call private methods of a superclass. This is method call.
However,
You can still define/declare method with the same name,parameters and return type, i.e. overriding, as defined in superclass. This method may be declared private, public, proteceted or default, if and only if overriden method in superclass is declared private. So it is right to say that, nothing is more restrictive than private method.
Does it make sense? If not then I think RHE book, chapter 3 (figure 3.2 on page 83) will solve this probelm.
[This message has been edited by NILESH PATEL (edited December 01, 2000).]
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nilesh,i am sorry to interpret ur explaination in a wrong way n created a confusion.
thanks Brett for putting it in a very simple way and i have got the answer for my query.
thankyou all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic