Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about protected modifier

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic