JAVA basis (inherited Notes)

1, inherited Notes

  • Subclass can only inherit all non-private members of the parent class (member methods and member variables)

  • Subclass can not inherit the parent class constructor, but by the super keyword to access the parent class constructor.

  • In order to do away inherit some functions

 

2, when to use inheritance

  • Inheritance is the embodiment of a relationship: "is a".

      

        Person

            Student

            Teacher

        fruit

            apple

            banana

            orange

 

            

  • Using the dummy method.

    •  If there are two classes A, B. Only they meet is a B of A or B is an A, you can consider using inheritance.


 

class Demo3_Extends {

    public static void main(String[] args) {

        Son s = new Son();

        s.show();

    }

}





class Father {

    private String name;

    private void show() {

        System.out.println("Hello World!");

    }

    

}





class Son extends Father {

    

}

 

Guess you like

Origin blog.csdn.net/Cricket_7/article/details/92060927