Note 19 summarizes the object-oriented inheritance

1. When inheritance?
When a plurality of classes having the same properties whatever means, we have the same properties and methods extracted, in the parent class, and then other classes inherit

2. satisfied: the same type (is a)

3.extends parent
single inheritance Java: there is only one direct parent
when our class does not inherit from any class, then the class will inherit Object
Object class is the base class for all types of
equals (): In the Object, in fact ==
string:
the equals (): value for comparing two strings of
all types of references:
Comparative == address
comparison values: the equals
the hashCode (): this method can be passed to obtain the memory address of the object (not): processed memory address
toString (): our object into a string

Requirements: When we write entity class, these three methods I hope we can rewrite the
finalize (): can be rewritten, when the system call gc (garbage collection) calls this method automatically
clone (): Learn
getClass (): Get your own class files, for reflecting

4. Implement subclass
4.1 Constructor
a. To complete creating (Super ([...])) parent class object
b. And then create their own
4.2 subclass own peculiar attributes and methods
4.3 subclass does not satisfy the parent class provides details of the method, a method to rewrite the parent class (toString)

5. When some parent class method can not be achieved, then these methods should be declared as abstract methods, but also the current class as an abstract class inherited by subclasses
when the subclass inherits the abstract parent class, you must override abstract methods, if you can not rewrite , then the subclass will exist as an abstract class
abstract class can not new target

  1. Construction of a change in the way the object
    reference = current class constructor new () class object;
    parent class object reference = new subclass constructor (); // call Richter law: the ability to use local parent class, then you can use subclasses

7. Type Conversion
class object reference to the new = current class constructor ();
Parent Class: major types (range) more abstract
subclasses: type and more particularly smaller
compile type: writing code, operating a compiler (see object reference type),
run time: when the code is run up a way of calling the bottom (see the actual object type)
upcast:
downcast:

8. With regard to inheritance and abstract content
8.1 virtual methods vs abstract method
what is virtual parent class overridden by subclasses off?
Usual way: the subclass does not eat sleep
virtual methods: in the parent class is normal, but subclasses rewritten, and this sub-class the parent class does not make sense
toString, study

 抽象方法: 在父类是不能实现的,子类一定要重写
 
 Q: 虚方法            vs     抽象方法
 相同点:都是在父类中声明的方法
 不同点:
    1.已经实现               未实现
    2.不会要求子类的行为     规定必须要做

8.2 If my class does not want to be inherited final?
Statement Math class
public final class Mathextends Object
statement String class
public final class Stringextends Objectimplements Serializable, the Comparable, CharSequence
final + class: represents the current class is not allowed to inherit: sealed class
summary final
1. constant
a.final a float the PI = 3.14f;
Final value types, that they can not change the value
PI = 3.1415f; // error

 b.final Student stu = new Student();
   final 用于引用类型 ,表示不能改地址
   stu.setAge(20);//改值是可以的
   stu  = new Student("俊俊",...);//error

2. The sealed class
public class Final {XXX}
3. The method of sealing
public final xxx xxx () {}

8.3 Method not want to be rewritten? Final

8.4 can abstract method is final, static

9. Projects:
category: no problem
calling: Today we have to solve

Published 19 original articles · won praise 0 · Views 112

Guess you like

Origin blog.csdn.net/qq_45212924/article/details/105015762