protected access in Object class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pjerald
    New Member
    • Oct 2007
    • 77

    protected access in Object class

    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
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by pjerald
    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:

    [code=java]
    class B { protected void m() { ... } }
    class D extends B { ... }
    class E extends B { ... }
    class Derived extends D { ... }
    ...
    [/code]

    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

    Comment

    • pronerd
      Recognized Expert Contributor
      • Nov 2006
      • 392

      #3
      Originally posted by pjerald
      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.Objec t 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.h tml

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by pronerd
        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:

        [CODE=Java]package com.acme.utils;
        public class Helper {
        public void pub() {}
        void packageLevel() {}
        }[/CODE]

        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.

        Comment

        • pjerald
          New Member
          • Oct 2007
          • 77

          #5
          Originally posted by JosAH
          [code=java]
          class B { protected void m() { ... } }
          class D extends B { ... }
          class E extends B { ... }
          class Derived extends D { ... }
          ...
          [/code]

          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
          [code=java]
          class F{
          class B { protected void m() { ... } }
          class D extends B { ... }
          class E extends B { ... }
          class Derived extends D { ... }
          ...

          C c = new C();
          c.m();
          // c invokes m. and the m is not defined inside C.
          // this is valid since m has protected acccess.

          D d = new D();
          d.m(); //valid

          E e = new e();
          e.m(); //valid

          Derived derived = new Derived();
          derived.m(); //valid


          }
          [/code]

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

          Comment

          Working...