jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Questions 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 "Inheritance Questions" Watch "Inheritance Questions" New topic
Author

Inheritance Questions

Brian Grant
Greenhorn

Joined: Jul 20, 2004
Posts: 4
I am studying for the SCJP exam and have a couple things which have tripped me up so far on inheritance.

1. I understand that methods marked final are not inherited. But what about instance variables marked final? I'm thinking specifically about "constants" marked public static final. Are they inherited?

2. Are members marked private inherited? What I'm thinking of here is that in order to follow encapsulation best practices, I should mark my instance variables private and provide accessor methods. But if I do this, won't subclasses have to provide a whole new set of instance variables because private variables are not accessible to the subclass?

Thanks,
Brian
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
final methods and variables are inherited as long as they are marked public or protected. They just can't be overridden (or changed).

Nothing that is marked private is inherited, nor is visible beyond instances of the same class. If you want your subclasses to have direct access to member variables, they will need to be marked protected. Otherwise, you can always just use the public getter and setter methods, which is preferable anyway.
Brian Grant
Greenhorn

Joined: Jul 20, 2004
Posts: 4
Thanks Fletcher, that helps.
F. Marotta
Greenhorn

Joined: Jul 21, 2004
Posts: 2
Originally posted by Fletcher Estes:
final methods and variables are inherited as long as they are marked public or protected. They just can't be overridden (or changed).



IMHO:
this is correct for public, private or "default" access (included inner classes).
(obviously default access can allow inheritance only for derived classes in the same package)


am I right?


cheers
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by F. Marotta:

IMHO:
this is correct for public, private or "default" access (included inner classes).
(obviously default access can allow inheritance only for derived classes in the same package)


Actually, it is public, protected, and default members which are inherited (note that default members are only inherited by subclasses defined within the same package as the superclass). Private members are never inherited.


SCJP Tipline, etc.
F. Marotta
Greenhorn

Joined: Jul 21, 2004
Posts: 2
Originally posted by Corey McGlone:

Actually, it is public, protected, and default members which are inherited


OMG, I made a very bad typo.... yes, I meant protected when I wrote "private" (really.... )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance Questions
 
Similar Threads
static abstract final for contructor
Regarding objective 5.3-SCJP 5.0
accessibility and inheritance
significance of private with final modifier method
constructors