Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

protected access in Object class

Question posted by: pjerald (Member) on May 15th, 2008 01:46 PM
Clear me friends.

Object class is the base class for all classes. Thus the methods with protected access in the object class can be invoked by any object(Of any class).

I know the statement is wrong!.

But i cannot convince myself(Since i don't know it clearly).

Shed some light.

Thanks
Jerlad
JosAH's Avatar
JosAH
Chief Editor
7,787 Posts
May 15th, 2008
03:16 PM
#2

Re: protected access in Object class
Quote:
Clear me friends.

Object class is the base class for all classes. Thus the methods with protected access in the object class can be invoked by any object(Of any class).

I know the statement is wrong!.


It's not wrong but it needs a bit more detail: a protected method is visible in any
of the (descending) classes where the method is defined. So not just any other
object can invoke that method on any other object, i.e. it has to be at least a
descendant of the other object's class. A short example:

Expand|Select|Wrap|Line Numbers
  1. class B { protected void m() { ... } }
  2. class D extends B { ... }
  3. class E extends B { ... }
  4. class Derived extends D { ... }
  5. ...


Now any D can call m() on any B and D. Any E can call m() on any E and B
and any Derived can call m() on any other Derived, D and B but not on an E object.

But none of the D objects can call m() on an E object, nor can any E object
call m() on any D object.

kind regards,

Jos

Reply
pronerd's Avatar
pronerd
Expert
334 Posts
May 15th, 2008
03:22 PM
#3

Re: protected access in Object class
Quote:
Object class is the base class for all classes. Thus the methods with protected access in the object class can be invoked by any object(Of any class).

I know the statement is wrong!.


I am not sure I follow the question. It is hard to make out what is being asked. Any class that extends java.lang.Object class can access its protected methods.

Protected status just means that the method or object in question can not be accessed outside of the package it is in. If you are extending that class you are actually making your object part of that object so you are inside the access boundaries.

This section of the Java tutorial describes what the access control arguments mean.
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

Reply
BigDaddyLH's Avatar
BigDaddyLH
Moderator
1,211 Posts
May 15th, 2008
03:54 PM
#4

Re: protected access in Object class
Quote:
Protected status just means that the method or object in question can not be accessed outside of the package it is in.


That's incorrect. You are thinking of the default access level:

Expand|Select|Wrap|Line Numbers
  1. package com.acme.utils;
  2. public class Helper {
  3.     public void pub() {}
  4.     void packageLevel() {}
  5. }


package elsewhere;
imports com.acme.utils.Helper;
...
Helper x = new Helper();
x.pub(); //okay, calling a public method
x.packageLevel(); //error. Can only be accessed with package com.acme.utils or from withing subclasses of Helper.

Reply
pjerald's Avatar
pjerald
Member
64 Posts
May 17th, 2008
09:03 AM
#5

Re: protected access in Object class
Quote:
Expand|Select|Wrap|Line Numbers
  1. class B { protected void m() { ... } }
  2. class D extends B { ... }
  3. class E extends B { ... }
  4. class Derived extends D { ... }
  5. ...


Now any D can call m() on any B and D. Any E can call m() on any E and B
and any Derived can call m() on any other Derived, D and B but not on an E object.

But none of the D objects can call m() on an E object, nor can any E object
call m() on any D object.

kind regards,

Jos


Sorry for the delayed replay. Jos I cannot understand any D can call m() on any B and D. Can you please explain that.

My understanding is this
Expand|Select|Wrap|Line Numbers
  1. class F{
  2. class B { protected void m() { ... } }
  3. class D extends B { ... }
  4. class E extends B { ... }
  5. class Derived extends D { ... }
  6. ...
  7.  
  8. C c = new C();
  9. c.m();
  10. // c invokes m. and the m is not defined inside C. 
  11. // this is valid since m has protected acccess.
  12.  
  13. D d = new D();
  14. d.m(); //valid
  15.  
  16. E e = new e();
  17. e.m(); //valid
  18.  
  19. Derived derived = new Derived();
  20. derived.m(); //valid
  21.  
  22.  
  23. }


I am introducing a class F here. Can you please explain "any D can call m() on any B and D."

Reply
Reply
Not the answer you were looking for? Post your question . . .
190,182 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Java Forum Contributors