Question:
May I know if Class B inherits from Class A, will the private methods in Class A be extended to Class B too?
Answer by Priety Sharma:
I think by methods in Class A be extended to Class B You intend to say that are they accessible from Class B.
No, private methods are accessible only from within that class.
If you want these methods of Class A to be accessible from its subclass i.e. Class B in this case then you could make them protected. This will allow a subclass residing outside the package of its superclass to access these methods.
If the subclass is in the same package as the super class then default access for the method will do. That is specifing no access modifier before the method signature.
If you want more info then search for java access modifiers for methods on the net:
It will give you details about the modifiers: public , protected , default and private.
This should clarify all your doubts.
Answer by Campbell Ritchie:
If these methods were private, it was in order that they be not accessible. There was surely a good reason for making the private; don't change the access modifier.
Q&A: Inherited Method
Posted by
Jessica
on Monday, September 28, 2009
Labels:
Inheritance,
Question and Answer
0 comments:
Post a Comment