Java inheritance
The concept of inheritance
Inheritance is a cornerstone of Java object-oriented programming technology because it allows classes of hierarchical hierarchies to be created.
Inheritance is that the subclass inherits the characteristics and behavior of the parent class, so that the subclass has the various properties and methods of the parent class, or the subclass inherits the method from the parent class, so that the child class has the same behavior as the parent class.
Inherited attributes
1. Subclasses have properties, methods, and constructors that are not private to the parent class.
2. Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.
3. Subclasses can implement methods of the parent class in their own way.
The inheritance of 4.Java is single inheritance, but can be multiple inheritance, single inheritance is a subclass can inherit only one parent class, multiple inheritance is, for example, Class A inherits Class B, Class B inherits Class C, so the Class C is the parent class of Class B, and Class B is the parent class of Class A, which is different from C + + in Java inheritance. An attribute of inheritance.
5. Increase the coupling between classes (the disadvantage of inheritance, high coupling will cause the relationship between the code).
Java override (Override) vs. overload (overload)
Override (Override)
Overrides are subclasses that rewrite the implementation of the method that allows access to the parent class, and the return value and formal parameters cannot be changed. That is, the shell does not change, the core rewrite!
The benefit of overriding is that subclasses can define their own behavior as needed. That is, subclasses can implement methods of the parent class as needed.
The overriding method cannot throw a new check exception or a broader exception than the overridden method declaration. For example: A method of the parent class declares a check exception ioexception, but cannot throw Exception exception when overriding this method, because Exception is the parent of IOException, only the subclass exception of IOException can be thrown.
In object-oriented principles, rewriting means that any existing method can be rewritten.
overriding rules for methods
1. The parameter list must be exactly the same as the overridden method;
2. The return type must be exactly the same as the return type of the overridden method;
3. Access permissions cannot be lower than the overridden methods in the parent class. For example, if a method of a parent class is declared public, overriding the method in a subclass cannot be declared as protected.
4. The Member method of the parent class can only be overridden by its subclasses.
5. A method that is declared final cannot be overridden.
6. A method declared as static cannot be overridden, but can be declared again.
7. Subclasses and parent classes are in the same package, then subclasses can override all methods of the parent class except for the methods declared private and final.
8. Subclasses and parent classes are not in the same package, then subclasses can only override non-final methods that are declared public and protected by a parent class.
9. The overridden method can throw any non-mandatory exception, regardless of whether the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception, or a more extensive mandatory exception than the overridden method declaration, or vice versa.
10. The construction method cannot be overridden.
11. If you cannot inherit a method, you cannot override this method.
Overloading (overload)
Overloading (overloading) is a class in which the method name is the same and the parameters are different. The return type can be the same or different.
Each overloaded method (or constructor) must have a unique list of parameter types.
Constructors can only be overloaded
Overloading rules
1. The overloaded method must change the parameter list (the number of parameters or the type or order is different);
2. The overloaded method can change the return type;
3. The overloaded method can change the access modifier;
4. Overloaded methods can declare new or broader inspection anomalies;
5. Methods can be overloaded in the same class or in a subclass.
6. The return value type cannot be used as a distinguishing criterion for overloaded functions.
Overrides of methods (overriding) and overloads (overloading) are different manifestations of Java polymorphism, which is a representation of polymorphism between parent class and subclass, and overloading is a representation of polymorphism in a class.
Java polymorphism
Polymorphism is the ability of the same behavior to have many different manifestations or forms.
Polymorphism is the same interface that uses different instances to perform different operations.
How to implement Polymorphism
Way one: rewrite:
This content has been described in detail in the previous section and is no longer elaborated, with detailed accessibility: Java rewrite (override) and overloading (overload).
Mode two: interface
The most representative of the interface in life is the socket, such as a three connector plug can be connected to the three-hole socket, because this is each country has its own rules of the interface rules, it is possible to go abroad, it is because the foreign own definition of the interface type.
The interface in Java is similar to the interface in life, which is the collection of some method features, but there is no implementation of the method. Specifically, you can see the Java Interface section of the content.
Way three: Abstract classes and abstract methods
Java Abstract class
In object-oriented concepts, all objects are depicted by classes, but conversely, not all classes are used to depict objects, and if a class does not contain enough information to depict a specific object, such a class is an abstract class.
Abstract classes In addition to the object cannot be instantiated, other functions of the class still exist, member variables, member methods and construction methods are accessed in the same way as the normal class.
Because an abstract class cannot instantiate an object, an abstract class must be inherited in order to be used. For this reason, it is often decided at the design stage to not design abstract classes.
The parent class contains a common method for the collection of subclasses, but because the parent class itself is abstract, these methods cannot be used.
In Java, an abstract class represents an inheritance relationship in which a class can inherit only one abstract class, while a class may implement multiple interfaces.
Abstract methods
If you want to design a class that contains a special member method whose implementation is determined by its subclasses, you can declare the method as an abstract method in the parent class.
The abstract keyword can also be used to declare an abstraction method, which contains only one method name and no method body.
Abstract methods are not defined, followed by a semicolon, not a curly brace, directly following the method name.
Declaring an abstract method can result in the following two results:
1. If a class contains an abstract method, the class must be an abstract class.
2. Any subclass must override the abstract method of the parent class, or declare itself an abstract class.
Subclasses of inherited abstract methods must override this method. Otherwise, the subclass must also be declared as an abstract class. Eventually, a subclass must implement the abstract method, otherwise, from the original parent class to the final subclass, it cannot be used to instantiate the object.
Abstract class Summary
Abstract classes can not be instantiated (beginners are prone to make mistakes), if instantiated, will error, compile cannot pass. Only non-abstract subclasses of abstract classes can create objects.
Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes.
Abstract methods in abstract classes are only declarations, do not contain method bodies, that is, the concrete implementation of the method is the specific function of the method.
A constructor method, a class method (a method that is decorated with static), cannot be declared as an abstract method.
Subclasses of an abstract class must give a concrete implementation of an abstract method in an abstract class, unless the subclass is also an abstract class.
Java Package
In an object-oriented programming approach, encapsulation (English: encapsulation) refers to a method of wrapping and hiding the actual details of an abstract function interface.
Encapsulation can be thought of as a protective barrier against random access to code and data of that class by code defined by external classes.
To access the code and data for this class, you must pass strict interface control.
The main function of encapsulation is that we can modify our own implementation code without modifying the program fragments that call our code.
Proper encapsulation can make code easier to understand and maintain, and it also enhances code security.
Advantages of Encapsulation
Good encapsulation can reduce coupling.
The structure within the class can be freely modified.
You can have more precise control over member variables.
Hide information and implement details.
Java interface
Interface (English: Interface), which is an abstract type in the Java programming language, is a collection of abstract methods, and interfaces are usually declared with Interface. A class inherits the abstract method of an interface by inheriting the interface.
Interfaces are not classes, and the way they are written is similar to classes, but they are different concepts. Class describes the properties and methods of an object. The interface contains the methods to be implemented by the class.
Unless the class that implements the interface is an abstract class, the class defines all the methods in the interface.
The interface cannot be instantiated, but it can be implemented. A class that implements an interface must implement all the methods described in the interface, otherwise it must be declared as an abstract class. In addition, in Java, the interface type can be used to declare a variable, they can be a null pointer, or be bound to an object implemented in this interface.
Interfaces are similar to classes:
An interface can have multiple methods.
The interface file is saved in a file that ends in. java, and the filename uses the interface name.
The bytecode file for the interface is saved in a file that ends in a. class.
The corresponding bytecode file for the interface must be in the directory structure that matches the package name.
The difference between an interface and a class:
An interface cannot be used to instantiate an object.
Interface has no constructor method.
All methods in an interface must be abstract methods.
An interface cannot contain member variables, except static and final variables.
Interfaces are not inherited by classes, but are implemented by classes.
The interface supports multiple inheritance.
Interface Features
Each method in the interface is implicitly abstract, and the methods in the interface are implicitly specified as public abstract (only public abstract, and other modifiers will be error-marked).
The interface can contain variables, but the variables in the interface are implicitly specified as public static final variables (and can only be public, and a compilation error is reported with the private adornment.)
The method in the interface is not implemented in the interface, only the class implementing the interface can implement the method in the interface.
The difference between an abstract class and an interface
A method in an abstract class can have a method body, which is a specific function that can implement a method, but the method in an interface is not.
member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types.
There cannot be static blocks of code and static methods (methods with static adornments) in an interface, whereas abstract classes can have static code blocks and static methods.
A class can inherit only one abstract class, while a class may implement multiple interfaces.
Java Packages (Package)
To better organize classes, Java provides a package mechanism for distinguishing the namespace of a class name.
The role of the package
1, the function of similar or related classes or interfaces organized in the same package, convenient for the search and use of the class.
2, like the folder, the package also uses the tree directory storage. The class names in the same package are different, and the names of the classes in different packages can be the same, and the package name should be distinguished when calling two classes of the same class name in different packages. Therefore, packages can avoid name collisions.
3. The package also restricts access, and classes that have access to the package can access the classes in a package.
Java uses the package as a mechanism to prevent naming conflicts, access control, to provide search and locate classes (class), interfaces, enumerations (enumerations), and annotations (annotation), and so on.
Java Object-oriented