-->afterireadyourearlierjavaq&a,"privateandfinal,"itriedthiscodeout.surprisingly,itcompiled:classtop{publictop(){}priva" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

private類繼承和訪問控制問題

系統 2063 0

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.?

private類繼承和訪問控制問題


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产成人 免费观看 | 97免费| 久久国产精品自线拍免费 | 性短视频在线观看免费不卡流畅 | 国产精品久久自在自线观看 | 国产乱子伦一级毛片 | 国产一区二区三区视频 | 欧美精品亚洲人成在线观看 | 偷偷操99 | 99久久国产综合精麻豆 | 九九影院在线观看 | 男人与牛做爰的视频 | 日韩中文字幕在线不卡 | 天天久久狠狠伊人第一麻豆 | 成年女人18毛片毛片免费 | 99精品视频在线在线视频观看 | 可以免费观看的一级毛片 | 国产成人免费片在线观看 | 奇米影音四色 | 亚洲欧美日本视频 | 免费福利视频在线观看 | 久久国产热这里只有精品8 久久国产三级 | 免费欧洲毛片a级视频老妇女 | 波多野结衣手机视频一区 | 四虎影视在线观看永久地址 | 久久综合九色综合97伊人麻豆 | 东北一级毛片 | 欧洲美女a视频一级毛片 | 按摩毛片 | 欧美精品亚洲网站 | 欧美日韩一区二区在线观看视频 | 伊人激情久久综合中文字幕 | 性欧美极品xxxx欧美一区二区 | 亚洲高清资源 | 黑人特级粗α级毛片 | 四虎在线永久免费视频网站 | 538在线视频二三区视视频 | 五月天婷婷网站 | 国产成人亚洲精品91专区高清 | 人人揉揉香蕉大免费不卡 | 国产香蕉一区二区精品视频 |