Object-Oriented Programming (9)-inheritance, rewriting, Object class, object-oriented programming object
Three features of object-oriented architecture:Inheritance;Encapsulate/hide;Polymorphism(To adapt to various changes in requirements, make the code more generic !)
Inheritance (extends)
- Class is the abstraction of objects, inheritance is the abstraction of a batch of classes, so as to achieve better modeling of the real world. (From the perspective of OOA and OOD, that is, object-oriented design)
- Inheritance can improve code reusability! (From the perspective of OOP)
- Extends means "extension ". Subclass is an extension of the parent class.
[Example 1] Do not use inheritance. Here, it is very troublesome to use Animal. Therefore, the concept of inheritance is naturally generated.
// One source file can define multiple classes // Animal class public class Animal {String eye; public void run () {System. out. println ("Run");} public void eat () {System. out. println ("eat") ;}/// Mammal class Mammal {String eye; public void run () {System. out. println ("Run");} public void eat () {System. out. println ("eat");} public void taiSheng () {System. out. println ("I Am a birthpoint") ;}// crawler class Paxing {String eye; public void run () {System. out. println ("Run");} public void eat () {System. out. println ("eat");} public void eggSheng () {System. out. println ("I am an egg ");}}
After inheritance:
// One source file can define multiple classes // Animal class public class Animal {String eye; public void run () {System. out. println ("Run");} public void eat () {System. out. println ("eat") ;}// the Mammal class Mammal extends Animal class of the Mammal extends from the Animal class {// What does the Animal class have, what does the Mammal class have? add your own public void taiSheng () {System. out. println ("I Am a birthpoint") ;}// crawler class Paxing extends Animal {public void eggSheng () {System. out. println ("I am an egg ");}}[Summary]
- Subclass inherits the parent class to obtain all attributes and methods of the parent class (except the constructor of the parent class).
- In Java, classes only have single inheritance, and (a class has only one direct parent class) do not have as many inheritance as C ++.Multi-inheritance can lead to confusion, making the inheritance chain too complex and difficult to maintain the system. In reality, if you have multiple parents, how chaotic the world is.Multi-inheritance aims to achieve code reusability, but introduces complexity, resulting in chaotic relationships between system classes.
- Multi-inheritance in Java, which can be implemented through interfaces.
- If extends is not called when defining a class, its parent class isJava. lang. Object.
- Different names: superclass, parent class, base class, subclass, and derived class
|
Method override (overwrite, override)
How to define Rewriting: In a Java program, the inheritance relationship of classes can generate a subclass that inherits the parent class and has all the features of the parent class, inherits all methods and variables of the parent class. Child classes can define new features. When child classes need to modify some methods of the parent class to expand and increase functions, programmers often refer to such an operation method as rewriting, it is also called overwrite or overwrite.
Rewriting represents the superiority of Java. Rewriting is based on the inheritance relationship, which makes the language structure richer. In Java inheritance, subclasses can hide and access methods of the parent class, or override methods that inherit the parent class. The method that overwrites the parent class in Java is implemented through method rewriting.
Method rewriting means that the method in the subclass has the same return value type, Method Name, number of parameters, and parameter type as the method inherited in the parent class. In this way, the parent class method can be overwritten.
- In subclass, You can override the methods inherited from the base class as needed.
- The override method must have the same method name, parameter list, and return type as the method to be rewritten.
- The method to be rewritten cannot use more strict access permissions than the method to be rewritten (due to polymorphism)
- Note: overload has nothing to do with rewriting.
[Example code]
Parent class Person class:
public class Person { private int age; private String name; public void setAge(int age){ this.age = age; } public void setName(String name){ this.name = name; } public int getAge(){ return age; } public String getName(){ return name; } public String getInfo(){ return "Name is:"+name+",Age is "+age; }}
Subclass Student class:
public class Student extends Person{ private String school; public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public String getInfo(){ return "Name is:"+getName()+",Age is "+getAge()+",School is:"+school; }}
Test code:
public class TestOverRide { public static void main(String[] args) { Student student = new Student(); Person person = new Person(); person.setAge(1000); person.setName("lili"); student.setAge(23); student.setName("vic"); student.setSchool("shnu"); System.out.println(person.getInfo()); System.out.println(student.getInfo()); }}
Running result:
Name is: lili, Age is 1000 Name is: vic, Age is 23, School is: shnuView CodeObject class
Check the source code of the Object and pay attention to the following methods:
Equals () method
ToString () method
- Rewrite: toString Method
- Returned by default: package name + class name + @ + hash code
- The hash code is generated based on the memory location of the object and is unique.
- The toString method can be rewritten.
[Example] Mobile
Public class Mobile {// If the extends keyword is not used in the class declaration to specify its base class, the default base class is the Object class // override the toString () of the parent class Object class () public String toString () {return "I Am a mobile phone ";}}
Test Object class:
Public class TestObject {public static void main (String [] args) {Object obj = new Object (); Object obj1 = new Object (); System. out. println (obj. toString (); // java. lang. object @ c3c749: class name + @ + hash code System. out. println (obj1.toString (); // The hash code is based on the address. System. out. println (obj = obj1); // false System. out. println (obj. equals (obj1); // false Mobile m = new Mobile (); System. out. println (m. toString ());}}
Execution result:
Java.lang.Object@c3c749java.lang.Object @ 150bd4dfalsefalse I'm a mobile phoneView Code
Ps:
View the inheritance structure (Type Hierarchy) in myeclipse/eclipse: place the cursor over the class name, press ctrl + t, or right-click the class name and select open Type Hierarchy (or F4)
The Mammal class inherits Animal and Animal inherits Object.