I. Inheritance 1, Concept:
Inheritance is a relationship between classes and is a relationship of "is a"
Ps:java is a single inheritance
2. Advantages:
(1) Subclasses directly have all the properties and methods of the parent class (except Privata)
(2) Implementing code Reuse
3. Grammar rules:
4, a demo
(1) Animal type
Package com.cnblogs; Public class Animal { publicint : Age ; Public String name; Public void Eat () { System.out.println ("eat");} }
(2) Dog type
Package com.cnblogs; Public class extends Animal { }
(3) Use
Package com.cnblogs; Public class Init { publicstaticvoid main (string[] args) { // TODO auto-generated method stub new Dog (); Dog.eat (); = "Sdfsdf"; =; }}
Subclasses can use the parent class directly except for members other than private.
Second, the method of rewriting 1, definition:
If the child class is not satisfied with the method of the parent class, you can override the parent class's method, which takes precedence over the subclass's method when invoked.
2. Grammar rules:
(1) Return value type (2) method name (3) parameter type and number
Is the same as the method inherited by the parent class, which is the override of the method
Iii. sequence of inheritance initialization
1. First initialize the parent class object and then initialize the subclass object
2. Initialize the properties in the object before performing the initialization in the constructor method
Java Learning Notes (ix)--Inheritance