How does inheritance in Java work?

前端 未结 6 1931
迷失自我
迷失自我 2020-12-09 21:23

We have next classes:

class Super {
    void foo() {
        System.out.println(\"Super\");
    }
}

class Sub extends Super {
    void foo() {
        super         


        
6条回答
  •  感动是毒
    2020-12-09 21:59

    foo method override in sub class .super.foo() calling print super and then System.out.println("Sub"); shows Sub.

    try this for inheritence

    class Super {
        Super()
        {
            System.out.println("1");
        }
        void foo() {
            System.out.println("Super");
        }
    }
    
    class Sub extends Super {
        public Sub() {
            // TODO Auto-generated constructor stub
            System.out.println("2");
        }
        void foo() {
           super.foo();
            System.out.println("Sub");
        }
    }
    

提交回复
热议问题