• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private method

 
Praveena khandavalli
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Praveena khandavalli
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
thanx...so we can say that private methods cannot be overriden rt?
correct me if i am wrong
regards
Praveena
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Praveena khandavalli
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then can we say private methods can be redeclared ??
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob, I was kinda iffy on that part.
--Chris
[ January 09, 2002: Message edited by: Chris Graham ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic