1. Inheritance
- The constructor of the parent class is called by default, and then the constructor of the subclass is called to instantiate so that if the parent class and the subclass constructor contain output statements, they will be printed at new
- In fact, super () is implied in the subclass construct, and the parent class construct is called
- Override of Method ("method overrides")
- The subclass override method cannot have more restrictive access than the parent class method
(such as: Parent class--public, subclass must be--public)
- Parent method How to access the parent class method after the quilt class method is overwritten?
```
- Use super (); keywords can be, such as: Super.print ();
- In subclasses, you can access methods in the parent class directly from the Super. Method () Form
```
- The exception:private--> to default is not to be written, but to redefine a new method!
- Redefine attributes after inheritance, consistent with overwrite methods
-
Special: In subclasses :
1.super.name-->访问父类中的name属性2.this.name-->访问“本“类中的name属性
- The difference between overloading and overwrite
-
Overload:
01.方法名相同参数类型或个数不同02.对权限没有要求03.发生在同一个类中
-
Overwrite:
01.方法名称,参数类型相同02.被覆写的方法(即子类方法)不能拥有(比父类方法)更严格的访问权限03.发生在继承类中
- The role of the Super keyword
-
Super calls the parent class construct
**同this一样,该语句必须放在构造方法的句首**
-
The difference between this and super:
This
01.属性:调用本类中的属性,若没有,则从父类中寻找02.方法:调用本类中的方法,若没有,则从父类中寻找03.构造:调用本领构造,必须放在构造方法的首行04.特殊:表示当前的对象
Super
01.属性:访问父类中的属性02.方法:直接访问父类中的方法03.构造:调用父类构造,必须放在子类的构造方法里的首行,如:super();
Attention:
1. 使用final声明的“类‘’不能有子类2. 使用final声明的”方法”不能被子类所覆写3. 使用final声明的变量即为常量,常量不可修改!4. public static final同时声明的变量称为全局”常“量
2. Abstract class
- A class that contains an abstract method must be an abstract class
- Both abstract and abstract methods use the abstract keyword declaration
- Abstract methods only need to be declared without implementing
- Abstract classes must be quilt-inherited, and subclasses (if not abstract classes) must overwrite all abstract methods in the abstract class
- Abstract classes cannot be instantiated directly, and subclasses must be instantiated through non-abstract subclasses
```
- Abstract class: Abstracted class A {}
- Abstract method: Access Permissions Abstract return value type method name (parameter);
There is no method body in an abstract method, and you do not need {}
- Subclass Definition Method: Class B extends a{}
The subclass must also overwrite all abstract methods in the abstract class
- Note: Abstract methods should not be declared with the private keyword, because abstract methods must be overridden by the quilt class
-
Essence: Abstract analogy common class more abstract method, in addition to the object can not be directly instantiated operation is actually not different
```
3. Interface
- One of the most important concepts
-
consists of global constants and common abstract methods
interface 接口名称 {全局常量;抽象方法;}
- The abstract method here must be defined as public access rights, cannot be changed!!!
- Of course, you can not write, default public,
-
Generally, we know that the global constants are public static final, the abstract method is public abstract, but here special, default so can omit the definition, actually there are
interface A{ String s="作者"; //定义全局常量 void print(); //定义抽象方法 String getInfo(); //抽象方法不需{}}
- Class subclass name implements interface A, interface b{} !!!
A subclass can implement multiple interfaces,Class A implements b,c{}
- After implementation, all abstract methods must be covered
- Inheritance of interfaces
-
An interface cannot inherit an abstract class, but an interface allows multiple interfaces to be inherited
interface A{} interface B{} interface C extends B,C{}
- That is, an interface can inherit multiple interfaces at the same time, note that it inherits extends!!!
-
Interfaces cannot implement interfaces and can implement inheritance
4. The polymorphism of the object
- Overloading and overriding of methods
- The polymorphism of the object
01. Transition Up: Child Object----Parent class object, automatically completed
02. Transition Down: Parent Object--child class object, must specify the subclass type of transformation
-
Object Transformation: Parent Class object = Subclass instance;
如:B b =new B(); //子类实例化过程 A a =b; //向上转型
-
Object down: Subclass sub-Class object = (subclass) Parent class instance;
如:A a =new B(); //向上转型B b =(B)a; //向下转型:A-->B
- Object instanceof Class--Returns a Boolean value
-
As follows:
A a1=new B(); A a2=new A();
01.A1 objects that are both A and B--objects instantiated by subclasses are also instances of the parent class and subclass, so you can transform directly up or down
02.A2 is just the object of a and cannot be turned down at this time because it is no longer a subclass instance, so it cannot be transformed without first being transformed--using the parent class to instantiate the class object.
-
Make judgments before you move down to avoid anomalies
```
A a=new B ();
if (a instanceof B) {}
Java Learning Note (iii): Object-oriented