aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private method" Watch "private method" New topic
Author

private method

Praveena khandavalli
Greenhorn

Joined: Jan 05, 2002
Posts: 21
can anyone explain why the following is true
can a private method of a superclass be declared within a subclass?
ans: true
thanks in advance
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
the method that you will be declaring in the subclass now will have nothing to do with the one in the superclass. Because the method in superclass is private. (the subclass has no means to access or find out about that method in superclass)
so basically you cannot say that the method (same as in super-class) that you defined in subclass is a overidding method. NO. it just a new method.
But if the same method in the superclass had been protected or public then you could say that the method is overidden.
so answer to your question is obvious now.
Originally posted by Praveena khandavalli:
can anyone explain why the following is true
can a private method of a superclass be declared within a subclass?
ans: true
thanks in advance
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

But if the same method in the superclass had been protected or public then you could say that the method is overidden

or package level access, incase the subclass belongs to the same package.
regds.
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Praveena khandavalli
Greenhorn

Joined: Jan 05, 2002
Posts: 21
hi,
thanx...so we can say that private methods cannot be overriden rt?
correct me if i am wrong
regards
Praveena
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
It's more correct to say that private methods are NEVER inherited by a subclass.
JLS 8.4.6 :
"A class inherits from its direct superclass and direct superinterfaces all the non-private methods of the superclass and superinterfaces..."
JSL 8.2 :
"Members of a class that are declared private are not inherited by subclasses of that class."
[ January 08, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Praveena khandavalli
Greenhorn

Joined: Jan 05, 2002
Posts: 21
then can we say private methods can be redeclared ??
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Since a private method is not inherited, if you create a method in your subclass with the exact same signature as a private method in the superclass, the new method is completely unrelated to the superclass method. You're not redeclaring it, although it might look that way in the code because it has the same name and signature. You're just creating a brand new method in the subclass.
Rob
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

then can we say private methods can be redeclared ??

As Rob said, its NOT re-declaring.
Its more like...YOU DONOT KNOW OF SUCH A METHOD
IN THE SUB-CLASS.
These are OO concepts you need to understand,
nothing related to Java.
Regds.
- satya
Praveena khandavalli
Greenhorn

Joined: Jan 05, 2002
Posts: 21
thanx rob and madhav.
its not at all related to the super class method...so we can declare a method with the same access modifier (in this case private) in the subclass.thanx a lot
Chris Graham
Greenhorn

Joined: Jan 08, 2002
Posts: 22
I'm pretty sure on this, but you guys keep me honest if you don't agree with it.
The only way Class B can access the private variables or methods in Class A, would be if Class B is an Inner Class of Class A.
Example:

Be merciful!
--Chris
[ January 09, 2002: Message edited by: Chris Graham ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
I would agree with your statement. However, in your example, you don't have to create a new A. You can just make calls like:
add(10);
subtract(7);
system.out.printline(count);
That's the whole point of having an inner class after all!
Rob
Chris Graham
Greenhorn

Joined: Jan 08, 2002
Posts: 22
Thanks Rob, I was kinda iffy on that part.
--Chris
[ January 09, 2002: Message edited by: Chris Graham ]
 
 
subject: private method