• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Protected methods in different packages

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been trying the following code and it is not working. Can anyone let me know what seems to be the problem? (tried on windows 98 and 2000)

my first question is why is the protected amethod() of base class not accessible from derived class and second as to why polymorphism is not working when i use protected method? If i have derived class in the same package as the base class it works fine.

These are the errors being reported. Please point out the error.
Thanks.
[ May 07, 2002: Message edited by: malini griddaluri ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read JLS 6..6.2 to understand the details of the protected access.
b1.amethod is not allowed because the type of the reference (base) is not derived or one of its subclasses.
derived d = new derived()
d.amethod
is ok.
The message given by the compiler gives a clue about the problem.
[ May 07, 2002: Message edited by: Jose Botella ]
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its because you are trying to access the base class protected method outside the derived class. If you do this:

in the main everything works fine. If you change the method in the base class to public everything also works fine.
Hope this helps.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Malini, that's a tricky concept here's what's in my notes hope it helps:
128 A subclass in another package can only access protected members in superclass via references of it's own type(new A().i) or a subtype (new B().i). It is not possible to access it by reference to superclass.
In this case of A a = new B() - a becomes reference to the superclass for B
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has been a real surprise for me as I've always heard that listing a method as protected meant that it could be accessed from any class in the same package or a subclass of the enclosing class. I took the above statement to mean that, when considering access to a protected method, I should ask myself the following question:
In what type of enclosing class does the line b1.amethod() appear? Is the enclosing class (i.e. derived in the example) in the same package as, or a subclass of, the base class? If so, then the access is permitted.
But from all of the above posts, I've obviously been mistaken. I'm wondering if the statement i've written above in bold is just plain wrong, or if I've misunderstood what it means? Could someone help me figure out where I've gone wrong?
[ May 07, 2002: Message edited by: Dave Winn ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this section of the JLS: §6.6.7 Example: protected Fields, Methods, and Constructors. There is a nice example there that illustrates what is happening in this case.
If you have more questions after looking at that, let me know.
Corey
 
malini griddaluri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Thank you very much for the clarification but I have a few more doubts. I perfectly understood what mike kelly was referring to :
That you can access it only thru a reference of type derived or any of its sub class and not thru a reference of super class type but when i went to the J.L.S and read that section about the protected method access specification the following sentence seemed a little bit confusing .
"
A compile-time error occurs in the method delta here: it cannot access the protected members x and y of its parameter p, because while Point3d (the class in which the references to fields x and y occur) is a subclass of Point (the class in which x and y are declared), it is not involved in the implementation of a Point (the type of the parameter p).
"
could any of you explain what this means? what exactly is meant by " it is not involved in the implementation"?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for this explaination Jose, Corey, Mike et al as I too was somewhat confused about the meaning of protected although I didn't know it until I saw this post!
R
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A derived class is not said to be involved in the implementation of an instance of the base class, because the instance fields of the base class are initialized by the constructor of the base class, not by the constructor of the derived one. We could say that the base class is involved in the implementation of the instances of its subclasses, because once initialized the instance fields, they are inherited by the derived.
This concept has buffled everybody reading the JLS, I guess.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you call amethod() of base class from derived class?
 
reply
    Bookmark Topic Watch Topic
  • New Topic