Java inheritance vs. C# inheritance

前端 未结 4 923
南旧
南旧 2021-01-01 22:08

Let\'s say Java has these hierarchical classes:

class A 
{
}
class B extends A
{
    public void m()
    {
        System.out.println(\"B\\n\");
    }
}
clas         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-01 22:44

    What's the logic behind the Java designer's decision to print C-C-C instead of B-C-C? I mean, why reference B uses the overriding method in C? What's the advantage of this approach?

    Look that you call method from D class object which inherited the M method from C class. The reference type doesn't matter in Java - the important part is which class has referenced object.

提交回复
热议问题