Inherits the abstraction of a batch of classes, reduces the amount of code, facilitates the modification of the method, and manages the class.
Subclasses cannot inherit from parent class: ① constructor method ② private property and method ③ Use default modifier not under the same package
Inheritance initialization: Parent class Property, parent class, child class property, subclass constructor method
Inheritance: ① overload ② override ③ Interface ④ parent class object reference to Child class object
Rewrite rule: ① method name ② parameter list same ③ return value type is same or its subclass ④ access adornment permission cannot be strict with parent class
Private (This class) >default (same package) >protected (same package and sub-category) >public (All)
The Super/this keyword cannot be applied to the static decoration method
Package com.huidao.day10;
/* Question:
Realize the host and pet play function
Play a Frisbee game with your dog.
Play swimming games with penguins.
------Inheritance
Analysis:
How to add a Frisbee to dog Catchingflydisc ()
Add Swimming method to Penguin swimming ()
Add the play (Pet Pet) method to the owner */
public class Test03 {
public static void Main (string[] args) {
Master master = new Master ();
Pet dog = new Dog ();
Master.play (dog);
Pet penguin = new Penguin ();
Master.play (Penguin);
}
}
Class master{
Pet Pet;
public void Play (Pet pet) {
Pet.shout ();
if (pet instanceof Dog) {
Dog dog = (dog) pet;
Dog.catchingflydisc ();
} else if (pet instanceof Penguin) {
Penguin Penguin = (Penguin) pet;
Penguin.swimming ();
}
}
}
Class pet{
public void Shout () {}
}
Class Dog extends pet{
Overridden methods
public void shout () {
System.out.println ("Barking Dog");
}
Common methods
public void Catchingflydisc () {
System.out.println ("Pick up the Frisbee");
}
}
Class Penguin extends pet{
public void shout () {
System.out.println ("Penguin bark");
}
public void swimming () {
System.out.println ("swimming");
}
}
Java object-oriented (inheritance)