Recent articles:
Popular archives:
Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can,
or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing
heavily in Java's future as a platform for platforms
Also see:
Discuss: Tim Bray on 'What Sun Should Do'
In the Java 2 training class I'm taking, the "In Packages and Inheritance" module states:
When you create a subclass, it cannot inherit any of the superclass's methods and variables that are restricted by access modifiers.A s I understood it, the access modifier
private would not prevent inheritance but would prevent access (overloading/overriding) by a subclass. Also, a subclass inherits
all the methods and attributes of its superclass(es), and visibility modifiers affect this: private methods and attributes
cannot be accessed in a subclass. You'd have to use protected to allow subclass access. Can you clear up my confusion?
Trust yourself my friend!
You are absolutely correct. Access modifiers do not prevent inheritance. Instead, access modifiers affect what you can and cannot see in the subclass.
Those private members and attributes are still in your subclasses. Your subclasses just cannot access them directly.
Let's review the access modifiers:
I use the following rules of thumb to determine access levels:
final.
protected unless I know that a subclass absolutely needs it. In general, I do not declare methods and attributes as protected in the chance that a subclass may need it sometime in the future. If my design does not justify it explicitly, I declare
everything that is not in the public interface as private.
want to know more in java.By Anonymous on November 14, 2008, 8:29 amcan you help me
Reply | Read entire comment
View all comments