1. Super class and subclass superclass and subclass
Parent class and Child class
Polymorphic:
An object variable can indicate that a number of actual types of phenomena are known as polymorphic
A variable can refer to the parent class object or its subclass object, which is polymorphic.
You cannot assign a reference to a superclass to a subclass variable because a run-time error may occur when a subclass method is called
Subclass references can be assigned to super class references
Dynamic binding:
The phenomenon of being able to automatically select which method at run time is called dynamic binding
is a very important feature, you can extend the program without modifying the existing code.
Block Inheritance: Final Classes and methods
Declaring methods and classes as final the primary purpose is to ensure that they do not alter the statement in the subclass
Forcing type conversions
You can only type conversions within an inheritance hierarchy.
Before you convert a superclass to a subclass, you should use instanceof to check.
Abstract class
Use the abstract keyword so that you do not need to implement this method at all.
To improve program clarity, a class that contains one or more abstract methods must itself be declared abstract .
In addition to abstract methods, abstract classes can also contain specific data and specific methods.
Try to place common fields and methods (whether abstract or not) in a superclass (whether abstract or not)
Extending abstract Classes
1 A subclass defines a partial abstract or abstract method and does not define it, so that subclasses must also be marked as abstract classes
2 Define all the abstract methods, subclasses are not abstract
Abstract classes cannot be instantiated (not new) objects of this class
You can create an object of a specific subclass that defines an abstract class variable, but it can only reference objects that are not abstract subclasses.
Protected Access
Private subclasses do not have access to the superclass's proprietary domain.
Protected allow subclasses to access a domain of a superclass
2, Object: All classes of Super class
object is the ancestor of Java, and every class in Java is extended by it.
You can reference any type of object using a variable of type Object
Eqluas method
Used to detect whether an object is equal to another object, in the object class, this method will determine whether two objects have the same reference.
When you define the Equals method in a subclass, the equals of the superclass is called first. If the detection fails, the objects cannot be equal.
If the fields in the superclass are equal, you need to compare the instance domains in the subclasses.
Equality Test and inheritance
The Java language specification requires the Equals method to have the following characteristics:
Reflexivity: X.eqluas (x) returns True
Symmetry: Returns True if and only if X.eqluas (y) returns True,y.eqluas (x)
Transitivity: If X.eqluas (y) returns True,y.eqluas (z) returns True,x.equlas (Z) should also return true
Consistency: Repeated calls to X.eqluas (Y) should return the same result if x, y refers to an object that has not changed
X,x.eqluas (NULL) should return FALSE for any non-null reference
Symmetry, Comparison of instanceof subclasses and super-classes
- If a subclass can have its own concept of equality, the symmetry requirement will force detection with GetClass
- If the concept of equality is determined by the superclass, then the instanceof can be used for detection, so that an equal comparison can be made between objects of different subclasses.
Suggestions for writing a perfect Equals method:
1 The explicit parameter is named Otherobject, and later it needs to be converted to another variable called other
2 detects if this and otherobject refer to the same object:
if (this = = Otherobject) return true;
3 detects if Otherobject is null, if NULL, returns false
if (OTHEROBJECR = = null) return false;
4 Compare whether this and otherobject belong to the same class.
If the semantics of equals are changed in each subclass, use GetClass to detect
if (getclass! = Otherobject.getclass ()) return false;
If all subclasses have uniform semantics, use instanceof to detect
if (!otherobject instanceof ClassName) return false;
5 converting Otherobject to the appropriate class-type variable
ClassName other = (ClassName) otherobjecrt;
6 Start comparing all the fields that need to be compared now. Use = = to compare the base type fields, using equals to compare object fields.
If you redefine equals in a subclass, call Super.equlas (other);
Hashcode method
A hash code (HASHCODE) is an integer value that is exported by an object.
Hashcode is defined in the object class, so each object has a default hash code whose value is the location where the object is stored.
If you redefine the Equals method, you must redefine the Hashcode method so that the user can insert the object into the hash table
ToString Method
3. Generic array list access array list object
Arraylist<t>
Do not indicate the size of the array
Add any number of elements to an array using add
Use size () instead of length to calculate the number of elements
Accessing elements using A.get (i) instead of a[i]
4. Object wrapper and Automatic packing
Base type conversion to Object
5, variable number of parameters method
Ellipsis ... is part of the Java code, which indicates that this method can receive any number of objects
6. Enumeration class
Enum
7. Reflection
Reflection mechanism:
Ability to analyze classes in the run
viewing objects in Run
Implementing a common array manipulation code
Using Method objects
Class
Class.forName (). newinstance ();
Catching exceptions
Try Catch
Ability to use reflection to analyze classes
Field Method Constructor
Three classes are available
GetName returns the name of the project
GetModifiers returns an integer value that describes the modifier
Field GetType returns a class object that describes the type of the domain
Getfileds
Getdeclaredfileds
Using reflection to parse objects at run time
Setaccessible
Writing generic array codes using reflection
CopyOf
Call any method
Method invoke
8, inherit the design skill
1. Put public operations and domains in super class
2. Do not use protected domains
3. Using inheritance to realize is-a relationship
4. Do not use inheritance unless all integrated methods make sense
5. Do not change the expected behavior when overriding the method
6. Use polymorphic, not type information
7, do not use the reflection too much
Java basic Syntax < eight > inheritance