1 2 subclass and superclass
3 extends keyword to declare a class is a subclass of another class
4 as: class subclass name extends superclass name
5 if there is no use extends keyword, the default is a subclass of the Object class java.lang package in
6 data member to store all the parent class in the class, subclass data members include not accessible (there may be a method used)
. 7 . 8 inheritance subclasses: Inherited member variable and methods access remains unchanged
9 if the parent class and subclass in the same package
10 subclass inherits the parent class is not private: the member variables and member methods
11 if the parent class and subclass in a different package
12 subclass inherits the parent class protected , public member variables and member methods
13 subclass can not inherit variables friendship and friendly parent class method
14 15 protected Description:
16 protected
: Variables and methods declared only in this package and subclasses access, the other access error
. 17 protected class variables and methods may be class A direct and indirect subclass inherits a subclass of A;
18 if the subclass B have declared a protected variables and methods:
19 if: the protected variables and methods in order to access other class D sub-type B, just the class D and class B in the same package
20 if: proteced variables and methods of the parent class of other classes D wants to access a subclass of B, only the class D and class B parent class in the same package can
21 is the source dates back to the parent class a class
22
23 subclass object constructor
24 constructor sub-class is created when the object: constructor of a subclass of default will first call the no-argument constructor of the parent class
25
26 hidden member variables:
27 variables subclass declared and parent variables declared in the same name: the subclass hides the inherited from the parent class member variable
28 using the name of the new variables declared in the subclass
29 rewrite method:
30 subclass can override the hidden inherited from the parent class instance method:
31 If you want to use a subclass of hidden variables or method would be to use the super keyword: super variable |. The method
32
33
34 subclass constructor:
35 super (); if no write super (); defaults have super (); the default call the parent class's constructor with no arguments
36 may also be used Super (name ...); a plurality of like parent class constructor
37 , such as the parent class name attribute
38 is . 1 : name = null ;
39 2 : call subclass constructors:
40 . 3 : subclass constructor calls automatically write Super ( " Tom " method, ..)
41 is . 4 : this.name superclass = name of the custom value becomes Tom
42 is . 5 : sub then returns class, continue to initialize other methods
43
44 Final keywords:
45 Final keywords can be modified: class, member variables, local variables, methods
46 is Final Dress Class: This class can not be inherited, i.e., can not have subclasses -> Final class A
47 Final modification method: This method can not be overridden by subclasses
48 Final modifications members and local variables: this variable is constant, not changed and it must be given a default value when statement
49
50
51 is the object of the transformation of the object
52 the parent class is a class a class B
53 when creating an object b of class B sub-time (ie: memory space has been allocated, subclass deposit with the object reference)
54 the reference object b into a parent object, then the object referred to as a transformation of the object on the object b
55 a a = new new B ();
56 is the transformation of the object on the sub-entities will lose class object attributes of an entity :( create some space in memory still exists, but can not access)
57 on the transformation of the object can not operate a new sub-class member variables b,
58 transformation of the object can not be called on a member of the new subclass b
59 members of a variable transformation of the object can be accessed on the b subclass inherits a parent or hidden: operating properties are members of the parent class of
60 A method of transformation of the object can be called a subclass inherits a b: the operations of the parent class is inherited
61 if the child class overrides the parent class, then the transition operation of the object is a subclass of this method overridden
62 the transition can cast the object to a subclass object b, this time with a subclass object includes all the features of subclasses
63 Note: objects can not be transferred to the parent class creates a subclass
64
65
66 polymorphism: a parent class when a method is overridden subclasses thereof, may each have different functions
67 polymorphic into: static polymorphism, dynamic polymorphism
68 static polymorphism: at compile time, determine which piece of code can be executed (and related overload)
69 dynamic polymorphism: at runtime, in order to determine which piece of code (and override the parent class and method object related to the transition) referenced
70 before is not running, there may be a plurality of method calls, which can not be determined calling
71 is
72 abstract class and method
73 is abstract class called modified ( abstract ) abstract
74 public abstract class a
75 An abstract class can have: a non-abstract methods and abstract methods
76 abstract class can not create an object using the new operator
77 (referred to as a modified method using abstract abstract ) abstract methods: public abstract void Speak ();
78 abstract method declarations only does not allow to achieve, and the final abstract and can not be modified with a method
79 if a non-abstract class B an abstract class is a subclass of a, then all the non-abstract class B abstract parent class must override the abstract class a, the method gives the body
80 if an abstract class B abstract class is a subclass of a, then B may be the abstract class abstract parent class abstract class a rewriting; can inherit, without rewriting
81 abstract class: only concerned with the operation : i.e. the method name, type, and parameters, the method does not care about the body
82
83 abstract polymorphic
84 abstract object class declaration, as a subclass of the object on the transformation of b, b subclass can be called rewritable
85 a a = new new B () | new new C () | new new D ();// the transformation of objects, methods, different subclass can call, but implementation is the same method name
86 A method Speak ();.
87 when programming: abstract class declaration by several abstract methods, the method body by its non abstract class to achieve
88 first modification of abstract classes were closed;
89 otherwise, if the modified abstract class, so all subclasses will again make changes
90 after good programming, then subclass open to the abstract
91
92 Interface:
93 java does not support multiple inheritance: a class can have only one parent class
94 but a class can implement multiple interfaces
95 interface declaration: interace interface declaration:
96 interface name and the interface -> interface a {...}
97 the interface body : contains the constant definitions and method definitions
98 constants: all constant access interface Final must be public;
99 and constants immutablepublic Final int cnt = 0 ; // public, can be omitted Final
100 constant through the interface with the name calling | calling the class name that implements the interface or class ---> Interface name .Max | A.Max
101 Method: Interface only abstract method, there is no common method
102 and access a public abstract ; may be omitted public, abstract
103 a class can declare itself implement one or more interfaces (with spaced interface name) by use of the keyword the implements
104 public class a interfaces the implements 1, the interface 2, ...} {
105 if a class implements an interface, then this class must override this method for all the interface: public must add
106 overridden methods modified to remove the abstract: namely: public The return type method name (parameter) {}
107 If a class is an abstract class A implements an interface, you can temporarily without rewriting the abstract methods of the interface
108 , but if the child is not an abstract class abstract class A, then have to rewrite all of the abstract methods
109
110 Interface callback: corresponds to the transformation of the object, which is very similar to the two
111 interfaces callback: refers to the class of the object referenced B creates an implementation of an interface B ( new new B ()) assigned to a variable declared in the interface a (a a;)
112 interface variables can then call a method of the interface class is overridden B
113 similarly interfaces not new, because no achieved, i.e. without allocating memory
114 polymorphism is achieved by the interface: different classes implement the same interface, may with different implementations,
115 then interfaces variable callback interface method, it is possible to have multiple polymorphic
116 oriented programming includes two abstract: abstract oriented programming and oriented programming interface