private类继承和访问控制问题

the truth about private

mr. happy object returns to teach a lesson about how java handles inheritance and access control


summary
-->-->

 
 



after i read your earlier java q&a, "private and final," i tried this code out. surprisingly, it compiled:


class top   
{   
  public top()    {}

  private void toplevel()  
  {
    int i = 2;
  }
}
class bottom extends top    
{
  public bottom() {}

  private void toplevel()  
  {
    int j = 3;
  }
}
public class runit  
{
  public static void main(string[] args)  
  {
    bottom b = new bottom();
    top t = new top();
    system.out.println("working");
  }
}

am i missing something here?

yes, something is missing here: a deep understanding of inheritance and javas access-control rules. dont feel bad, though; many people new to java or object-oriented programming in general fall into this same trap. in a way, everyone "inherits" the same confusion. thats why i am here: to fix confusion wherever it may lurk!
in my answer to "private and final," i claimed that a subclass couldnt override a superclasss private methods. and indeed, a subclass cannot override a superclasss private methods.
oh, sure, you can define a method in the subclass that has the same name as a private method in the superclass. it will compile and it will even run. however, if youre trying to override some behavior defined in the superclass, the subclass will not behave in the way that you might expect.
lets take a look at your example:


class top   
{   
  public top()    {}

  private void toplevel()  
  {
    int i = 2;
  }
}
class bottom extends top    
{
  public bottom() {}

  private void toplevel()  
  {
    int j = 3;
  }
}

here we see two classes: top and bottom. top defines a method private void toplevel() and bottom defines a method private void toplevel(). this code compiles and runs. however, bottom::toplevel() has not overridden top::toplevel(). unfortunately, it is difficult to explain why given this example. so, lets consider another example.
first, lets define mrhappyobject. hes happy:


public class mrhappyobject
{
  private string getmood()
  {
    return "happy";
  }
  public string howdoyoufeel()
  {
    return "i feel " + getmood() + "!";
  }
}

next, lets define mrsadobject. hes sad:


public class mrsadobject
  extends mrhappyobject
{
  private string getmood()
  {
    return "sad";
  }
}

here we have two classes. the superclass defines a public method howdoyoufeel() and a private method getmood(). the subclass inherits the howdoyoufeel() method from mrhappyobject. likewise, the subclass defines a private method getmood().
now, if a subclass could truly override a superclasss private methods, we would expect to hear mrhappyobject tell us that he is happy and mrsadobject tell us that he is sad. lets define a psychiatristobject that will ask each object how it feels:


public class psychiatristobject
{
  public static void main(string[] args)
  {
    mrhappyobject mho = new mrhappyobject();
    mrsadobject   mso = new mrsadobject();

    system.out.println("how do you feel, mr. happy object?");
    system.out.println(mho.howdoyoufeel());
    system.out.println("");
    system.out.println("how do you feel, mr. sad object?");
    system.out.println(mso.howdoyoufeel());
    system.out.println("thank you, objects. that will be $200 for your consultation.");
  }
}

upon executing the psychiatristobject, we get:


how do you feel, mr. happy object?
i feel happy!

how do you feel, mr. sad object?
i feel happy!
thank you, objects. that will be $200 for your consultation.

as you can see, even though we defined a method named getmood in the subclass, we didnt really override anything.
overriding getmood() fails since the jvm wont check to see if getmood() is overridden when howdoyoufeel() calls it. the jvm wont make the check since we defined getmood() as private.
lets fix mrhappyobject so that we can override the mood:


public class mrhappyobject
{
  protected string getmood()
  {
    return "happy";
  }
  public string howdoyoufeel()
  {
    return "i feel " + getmood() + "!";
  }
}

now upon execution of the psychiatristobject we get:


how do you feel, mr. happy object?
i feel happy!

how do you feel, mr. sad object?
i feel sad!
thank you, objects. that will be $200 for your consultation.

because weve now declared getmood() as protected, it is visible to the subclass and we may override it.
when you extend an object, the subclass can only see its superclasss protected and public methods. the access control prevents the subclass from having access to anything declared as private. however, as the readers example illustrates, the subclass can have a member or method that matches the definition of some private method or member in the superclass. this naming clash is possible since the superclasss private member or method is invisible to the subclass.
since the access control of getmood() has changed from private to protected, you must also change the access control to at least protected in all subclasses. remember, subclasses may only widen control access to overridden methods. they may not limit access beyond what the superclass defines. with that in mind, heres the new mrsadobject:


public class mrsadobject
  extends mrhappyobject
{
  protected string getmood()
  {
    return "sad";
  }
}

one more thing, before i get a million and one emails -- mrhappyobject and mrsadobject comprise a crummy inheritance hierarchy. this is a contrived example meant to illustrate the fact that you cannot override private methods. it is not meant to teach good class/inheritance design. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值