Inheritance of the three main features of java object-oriented, and java object-oriented

Source: Internet
Author: User

Inheritance of the three main features of java object-oriented, and java object-oriented
By reusing the code that has been tested and verified, all development teams are working hard to solve the problem. A tested method is to effectively use Java inheritance to optimize application development. In a sense, the temporary beauty of inheritance is like a special relationship between things in the universe and other things.
Java inheritance is the most significant feature of object-oriented. Inheritance is to derive a new class from an existing class. The new class can absorb the data attributes and behaviors of the existing class and expand new capabilities.
Java inheritance uses existing class definitions as the basis to create new class technologies. New class definitions can add new data or functions, or use the features of parent classes, however, the parent class cannot be inherited selectively. This technology makes it easy to reuse previous code, greatly shortening the development cycle and reducing development costs. For example, you can use a separator to define a car type. The car type has the following attributes: car body size, color, steering wheel, and tires. The car type derives from the car type and truck type, add a small trunk for the car and a large cargo bin for the truck. The inheritance relationship between classes can be expressed by UML symbols. The parent class is also called a super class or a base class, And the subclass is also called a derived class. The parent class is the generalization of the Child class, and the Child class is the special (concrete) of the parent class ).
JAVA does not support multi-inheritance. Single inheritance makes JAVA's inheritance relationship simple. A class can only have one parent class, which is easy to manage programs. At the same time, a class can implement multiple interfaces, to overcome the disadvantages of single inheritance. In object-oriented programming, the inheritance principle is applied to every general-special structure formed by general classes and special classes, explicitly defines the attributes and operations that are common to the object instances of the general class and all object instances of the special class in the general class at one time, in special classes, we do not repeatedly define what is already defined in a general class, but in terms of semantics, A special class automatically and implicitly owns the attributes and operations defined in its general class (and all upper-level general classes. Objects of special classes have all or part of the attributes and methods of their general classes. They are called special classes that inherit general classes. Inheritance represents the intersection between object classes. It allows an object to inherit the data member and member methods of another object. If Class B inherits Class A, the object belonging to Class B has all or part of Class A (data attributes) and functions (operations ), we call the inherited Class A as the base class, parent class, or superclass, And the inherited Class B as the derived class or subclass of Class. Inheritance avoids repeated descriptions of common features between general and special classes. At the same time, through inheritance, you can clearly express the scope of concepts that each common feature adapts to-attributes and operations defined in a general class are adapted to the class itself and all of the special classes below it object. The inheritance principle makes the system model concise and clear. Java inheritance class inheritance is divided into single inheritance and multi-inheritance. Single inheritance means that a subclass can have at most one parent class. Multi-inheritance is a subclass that can have more than two parent classes. Because multi-inheritance brings about ambiguity, we should try to use single inheritance in practical applications. Classes in Java only support single inheritance, while interfaces support multiple inheritance. Multi-inherited functions in Java are indirectly implemented through interfaces.Features inherited by Java(1) The inheritance relationship is passed. If Class C inherits class B and class B inherits Class A (Multi-inheritance), Class C has the attributes and methods inherited from Class B, there are also attributes and methods inherited from Class A, and new attributes and methods. Although the inherited attributes and methods are implicit, they are still attributes and methods of class C. Inheritance is the most effective way to construct, establish, and expand new classes on the basis of some common classes. (2) Inheritance simplifies people's understanding and description of things, and clearly reflects the hierarchical relationship between classes. (3) Inheritance provides software reuse functions. If Class B inherits Class A, you only need to describe A small number of features (data member and member methods) different from the base class (Class A) when creating Class B. This method can reduce code and data redundancy and greatly increase the reusability of the program. (4) Inheritance reduces inter-module interfaces and interfaces by enhancing consistency, greatly increasing the maintainability of the program. (5) multiple inheritance mechanisms are provided. Theoretically, a class can be a special class of multiple general classes. It can inherit attributes and methods from multiple general classes. This is multi-inheritance. For the sake of security and reliability, Java only supports single-re-inheritance, and implements Multi-inheritance by using the interface mechanism. Java-inherited instances

// Create A class A {// member variable where num is the real parameter int num = 0; // member method, where I is the type parameter public A (int I) {// input the int type file and output the following System. out. println ("aaaaaaaaaaaaaaaaaaa"); System. out. println ("I =" + I); // assign the input I value to the member variable numnum = I ;}// B inherits Aclass B extends A {int num = 0; // member method B. public B () {// inherit the method of Class. because B inherits the features of. therefore, input the int value 10 to implement this method. super (10); System. out. println ("bbbbbbbbbbbbbbbbbbbbbb"); // The num here is Class B. system. out. println ("num =" + num); // if you want to display the num of Class A, use the following super. num. note that num must be A member variable in Class. system. out. println ("super. num = "+ super. num) ;}/// create a test class Cpublic class C {// mainpublic static void main (String aa []) {// new an object B () new B ();}}
JAVA inheritance advantages Java inheritance in a sense, the short beauty of Java inheritance is like there is a special relationship between things in the universe and other things. Educators try to explain the concept of inheritance to outsiders. They often discuss the relationship between animal groups to explain this concept. In Java and NET, to some extent, everything is a software component of the object type. However, from there, it is the focus of the design to select what to associate in subclass and super class inheritance. The problem with using Java for desktop rendering is that too many ideas are invested in grouping together visual projects. From the perspective of parent-child relationship, although the well-known graphic elements in all regions are different from the well-known methods, they all have a great relationship with the general Window class. All the items we select that inherit from the Window class and related to the JDialog attribute are related to the JFrame of the upper level, because they have a common parent Window class. Indeed, writing reusable code to manage many related sub-objects is a sign of good design. As the dogs and Windows mentioned above, great polymorphism designs are about abstraction, classification, design, and inheritance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.