Java Inheritance and composition
Inherited
In Java, the extends keyword is used to represent an inheritance relationship, and when a class is created, it is implicitly inherited from the root class object if it does not explicitly indicate which class to inherit.
- Subclass inherits member variables of parent class
- Subclasses can inherit the public and protected member variables of the parent class and cannot inherit the private member variable of the parent class
- if a member variable of the same name appears in the subclass, the member variable of the child class is masked out of the parent class with the same member variable. To access a member variable of the same name in a parent class in a subclass, you need to use the Super keyword to refer to it.
Public classMain {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubPerson p =NewStudent (); System.out.println (P.name); }}classPerson { PublicString name; PublicString sex; Public ShortAge ; PublicPerson () {name= "John"; System.out.println ("It's A person"); }}classStudentextendsperson{ PublicString name; PublicStudent () { This. Name =Super. Name;//using Super to refer to a parent class variableSystem.out.println ("It is a student"); }}//OutputIt is a personit a studentjohnView Code
- Subclass inherits methods of parent class
- can inherit the public and protected member methods of the parent class, and cannot inherit the private member method of the parent class
- If a member method of the same name appears in a subclass, it is called overwrite, which means that the member method of the child class overrides the Member method of the parent class. If you want to access a member method of the same name in a parent class in a subclass, you need to use the Super keyword to refer to it
- For a static method of a parent class, it can be inherited but cannot be overridden by a method with the same name in the class, and the member variables of the parent class can be hidden in a subclass, only the variable names that are declared are the same, and the parent class member variables can be hidden, even if the variables have different data types.
Public classMain {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubPerson p =NewStudent (); P.printname (); P.printage (); }}classPerson {PrivatePet p =NewPet (); /*static{System.out.println ("person"); }*/ PublicString name; PublicString sex; Public ShortAge ; PublicPerson (String name) { This. Name =name; } Public voidPrintname () {System.out.println ("My name is" +name); } Public Static voidPrintage () {System.out.println ("This was used to print the people's age"); }}classStudentextendsperson{ PublicString name; Public intAge ; PublicStudent () {Super("John"); This. Name =Super. Name;//using Super to refer to a parent class variable } Public Static voidPrintage () {System.out.println ("This was used to print the student's age"); } Public voidPrintname () {System.out.println ("It's used to print the student ' s name"); }}//OutputIt's used to print the student ' s namethis are used to print the people's ageView Code
- Constructors
- A subclass is a constructor that cannot inherit the parent class, but if the constructor of the parent class is parameterized, the constructor of the subclass must be displayed with the Super keyword to invoke the constructor of the parent class with the appropriate argument list. and must be the first statement of a subclass constructor. If the parent class has a parameterless constructor, it is not necessary to call the parent class constructor with the Super keyword in the subclass's constructor, and if the Super keyword is not used, the system automatically calls the parent class's parameterless constructor.
classPerson { PublicString name; PublicPerson (String name) { This. Name =name; } Public voidPrintname () {System.out.println ("My name is" +name); }}classStudentextendsperson{ PublicStudent () {Super("John"); } Public voidPrintname () {System.out.println ("It's used to print the student ' s name"); }}View Code
Combination
A combination is the addition of the object of the class that will be assembled to the class as its own member variable, while the class is being designed.
classStudentextendsperson{ PublicString name; PublicSchool School;//Combination PublicStudent () {Super("John"); This. Name =Super. Name;//using Super to refer to a parent class variable } Public voidPrintname () {System.out.println ("It's used to print the student ' s name"); }}classPerson { PublicString name; PublicPerson (String name) { This. Name =name; } Public voidPrintname () {System.out.println ("My name is" +name); }}classSchool { PublicString name; PublicString address; Public voidPrintname () {System.out.println ("School Name:" +name); } Public voidprintaddress () {System.out.println ("School Address" +address); }}View Code
Select the principle of integration or combination in the program:
- If there is a is-a relationship (such as student is a person), and one class needs to expose all the method interfaces to another class, then the inheritance mechanism should be used.
- If there is a has-a relationship (such as student must have a school), then the combination should be used.
Java Inheritance and composition