Inherited
Benefits of Inheritance: Subclasses have all the properties and methods of the parent class, the peivate adornment is not valid; Implement code reuse
class Subclass extends Parent class such as: class dog extends animal{ } Parent class: Package com.imooc;public class animal { public int age; public string name; public void eat () { system.out.println ("Animals have food"); }} Subclass: package com.imooc;public class dog extends animal {} Test Class:package Com.imooc;public class initail { public static void main (String[] args ) { // TODO Auto-generated method stub dog dog = new dog (); dog.age = 10; dog.name = "Xiaohui"; dog.eat (); }}
Rewrite
Method overrides: Subclasses can override methods inherited by the parent class when the calling method takes precedence over methods of calling subclasses
Syntax rules: A: Return value type B: Method name C: The parameter type and number are the same as the method inherited by the parent class, only called method overrides
Rewrite Dog class: Package Com.imooc;public class Dog extends Animal {public void Eat () {System.out.println ("Age" +age+ "dog Can Eat");}}
3. Initialization order of inheritance: 1) initializing the parent class and then initializing the subclass 2) executes the properties in the initialization object before executing the initialization in the constructor method
Final use in Java
Final, non-modifiable meaning; Final can modify classes, methods, properties, and variables. Final decorated class, the class is not allowed to be inherited; The method cannot be overridden, the property is not implicitly initialized (the class initialization property must have a value), or it can be assigned in a constructor method (but only one is selected). Final modified variable, the value of the variable can only be assigned once, which becomes a constant.
The use of super in Java
Super: Used inside the object, can represent the parent class object
Access the properties of the parent class object: Super.age; Methods to access the parent class object: Super.eat ()
Super Application: 1) The construction of the subclass must call the constructor of its parent class, the system calls the parent class without a parameter constructor, implicitly calls Super (), 2) if the subclass constructor method does not explicitly call the constructor of the parent class, and the parent class does not have a parameterless constructor method, the compilation error occurs. You need to add super () to the first line in the subclass construction method.
The object class in Java
The 1.Object class is the parent class for all classes, and if a class does not inherit another class using extends, the class inherits the object class by default. The methods in the object class are suitable for all subclasses.
1) toString (): Returns the hash code for the object, which can be represented by overriding the ToString () method--eclipse has the method of automatically helping us to rewrite toString (),eclipse->source-> Generate toString ()
2) equals (): Compares whether the reference to the object is pointing to the same piece of memory as dog dog = new Dog ()
In general, when comparing two objects, it is more consistent to compare his values, so rewrite them.
eclipse->source-> Generate hashcode () and Equals ()
When overridden, compares the values of two objects:
public boolean equals (Object obj) {if (this = = obj) return true; if (obj = = null) return false; if (getclass () = Obj.getclass ()) return false; Dog other = (dog) obj; if (age! = Other.age) return false; return true; }
Java Learning Note 3--inheritance