1, inheritance: up-tracing, the same class of abstraction, continuation and extension of all the information of the parent class!
1) Keywords:extends For example, the parent class is animal, and the subclass is dog; Eg: public class Dog extends Animal
2) The parent class is also called a superclass and can be referenced by super
3) Subclasses are also called derived classes: You must inherit the parent class, and you can inherit the properties and methods of the parent class
Rewrite: In the subclass, the method name, formal parameters, number, type, the same order! Keywords: @Override
overloading: In the same class, with the same name, the number of parameters, type, order is different!
Note: When inheriting a method from a parent class, because the parent class does not meet the requirements, we need to override the parent class's methods.
Eg: @Override
public void Eat () {
System.out.println ("Cat eats fish");
}
4) Note: attributes cannot be overridden!
Private methods, static methods, final methods cannot be overridden!
overriding prerequisites Require inheritance!
2. Benefits of Inheritance:
Reuse code, avoid duplication, rewrite methods, new methods, and improve efficiency!
3. Object class
Is the ancestor of all classes, and any class inherits directly or indirectly from the object!.
If a class does not show extends any class, the default inherits the object Class!
4. Example: function: Aniaml class, Dog class, Cat class
/*
*animal class
*/
Public classAnimalextendsobject{//ColorString color; //Age intAge ; Public voideat () {System.out.println ("Food for the Animals"); } Public voidsleep () {System.out.println ("Sleeping Animals"); } Public voidintroduce () {System.out.println ("Self Introduction: Color:" +color+ ", Age:" +Age ); } @Override PublicString toString () {return"Animal color:" +color+ ", Age:" +Age ; } }
Dog class Public classDogextendsanimal{//dog's own peculiar attributesString name; String type; //unique approach to the gatekeeper Public voidGuard () {System.out.println ("Puppy" +name+ "in the janitor, is a good friend of mankind"); } @Override Public voideat () {System.out.println ("Puppy" +name+ "at Dinner"); } @Override Public voidsleep () {System.out.println ("Puppy" +name+ "in Bed"); } @Override Public voidintroduce () {System.out.println ("Dog:color:" +color+ ", Age:" +age+ ", Name" +name+ ", type:" +type); } @Override PublicString toString () {return"Dog's name:" +name+ ", age; +age+ "years old, Kind" +type+ ", Color:" +color; }}
//Cat Class Public classCatextendsanimal{String name; String sex; @Override Public voideat () {System.out.println ("Cat Eats fish"); } @Override Public voidsleep () {System.out.println ("The cat can't see it sleeping."); } @Override Public voidintroduce () {SYSTEM.OUT.PRINTLN (name+ "Meow Meow"); } @Override PublicString toString () {return"Cat name:" +name+ ", Age:" +age+ "age, Gender" +sex+ ", Color:" +color; } }
//Testanimal Public classTestanimal { Public Static voidMain (string[] args) {Animal an=NewAnimal (); An.age=2; An.color= "Yellow"; An.eat (); An.sleep (); An.introduce (); System.out.println (An.tostring ()); System.out.println ("---------------"); Dog Dog=NewDog (); Dog.age=3; Dog.color= "Black and Blue"; Dog.name= "Wang Choi"; Dog.type= "Labrador"; Dog.eat (); Dog.sleep (); Dog.introduce (); Dog.guard (); System.out.println (Dog.tostring ()); System.out.println ("---------------"); Cat Cat=NewCat (); Cat.age= 2;//properties of the parent classcat.color= "White";//properties of the parent classCat.name= "Tutu";//the child's own uniquecat.sex= "Mother";//the child's own uniqueCat.eat ();//rewrite theCat.sleep ();//rewrite theCat.introduce ();//rewrite theSystem.out.println (CAT); } }
Java core technology--inheritance