Inheritance: A new class can be constructed based on an existing class. Inheriting existing classes can reuse methods and domains of these classes. On this basis, new methods and domains can be added to expand the functions of the class.
public class ExtendsStu {/*Animal class: All animals can move* 1.Dog 2.Cat * In java, subclasses can inherit the properties and functions of the parent class; * Inheritance relationship specification: Subclass extends Parent class* Resources that cannot be inherited: * 1. Subclasses cannot inherit the construction method of the parent class, and must call a parent class constructor (because the parent class attributes will be initialized when generating the child class object) * 2. Private resources cannot be inherited* Special resources: * 1. Static resources can be inherited* Expansion: * The protected modified resources can be accessed in the subclass; (In the case of cross-package inheritance, they can only be accessed within the subclass) * Notes on inheritance: * 1. The inheritance of classes in java is single inheritance; a parent class can have n subclasses* 2. The subclass constructor must call the parent class constructor* 3. When the subclass has an attribute with the same name as the parent class, the subclass object this accesses its own attribute* 4. When generating the subclass object, it will carry all the resources connected to the inheritance; */public static void main(String[] args) {Rose rose = new Rose();rose.type = "rose";rose.sendPeople();//rose.smile = 'Fragrance';Rose.colorFul = true;}}class Flower {public String type;String color;protected double size;static Boolean colorFul;private char smile;public Flower(){}public Flower(String type, String color, double size, Boolean colorFul, char smile) {//super();System.out.println("The parent class parameter constructor was called"); this.type = type; this.color = color; this.size = size; this.colorFul = colorFul; this.smile = smile;}public void sendPeople(){System.out.println(type+"sent");}private void demo(){System.out.println("I am a method private to the parent class");}}class Rose extends Flower{public void hello(){System.out.println("Hello, my smell");//this.demo(); Cannot inherit the method private to the parent class}}Summarize
The above is all about the analysis of inheritance test code in Java. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!