aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about protected modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about protected modifier" Watch "about protected modifier" New topic
Author

about protected modifier

Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Before I get totally confused with the C++/Java similarities, I'd like to get this out of my head.
Does the 'protected' modifier change to 'private' in the subclass?

Paul
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Nope, not automatically -- if you don't define the variable yourself, it keeps the same accessibility. Let me try to explain without making this even *more* confusing:
This example is from the Programming Puzzlers session at JavaOne

However, if I removed the line marked as (1), things would compile fine. Because the variable would still have protected access. Because they are within the same package, the variable is accessible.
Realize the rules with methods are different, you're not allowed to override a method with more restrictive access.

Does that help? or was it more confusing?
Try plopping those code snippets in a file and compile them -- play around with it a bit, that might help.
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
However, if I removed the line marked as (1), things would compile fine. Because the variable would still have protected access.

Which variable gets protected access? The variable name is public in Base.
Anyway, I get the point. I'm just trying to get the C++ concepts that are not in Java out of my head. For example, the sizeof keyword in C++(which is not a keyword in Java) still gets me now and then.
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
Does that mean, I can't assingn the variable name(String variable) in Derived class at all? I tried with all modifiers including public? If, it is not possible, then it is a conclusion that methods defined in superclass can not be declared in subclass. But doesn't sound good to me..


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

Which variable gets protected access? The variable name is public in Base.

I think she meant


I'm just trying to get the C++ concepts that are not in Java out of my head. For example, the sizeof keyword in C++(which is not a keyword in Java) still gets me now and then.

The sizeof operator is not really that necessary in java because the sizes of the data types are platform-independent.

Does that mean, I can't assingn the variable name(String variable) in Derived class at all? I tried with all modifiers including public?

If I understood you correctly, you don't have to redeclare the name variable in the subclass to use it.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
This is one of those key differences between Java and C++. In C++, inheriting from a class that has a protected member makes that member private in the subclass. In Java, however, that member remains protected. Therefore, members can be inherited through multiple levels. Look at this code:

As you can see, the member i is accessible in class C. If i had become private within class B, it would not have been inherited by class C.
However, I think what Jess was saying was a little misleading. Any time you define a member in a subclass that has the same name as the inherited member of its superclass, we say that we are hiding the superclass' member. It doesn't matter what accessibility you put on the superclass' or subclass' member. If you have a member in the subclass with the same name as one in the superclass, the superclass' member is hidden.
Here's an example:

Notice that we output the value of i in B, not in A. You can get to the parent's member by using super, like this:

But you can't chain the keyword super (you can't do super.super.i), so you can only go up one level this way. In order to get the value of i from A in class C, class B would have to form some sort of a bridge.
Be sure to check out the JLS,
[/code]§8.3.3.2 Example: Hiding of Instance Variables for more details.
I hope that helps,
Corey


SCJP Tipline, etc.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

In C++, inheriting from a class that has a protected member makes that member private in the subclass.

However, I think what Corey was saying was a little misleading, as there are several kinds of inheritance in C++. It's only true if private inheritance is used.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Anthony Villanueva:

However, I think what Corey was saying was a little misleading, as there are several kinds of inheritance in C++. It's only true if private inheritance is used.

Bah! Of course there are. Obviously, my C++ has gotten a bit rusty over time. Sorry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about protected modifier