aspose file tools*
The moose likes Associate Certification (OCAJP 7) and the fly likes protected member in inherited class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7)
Bookmark "protected member in inherited class" Watch "protected member in inherited class" New topic
Author

protected member in inherited class

Mustakimur Rahman
Greenhorn

Joined: Oct 12, 2013
Posts: 13


Does not Line 1 show some compilation error, as the instance variable of class A is protected so far as inheritance it will only available in its immediate subclass as its private data.
So, it should not be possible to modify the value of variable a using a reference of class B as that is actually private there.

If I am wrong please correct me with more details if you can.

Thanks in advance.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

Try putting class A and B in package x and class C in package y. See what happens now.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Mustakimur Rahman
Greenhorn

Joined: Oct 12, 2013
Posts: 13

Roel De Nijs wrote:Try putting class A and B in package x and class C in package y. See what happens now.

I already try this, and found my theory works in this case. But, why not it behave same which I am posted here.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

Time for a pop quiz!

If you remove the protected modifier from a in your example (so you'll end up with a data member with default access modifier), what will happen with line 1? Compiler error? Compiles just fine? Runtime exception? ...
Mustakimur Rahman
Greenhorn

Joined: Oct 12, 2013
Posts: 13

As it is in same package scope,
the default modifier will be available everywhere in that package.
So, its works fine.
There is nothing like protected member when it is inherited will be private. This part confused me, is that will same when its happened in same package?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

The difference between default and protected access modifier is one of the hardest things to master. I even see experienced developers struggle with the distinction.

Default and protected access modifiers are almost identical. A default member may be accessed only if the class accessing the member belongs to the same package (using inheritance and/or reference). So if A and B are in the same package this code will compile and run:


A protected member can be accessed as a default member + can be accessed (but only through inheritance) by a subclass even if the subclass is in a different package. So as an easy reminder: default = package-private and protected = package-private + kids. So the above code will still compile when member a is marked protected.

Because in your example all classes are in the same package the package-private rule applies. The kids-rule is an addition to the package-private one, it's not a replacement. So protected is exactly the same as default, but there is a little bonus. With default access all classes and sub classes can access the member if they are in the same package. A protected member can also be accessed by all classes and sub classes in the same package, but the member can also be accessed by a subclass in another package (but only through inheritance). This example demonstrates this behavior:
Mustakimur Rahman
Greenhorn

Joined: Oct 12, 2013
Posts: 13

Thank you all for your nice explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected member in inherited class
 
Similar Threads
Private member variable inherited??
confusion regarding protected keyword
Possible error in K&B book (resolved - no error)
Private variables aren't inherited?
confusion of protected