javaOO-- inheritance

Inheritance
subclasses:
 1. extends keyword, you can directly owned properties and behavior from a parent class. (But you can not receive access to restrict access modifiers - make it clear: with and can not directly operate two different concepts when to)
 2, the parent class attributes and behavior of the subclass automatically inherits, but a subclass can not inherit the parent class constructor of
 Reason 1: The method name must match the name of the class constructor syntax requirements. If the child class inherits the parent class constructor, then there will be a sub-class called the parent class constructor name. This syntax conflicts with the constructor.
 Reason 2: The effect produced when the object constructor. Parent class constructor parent class object is generated. Constructor subclass generate subclass object.
 3, although the parent class constructor is not inherited by subclasses, but in the process of constructing the subclass object, will first generate in memory the parent object portion, and then superimposed subclass-specific portions, so as to construct a complete sub-class objects.
 --- that is to say when a new sub-class object, it will first call the parent class constructor, and then call the subclass constructor.
 --- "memory overlay" approach
 
constructor calls the parent class - super ()
4, if the child and parent class defines exactly the same properties, who will then subclass object of the two properties, a portion in the parent object, in a subclass-specific portions.
 (1) Should such a design?
  - should not, because the design time has been considered a subclass of the total design time to consider the unique subclass.
  Then each property should be considered only once, does not belong to the parent class belongs to the subclass. So this design is unreasonable.
  (A problem when extracting common attributes and unique attributes.) Basically does not appear, but the interview is often asked.
 (2) that how to distinguish it?
  With super. Attribute name represents the properties defined in the parent class of the same name portion
  of the same name attribute with this. Attribute name represents the portion of the definition of a subclass of 
5, if the child class defines the methods and the father of it exactly? --- This "rewrite" is called the method
 it refers to all classes have common behavior, all we have to define in the parent class.
 However, different subclasses have different implementations, so it is necessary to re-write once in a subclass.
Rewrite request:
 method name must match
 the argument list must be identical
 return the same type or a subclass
 can not be overwritten narrow access: greater than or equal rights can
 not be reduced exception types (also redundant parent class exception type)
When Java single inheritance system practiced in
Java systems inherit transitive
Inheritance advantages:
 code reuse, properties and methods of the parent class is a subclass, design application easier, subclass can extend the properties and methods of the parent class, improve the development efficiency and reduce development costs.
The overriding method: subclass parent class method with the same method (method name, parameter list, return type)
method overloading: a method of the same class with another method, different parameter list.
Rewriting reflects the relationship between parent and child class methods; Overload reflect the relationship between the internal method of a class.
Compares two objects are equal
1, when comparing == always compare the right and left ends of the data values are equal. - if the left and right ends are reference data type, this value is a reference to the address pointed to by the variable object. So do use == comparison, the comparison reference data types are two variables refer to the same object.
2, when comparing using the equals method, the comparison is whether the two objects are equal in the content business, rather than a physical storage position (reference value)
 equals method is defined in advance inside the Object class, all the classes that both both methods should compare the contents of another.
 But the realization is only in Object Object itself, can not consider each specific subclass-specific implementation.
 --- So this method to be used by writers concrete subclasses of your business, complete rewrite.
toString () Returns a string description of an object
when an object is directly string operations, will be called directly
 , such as: a direct printing objects
with String () is also implemented in Object, therefore it can not be determined for each subclass What string description,
can only return to form unified "full class name @ hex reference".
If you want to have your own subclass to achieve, you need to have to rewrite
finalize ()
to achieve the destruction of the object, to clear space, resource recovery function.
Called by the garbage collection (GC), the completion of the stated characteristics of java programmers do not need to worry about memory cleanup.
Implement finalize () Object is implemented, in turn, call the GC calls. Therefore, this method is not designed to give us rewrite, it is to get behind the work.

Questions and Answers:
1. finalize the role of
2, final finally finalize the difference?
(1) final, finally, it is the keyword. finalize method
(2) final - meaning is "final immutable"
 final modification of variables, which becomes constant 
 final modification method: can not be rewritten, called "final method"
 final modification categories: it can not be inherited , i.e. not produce subclass, referred to as "the final class", "final class"

Guess you like

Origin www.cnblogs.com/libobo22/p/11613046.html