Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

doubt in access modifier. Please explain this code

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
package certification;
public class Parent{
protected int x=9;
}

package other;
import certification.Parent;
class Child extends Parent {
public void testIt(){
Parent p=new Parent();
System.out.println("X in parent is" + p.x);
}
}

In the above mentioned code since the child class is in different package and creating a instance of superclass and accessing its protected member from an instance its a compiler error but in the below mentioned code if it accessed in the same way in the same package it works. Can anybody explain why it happens like this. Why it works in the same package but not in the different package.

"Code in the same package"

package certification;
public class Parent{
protected int x=9;
}

class Child extends Parent {
public void testIt(){
Parent p=new Parent();
System.out.println("X in parent is" + p.x);
}
}

Thanks All
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nikhil,




1- A member variable marked protected can only be accessed in the same package. And it can be accessed by the subclass (in the different package) without dot (.) operator after Parent class reference variable.
You are trying to access the protected member of the Parent class in the Child class that is in the different package using the Parent class reference variable hence compiler error.

See the code below, how you can access x of the Parent class




Didn't you come back to this post
doubt in static method

Regards,
cmbhatt
[ April 29, 2007: Message edited by: Chandra Bhatt ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have one doubt when it is accessed with "."(dot) operator in the differnt package in a subclass it gives compiler error but if it is accessed in the same package in a subclass it does not give a compiler erroe. Why?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:
I have one doubt when it is accessed with "."(dot) operator in the differnt package in a subclass it gives compiler error but if it is accessed in the same package in a subclass it does not give a compiler erroe. Why?




That is what protected access means!

You want to allow other packages subclass to see your protected members through inheritance only, not through reference variable of that class.

Until the member variable is private, classes, subclass of the same package can access that using "." operator, no issue.



cmbhatt
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks chandra bye
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic