First, inheritance
1. Advantages of inheritance
Reusability of code
Subclasses can extend the properties and methods of the parent class
Designing your application makes it easier
Properties and methods of the parent class can be used for subclasses
2, how to determine the class has an inheritance relationship
Relationship of classes and classes
1) has-a (combination)
2) is-a (inheritance)
3. Single inheritance and multiple inheritance
Single-Sex:
In Java, a class can have only one parent class
Transitivity:
Subclasses can pass properties and methods of the parent class to their own subclasses
4. The root class of all classes is object (superclass, base class)
5. Method rewriting (used for later polymorphic use) @Override//annotations
1) The return value type, method name, and formal argument list of the method of the subclass,
Must be the same as in the parent class
2) The access modifier for a method of a subclass cannot be more restrictive than the parent class
3) Exceptions overridden in subclasses cannot throw more exceptions than parent classes
6. Construction method and inheritance
Not like a member of a super class
Super parentheses call the constructor of the parent class and must also be the first sentence of the statement block
Note: By default, the constructor of a subclass invokes the parameterless constructor of the parent class.
When the parent class does not have a parameterless construction method, and the subclass has no parameter construction method,
You need to indicate what construction method is called.
7. Super keyword
super.*** and Super (* * *);
8. Final keyword
Using the final decoration of a class, this class can no longer be inherited
Use final to modify a method, this method can no longer be rewritten
9. Abstract Abstraction
When you write a class, you typically define methods in your class that describe the behavior that the class has.
In the method of the class, we write the code
Note: Abstract classes cannot be instantiated.
Abstract classes and final cannot occur at the same time, because abstract classes need to be inherited
The abstract method is not a method body,
Abstract methods appear in abstract classes,
An abstract method must write out an entity in a subclass, that is, a method override, unless the subclass is also an abstract class
Significance:
Abstraction is needed when our class or method has no specific business meaning.
Add:
Public, protected modified methods and members can be inherited by the quilt class;
And the default to see whether in the same package, in the same package can inherit the quilt class;
Private cannot be inherited to.
Second, polymorphic
Polymorphic
1. Referencing instances of subclasses using the type of the parent class
/* Save 5 cars with an array: 3 Sedan 2 Bus */
Motovehicle[] vehicles = new MOTOVEHICLE[5];
vehicles [0] = new Car ("1", "BMW", "BMW 550i");
vehicles [1] = new Car ("2", "BMW", "BMW 550i");
vehicles [2] = new Car ("3", "BMW", "BMW 550i");
vehicles [3] = new Bus ("4", "BMW", "BMW 550i");
vehicles [4] = new Bus ("5", "BMW", "BMW 550i");
2, the implementation of polymorphic steps: (Dynamic polymorphism: Need to be in operation to know what method to call)
1) Writing parent class, derived subclass
2) overriding methods of the parent class in the subclass
3) Reference child class object with the data type of the parent class
4) Call the overridden method
Add: Static polymorphism is called method overloading
3. Supplement
Type conversion instanceof
We are using employee salary = new Salary ("", 1, "", 2,200000);
The Salary s2 = (salary) salary, but the downward conversion,
But we don't know if it's right after conversion.
System.out.println (Salary instanceof Employee); True
System.out.println (Salary instanceof salary); True
System.out.println (Salary instanceof Hourly); False
Third, the interface
1. Wording
Public interface class name ();
2, attention to problems
The compiled suffix is still. Java and can still be saved as. class;
Interfaces are not constructed and cannot be instantiated
You can omit public and abstract
Methods in an interface are abstract methods
Remember to write the notes well!
The member variables in the interface are public, static, constant
For example: public static final name = "ABC";
3. Implement the interface
Implements name of the interface after adding
The class that implements the interface must implement all the methods in the interface
A class can implement multiple interfaces (similar to multiple inheritance)
Implements and extends are is-a relationships.
An interface can declare attributes, which are public static final by default
Interface inheritance interfaces can be multi-inherited (an interface can inherit multiple interfaces)
4, the principle of using the interface
1) Use interface to resolve multiple inheritance
2) Add functionality to external classes using interfaces
3) Consider, in an object-oriented way, the inherent characteristics and behavior of a class and
dependent on external optional features and behavior separation, so that the class as simple as possible, that is, interface
5, the advantages of the interface
1) Detach the design and implementation, hiding the implementation from the external (caller) (and usually the caller does not need to be concerned about implementation)
2) interface-oriented programming is the core of OOP
6. Differences between interfaces and abstract classes
1) All the methods in the interface are abstract and all properties are public static final
2) An abstract class is a partial implementation of an interface that can define an abstract method,
Can also define properties of common methods, more flexible than interfaces
Add:
Get current time
Data date = new Date ();
Iv. Static member variables
Static Class Members
Using class members (class variables and class methods)
You do not need to create an instance, you can call it directly by: Classname.variablename (class name dot call)
1, static initialization block static {System.out.println ("Static code block"); }
2, non-static initialization block (member code block) {SYSTEM.OUT.PRINTLN ("non-static block of code"); }
Note: In the same class, both static and non-static code blocks are executed before the constructor.
When this class inherits from the parent class, the non-static code block executes after the parent class's construction method
That
Static code block
Parent class constructor
Non-static code block
Sub-class constructors
Static code blocks are shared by all members, so the creation of objects is performed only once.
Inner class
In Java, you can also define classes within a class.
This class, defined inside a class, is called an inner class.
The class that contains the inner class is called an external class
Classification of inner classes
Divided into member inner classes (written in class) and local inner classes (written in methods) by location
A special case of a member's inner class is a static inner class
A special case of a local inner class is an anonymous inner class
Note: Methods of inner classes can access external members, but static inner classes can only reference static members and methods
A member variable called by a local inner class is a final decorated
The instantiation of a static inner class is required to be instantiated with an external class class name.
Creation of members Inner classes: outter outter = new Outter (); Outter.inner1 = Outter.new Inner1 ();
Creation of a local inner class: Outter.show ();(We have initialized the object in the same method as the local inner class)
Anonymous inner class creation: Sometimes when a method of a local inner class is referenced to an interface,
And the interface is very small, we do not want to create a class for the interface, we need to use
Anonymous inner class:
Ditto
Outter.print (New Inter () {
@Override
public void Printflnface () {
SYSTEM.OUT.PIRNTLN ("Anonymous internal class printing");
}
});
Java inheritance, polymorphism and interfaces