All development teams are working on a problem by reusing code that has been tested and validated, and how to reduce development efforts. A proven approach is to optimize application development by effectively using Java inheritance. Inherited in a sense, the ephemeral beauty of inheritance is as if everything in the universe has a special relationship with everything else.
Java InheritanceJava inheritance is one of the most prominent features of object-oriented. Inheritance is the derivation of new classes from existing classes, which can absorb data properties and behaviors of existing classes, and can extend new capabilities.
Java inheritance is the technique of building a new class using the definition of an existing class, and the definition of a new class can either add new data or new functionality, or use the functionality of the parent class, but not selectively inherit the parent class. This technique makes it easy to reuse the previous code, greatly shortening the development cycle and reducing development costs. For example, you can separate Mr. Foo to define a class called a car, the car has the following properties: Body size, color, steering wheel, tires, and by car this class derives two classes of cars and trucks, adds a small trunk to the car, and adds a large container for the truck. the inheritance relationship between classes and classes can be expressed in UML notation, where the parent class is also called a superclass or base class , and a subclass is also called a derived class . The parent class is the generalization of the subclass, which is the specialization (materialization) of the parent class.
Java does not support multiple inheritance , single inheritance makes Java inheritance simple, a class can have only one parent class, easy to manage the program, and a class can implement multiple interfaces, thereby overcoming the disadvantages of single inheritance. in object-oriented programming , the principle of inheritance is that in each general-special structure formed by general class and special class, the properties and operations common to the object instances of the general class and all the object instances of all special classes are explicitly defined in the general class at once. In a special class no longer repeats the definition of something that is already defined in a generic class, but semantically, the special class automatically and implicitly owns the properties and operations defined in its generic class (and all the higher-level generic classes). objects of a particular class have all or part of the properties and methods of their generic classes, called special classes that inherit from generic classes. inheritance is expressed as an intersection between object classes, which enables a class of objects to inherit data members and member methods of another class of objects. If Class B inherits from Class A, then the object belonging to B has all or part of the nature of Class A (data attributes) and functions (operations), we call the inherited Class A as a base class , a parent class, or a superclass , whereas inheriting Class B is a derived class or subclass of a. inheritance avoids repeating descriptions of common features between general classes and special classes. at the same time, inheritance can clearly express the conceptual scope in which each common feature is adapted--attributes and operations defined in a generic class are adapted to the class itself and to all objects of each layer of special classes below it. The application of the principle of inheritance makes the system model more concise and more clear. Java Inheritance Classificationinheritance is divided into single inheritance and multiple inheritance. Single inheritance means that a subclass can have at most one parent class. Multiple inheritance is a subclass that can have more than two parent classes. as multiple inheritance brings ambiguity , single inheritance should be used as much as possible in practical applications. Classes in the Java language only support single inheritance, while interfaces support multiple inheritance. The multi-inheritance feature in Java is implemented indirectly through interfaces (interface) .
features of Java inheritance(1) The inheritance relationship is transitive. If Class C Inherits Class B and Class B inherits Class A (multiple inheritance), then Class C has properties and methods inherited from Class B, as well as properties and methods inherited from Class A, and can have its own newly defined properties and methods. inherited properties and methods, although implicit, are still properties and methods of Class C. Inheritance is the most effective means of constructing, building and expanding new classes on the basis of some more general classes. (2) Inheritance simplifies people's understanding and description of things, and can clearly reflect the hierarchical relationship among related classes. (3) inheritance provides the software reuse function. If Class B inherits from Class A, then building Class B requires only a few more features ( data members and member methods) that are different from the base class (Class A). This method can reduce the redundancy of code and data, and greatly increase the reusability of the program. (4) Inheritance by enhancing the consistency to reduce the interface between the modules and interface, greatly increasing the ease of maintenance of the program. (5) Provide multiple inheritance mechanisms. Theoretically, a class can be a special class of more than one generic class, and it can inherit properties and methods from multiple generic classes, which is multiple inheritance. for security and reliability reasons, Java supports single-inheritance only, and multiple inheritance by using an interface mechanism. Instances of Java Inheritance
//build a Class Aclassa{//member variables where num is an argumentintnum=0;//member method, where I is the type parameter PublicAinti) {//input int type file output belowSystem. out. println ("aaaaaaaaaaaaaaaaaaa"); System. out. println ("i="+i);//assign the input I to the member variable numnum=i;}}//b inherit aclassB extends a{intnum=0;//member Method B. PublicB () {//the method that inherits Class A. Because B inherits a, a must inherit the characteristics of a. So the input int value 10 is implemented by the method.SuperTen); System. out. println ("bbbbbbbbbbbbbbbbbbbb");//num Here is Class B.System. out. println ("num="+num);//If you want to display num for Class A, you need to use the following super.num. Note here that NUM needs to be a member variable in Class A .System. out. println ("super.num="+super.num);}}//build test Class C Public classc{//Main Public Static voidMain (String aa[]) {//new An object B ()NewB ();}}Benefits of Java Inheritance Java inherits in a sense, the ephemeral beauty ofJava Inheritance is like the existence of a special relationship between everything in the universe and everything else. educators try to explain the concept of succession to laymen, often explaining the concept by discussing the relationship between the relevant animal groups. in Java and NET , in a way, everything is a software component of an object type. However, starting from there, the development of choosing what to associate in subclasses and superclass inheritance is the focus of the design.The problem with using Java for desktop rendering is that too much thought is put into grouping visual artifacts together. from the parent-child relationship, all the well-known graphic elements, although well-known methods are different, but all with the general Window class has a great relationship. All of the things we choose that inherit from the window class, related to the JDialog attribute, are related to the JFrame of the previous level because they have a common parent window class. Indeed, writing reusable code to manage many of the related sub-objects is a hallmark of good design. Like the dog and the window above, the great polymorphic design is about abstraction, classification, design, and inheritance.
Inheritance of three major features of Java object-oriented