1. Concepts and implementation of inheritance
The concept of inheritance is everywhere in life
The relationship that inheritance needs to conform to: is-a; Parents are more generic, abstract, subclasses are more special, and specific
The idea behind inheritance is to build new classes based on existing classes
When inheriting from an existing class, its methods and properties are reused and can be added
New methods and attributes to customize new classes to meet demand
. Convention: Classes exported from other classes are called subclasses.
The exported class is called the parent class
In java, all classes are subclasses except the Object class, all of which are
Unique parent class
In the java language, the extends keyword is used to indicate that one class inherits another
2. super keywords
1. Features of the super keyword
The super and this keywords are similar in that they represent references to parent objects
. When members of a child's parent class have the same name, they can be distinguished by super
In the construction method of a subclass, invoke the construction method of the parent class through the super keyword
2. Code implementation
package jichneg.su.www; class Preson { String name; String gender; public Preson(String name, String gender) { this.name = name; this.gender = gender; System.out.println("Construction methods for parent classes"); } public void eat() { System.out.println("People Drink Water"); } public void eat1() { System.out.println(); } } class Student extends Preson { public Student(String name, String gender) { super(name, gender); // In the subclass construction method, the parent class construction method is invoked by the super keyword // Since inheritance, of course, you need to know the specifics of the parent class (construction method) System.out.println("Construction methods of subclasses"); } int eag; String calss; public void eat() { super.eat(); //When a member of a child class and a parent class have the same name, they are distinguished by the super keyword System.out.println("Children Drink Water"); } } public class Demo11 { public static void main(String[] args) { // Preson sa=new Preson("Zhang San", "1002"); Student so = new Student("kitten", "1001"); so.eat(); } }
3. Method Rewrite-Override
1. What is method override
Method overrides refer to subclasses that can override methods inherited from the parent class as needed.
Is the prelude to the polymorphic mechanism
2. Method Rewrite Notes
The overridden method must have the same method name, parameter list, and return value as the overridden method
Rewrite methods cannot have stricter access than overridden methods
Private methods in the parent class cannot be overridden
.Continuing to call the method whose parent class is overridden in the method of subclass override can be obtained by super.function name
3. Code implementation
package jichneg.su.www; class Preson { String name; private String gender; protected void eat() { System.out.println("People Drink Water"); } public void eat1() { System.out.println(); } } //The overridden method must have the same method name, parameter list, and return value as the overridden method //Rewrite methods cannot have stricter access than overridden methods //Private methods in the parent class cannot be overridden //.Continuing to call the method whose parent class is overridden in the method of subclass override can be obtained by super.function name class Student extends Preson { int eag; String calss; String gender; public void eat(){ //Subclass override methods should have a larger range of access modifiers super.eat(); System.out.println("Children Drink Water"); } } public class Demo11 { public static void main(String[] args) { Student so=new Student(); so.eat(); // so.gende="other"; } }
4. Common methods of Object class
In java, all classes inherit directly or indirectly from the java.lang.Object class
Object is arguably the ancestor of all classes in java, that is, the root class
. Any class in java inherits the methods in the Object class, mainly
toString()
equals()
hashcode()
clone()
getClass()
finalize()
String toString()
Returns the string description of the object. Default output format:
Class Name [Field Value, Field Value...]
Whenever an object is connected to a string through'+', the system automatically calls toString to get the object's
String Descriptor
Frequently rewritten: can be rewritten to suit the needs of the user
Boolean equals()
The original function of Object class is to judge whether two objects have the same reference, and to judge two objects
Equality of states.