——Package _
object
1 Everything is an object
2 Each object is unique
3 Objects have properties and behaviors
kind
A class is a collection of objects with the same properties and behavior. The characteristics possessed by an object are called properties of the class, and the behavior of the object is called the abstraction of the object of the class.
package
1 Property Privatization
2 Add getter and setter methods
3 In the method of assignment and value access, add access control to attributes
access modifier |
this class |
Other classes in the same package |
different types of buns |
Different package non-subclass |
Public |
Can |
Can |
Can |
Can |
Protected |
Can |
Can |
Can |
Can not |
default |
Can |
Can |
Can not |
Can not |
Private |
Can |
Can not |
Can not |
Can not |
The characteristics of overloading are :
1 occurs in the same class
2 methods have the same name
3 parameter lists are different
The difference in the parameter list can be: the number is different, the order is different, and the type is different.
Method overloading specific specifications:
1 The method name must be the same.
2 The parameter list of the method must be different, including the type or number of parameters
(1) If the number of parameters is different, it does not matter its parameter type
(2) If the number of parameters is the same, then the type of parameters or the order of parameters must be different
3 The return type and modifier of the method can be the same or different
what is a constructor
The constructor constucory is used to instantiate an object of a class. The constructor has the following characteristics:
1. The constructor is a special method. Each class in Ja has a constructor, which is used to initialize an object of the class.
2. The constructor method name is the same as the class name, and there is no return type.
3. The constructor can be overloaded.
4. The channel construction method can only be called by the new key word and this(), this() can only be used in the construction method to call other construction methods, and can only be used as the first statement of the construction force method code body
Encapsulation is to hide things that you don’t want or shouldn’t tell others, and expose what you can tell others. "Hide the implementation details of the properties and methods of objects, and only expose the external interface."
The specific method of encapsulation is to privatize attributes, provide public methods to access these attributes, and implement control over attributes in these public methods
If multiple methods with the same name are defined in a class, and they either have different parameter numbers or different parameter types, it is called method overloading.
Constructors can be overloaded
——Inheritance _
inherit
Find subclasses with common characteristics and behaviors
transitivity of inheritance
Inheritance needs to conform to the relationship
A subclass has the properties and behaviors of the parent class, as well as its own special properties and behaviors
Inherited Implementation Details
Extends keyword
Method override (override)
Super keyword
Protected access modifier
Inheritance relationships are transitive
1 In addition to calling the methods of the parent class, the subclass can also call the methods of the parent class of the parent class, that is to say, inheritance can ensure that all classes under a parent type will have all the methods held by the parent type.
2 Single Inheritance Inheritance Java does not support township inheritance, because multiple inheritance will have a problem called "deadly block".
3 Final class Java also has a keyword final, the class modified with it cannot be inherited, called final class. Tool classes that do not want to be inherited by their objects are generally made into final classes. The Math class we have learned before is the final class.
The relationship between the subclass constructor and the parent class constructor
1. The subclass constructor explicitly calls the parent class constructor
2. The subclass constructor implicitly calls the parent class constructor
meaning of inheritance
1 Avoid code redundancy and improve code reusability and maintainability
2 The properties and methods of the parent class are available to the child class
3 Subclasses can be easily customized and designing applications becomes simpler
Summarize
Inheritance can save us a lot of time in coding. In the concept of inheritance, the parent class is the most basic class, which is equivalent to a template. The parent class must be generalized, so as to facilitate class expansion.
Subclasses can inherit the properties and methods of the parent class, and can also extend their own special properties and methods.
In the inheritance relationship, the parent class and the child class need to satisfy the is-a relationship.
Inheritance prevents duplication of program code in subclasses, that is, code reuse.
In the inheritance relationship, if the subclass has a method with the same name, the same parameter list and the same return type as the superclass, then the method we call the subclass overrides the superclass method.
The super keyword can be used to access the properties, methods, and constructors of the parent class. When accessing the parent class constructor in the subclass constructor, the super key must be located in the first sentence of the subclass constructor.
Inheritance relationship is transitive
subclasses can not only call the methods of the parent class, but also call the methods of the parent class of the parent class, that is to say, inheritance can ensure that all classes under a parent type will have the methods held by the parent type. All methods.
Java has single inheritance, a class cannot inherit from more than one parent class at the same time.
A class modified by the final keyword is called a final class, and a final class cannot be inherited.
If the parent class only provides a constructor with parameters and does not provide a default no-parameter constructor, the subclass constructor must explicitly call the parent class constructor with parameters.
If the super keyword is not explicitly used in the subclass constructor to explicitly indicate which constructor of the parent class is called, the subclass constructor will implicitly call the default no-argument constructor of the parent class. In this case, the parent class must have a default no-argument constructor.
——Polymorphism
Necessary conditions for implementing polymorphism in java
to have inheritance
to rewrite
Parent class reference points to child class object
All abstract methods must be implemented
The abstract method of the parent class has no content, it exists only to mark polymorphism. This means that subclasses must implement all abstract methods.
The compiler won't let you instantiate abstract classes
public class Test { public static void main (String[] args){ Animal animal ; animal = new Cat2("Xiao Bai) ; animal.eat () ; animal= new Animal ("Xiaobai) ; }
Polymorphism is to have multiple manifestations. Theoretically, it is related to different subclasses generated by inheritance, and their objects will respond differently to the same message (method call).
Using polyphony can greatly enhance the maintainability and extensibility of programs.
A class modified with the abstract keyword is called an abstract class, and an abstract class cannot be instantiated.
A class that inherits from an abstract class implements all abstract methods.
A method modified by the abstract keyword is called an abstract method. An abstract method has no method body and must be overridden by subclasses. Classes containing abstract methods must be declared as abstract classes, but abstract classes may not all be abstract methods, and abstract classes can have concrete implementation methods.
Difference between abstract class and interface
A class with the abstract modifier is an abstract class. An abstract class cannot be instantiated. The methods in an abstract class do not have to be abstract. An abstract class must be inherited.
An interface is a special abstract class. The methods in the interface are abstract methods, and the interface cannot have a constructor. The interface can only be implemented by a class, and cannot be inherited. Implementing an interface means overriding all abstract methods in the interface.
A class can implement multiple interfaces, but can only inherit from one class.