first Look at a picture:
There are now two classes, student and workers, in order to improve reusability, build only one code, and let one class and another class produce a relationship, the relationship is: inheritance.
But we found that these two classes have no inheritance, but they have something in common, and we can find their shared type, → both students and workers are Person,person with name and age. That
Use keyword Inheritance (extends) in your code to have students and workers relate to person
Class person{string Name;int age;} Class Student extends person//student inherits the person student is the subclass person is the parent class (base class, Super Class) {void study () {System.out.println ("good Good Study");}} Class Worker extends Person{void work () {System.out.println ("hard work");}}
Inheritance Benefits: Improve the reusability of the code, so that classes and classes have a relationship, to another feature polymorphism provides a precondition.
when to define inheritance: you must guarantee that the class and class have an owning (is a) relationship. Apples are one of the fruits. A dog is one of the canine branches.
The embodiment of inheritance in Java: Java allows single inheritance. Do not directly support multiple inheritance, will be multi-inheritance in other ways to reflect. single inheritance: A subclass can have only one parent class.
Multiple inheritance: A subclass can have more than one parent class.
After writing this blog, how to feel it is too simple (I wrote),,,
Java object-oriented inheritance from where, why use, how to use?