One: Object class
The object class is the root class for all Java classes (ancestor Class)
If the base class is not indicated in the declaration of the class with the extends keyword, the default base class is the object class
1.toString () method
The object class is defined with the public String toString () method whose return value is a String type that describes information about the current object.
If a reference to an object is printed directly, the ToString () method of the object is called by default, and the default printed content contains the memory address pointed to by the reference.
You can override the ToString () method in a user-defined class as needed.
2.equals () method
The object class is defined with the public boolean equals (Object obj) method, which provides the logic to define whether the object is "equal".
The Equals method of object is defined as: X.equals (y), which returns True when X and Y point to the same address, otherwise false.
The Equals (Object obj) method has been overridden in the string class, and the overridden method compares the contents of the two strings (Note: = = compares references to objects).
You can override the Equals method in a user-defined type as needed.
Two: Polymorphism
Encapsulation is an operation to protect a property, and inheritance is to extend the functionality of a class
is another feature of object-oriented programming language derived from encapsulation and inheritance.
From a methodological point of view:
Overloading and overriding of methods
Overloading (overloading): Different functions are performed depending on the parameters passed in
Override: Subclasses override methods in the parent class as required.
instanceof Keywords
In Java, you can use the INSTANCEOF keyword to determine whether an object belongs to an instance of a class
Format:
Object instanceof class? Returns a Boolean type
Final keyword
When declaring classes, properties, and methods in Java, you can use the keyword final to decorate.
Attention:
1.final modifier variable (member variable or local variable) becomes constant and can only be assigned once
final 类型 variableName;
When you modify a member variable, you give the initial value when you define it, and you do not need to modify the local variable.
2.final decoration method, the method cannot be overridden by the quilt class
Final return value type MethodName (paramlist)
{
...
}
3.final decorated class, the class cannot be inherited
Final class finalclassname{
...
}
Three: abstract classes and interfaces
Classes that are decorated with abstract are abstract classes
Abstract class Name {
}
An abstract class cannot be instantiated, must be inherited, and an abstract method must be rewritten to generate its subclasses.
The method that is modified by abstract is an abstract method, and there is no method body for abstract methods.
Abstract classes do not necessarily contain abstract methods, and if the class contains abstract methods, the class must be defined as an abstract class.
If a subclass does not implement all the abstract methods in the abstract parent class, the subclass becomes an abstract class as well.
A method that constructs a method, a static method, a private method, or a final method cannot be declared abstract.
Interface
:
An interface (interface) is a collection of definitions of abstract methods and constant values.
An interface is a "standard", "contract".
In essence, an interface is a special kind of abstract class that can contain only constants and method definitions, without the implementation of variables and methods.
Declaration syntax for an interface
Includes interface declaration and interface body
The complete interface declaration:
[Public] Interface Interface name [extends Listofsuperinterface]
{ ... }
Interface bodies include constant definitions and method definitions
常量定义: type NAME=value; 该常量被实现该接口的多个类共享; 具有public ,final, static的属性.方法体定义:具有 public和abstract属性
Implementation class of the interface
:
As with abstract classes, interfaces must also be used through subclasses, and subclasses implement interfaces through the Implements keyword
A class can implement multiple interfaces, separated by commas in the implements clause
Non-abstract subclasses must implement all the methods defined in the interface
Implementation format:
Class subclass implements interface A, interface B ... {
}
Usage rules for interfaces:
All methods in the interface are public abstract.
When declaring a method in an interface, you cannot use modifiers such as static,final,synchronized,private,protected.
An interface can inherit from another interface.
Multiple inheritance of classes is not allowed in Java, but multiple inheritance of interfaces is allowed.
Interfaces can have data members, which are public static final by default
Purpose of the interface:
(1)通过接口实现不相关类的相同行为,而无需考虑这些类之间的关系.(2)通过接口指明多个类需要实现的方法(3)通过接口了解对象的交互界面,而无需了解对象所对应的类
Java object-oriented polymorphism and abstract classes and interfaces