JAVA Note 11 – Polymorphic and abstract methods
1.Object:
Object is the parent of all classes, and all objects (including arrays) implement this class’s method, inheritance
2. Override equals ();
1. If the addresses of the two objects are the same, the two objects must be equal
2. If the types of the two objects are different, the two objects are not equal
3. If the addresses are different but the types are the same, compare the attributes of the two objects to determine whether they are the same
Homework:
Override equals to compare student objects
studentNo,name,age,height;
3. Tamagotchi System
Parent: Pet:
-name,-health,-love; Nicknames, health values, intimacy
+ print, set/get — — –
Subclass: Dog extends Pet
– strain, breed
Subclasses: Penguin
Sex, sex
4. Abstract methods:
a.why:
In some cases, multiple subclasses need to override a method of the same name as the parent class, but each subclass has a different implementation, and the parent class’s implementation of that method doesn’t make any sense — redundant?
b.what:
The method modified by abstract is abstract method, which has no method body.
C. the grammar:
Modifier abstract returns the method name (argument list);
D. Notes:
1). The abstract method must be in the abstract class;
2). An abstract method must be implemented in its subclass unless the subclass is also an abstract class;
5. Abstract class:
a.what:
The class modified by abstract
B. Matters needing attention:
1). Abstract classes cannot be instantiated — objects cannot be created directly;
2). Abstract classes may not have abstract methods, but if a class contains abstract methods, the class must be abstract
6.final:
1). Final class:
Classes decorated by fianL are not allowed to inherit;
2). The final print () :
Methods modified by final are not allowed to be overridden;
3). The fianl variables:
Variables modified by final are constants and cannot be reassigned, even if the reassigned value is the same
(*) Use final to modify reference variables, where the value of the variable is fixed and the value of the property of the object to which the variable refers is mutable
————————————————————
Question: Pet feeding method optimization?
1. The polymorphism:
a.why:
In some cases, the method body is the same, but inconsistent conditions lead to inconsistent results, if you write multiple method items – poor scalability, poor maintainability;
B. Use different instances of the same reference type to perform different operations — a parent reference refers to a subclass object
c.how:
1) Realization conditions of polymorphism:
Write parent and subclass classes with inheritance relationships
A subclass overrides a parent class method
Use a parent class reference to point to a subclass object — how to point;
2) Realization method of polymorphism:
Implement polymorphism using a parent class as a method parameter
Implement polymorphism using the parent class as the method return value
3) eg:
Parameters – Feeding cases
Return value — Adoption case
Combined use – the play case
2.instanceof:
A. the grammar:
Object Instanceof class or interface
Return true if the left-hand object is an instance of the right-hand class, false otherwise;
Return true if the left-hand object implements the right-hand interface, false otherwise;
(***) This keyword is usually used in conjunction with type conversions
Question:
How are abstract methods used in practice? How to solve the problem of using abstract classes?
There are only abstract methods in a class – interfaces