Advantages and disadvantages inherited in Java programming

Source: Internet
Author: User
Document directory
  • 1. code reuse
  • 2. Code Reliability
  • 3. Other benefits
  • 1. Problems arising from multi-level inheritance
  • 2. Other limitations

Abstract: In Java, correct application inheritance can achieve code reuse, enhance reliability, simplify program design, improve programming efficiency, and make it easy to maintain. However, it is not advisable to use inheritance too much in a program, which brings some limitations. This article analyzes the advantages and disadvantages of inheritance.

  Key words: inheritance; superclass; subclass; code reuse

Inheritance is inherent in Java object-oriented programming. All classes, whether written by APIs or programmers themselves, are automatically inherited from the object class, the ancestor of all classes in Java, representing the commonality of all classes.

1. Inheritance Overview

Inheritance is one of the characteristics of object-oriented design. Like inheritance in the real world, inheritance means code reuse. Inherit and expand existing or compiled classes. For example, the API class solves specific problems in applications. Inheritance indicates the existence of two or more classes in programming.Link-- Definition, function, andLogic. It is called the inheritance basis, and the class that covers the subclass is a superclass. The class derived or inherited from the superclass is called a subclass.

Ii. Benefits of Inheritance

Correct Application inheritanceTechnologyTo achieve code reuse, enhance reliability, simplify program design, improve programming efficiency, and make it easy to maintain.

1. code reuse

Code reusability refers to the code that exists in a super class in a class with an inheritance relationship, including member variables and methods, which can be inherited into sub-classes. Child classes can use the code just like their own "property" or attributes. In addition, subclass can also add new variables and methods, and overwrite the superclasses to change their behavior to make them more powerful and solve more specific problems. In inheritance, a subclass can have one or more superclasses to form an inheritance chain. The superclass next to the subclass is called the direct superclass or parent class. In turn, the subclass of the superclass is called the direct subclass. Go up along this inheritance chain, and all these classes are subclass‑class superclasses, indirect superclasses, or ancestor classes. Of course, the top is the ancestor of all classes-object. In inheritance, a subclass inherits all the variables and methods of the parent class and ancestor class on the inheritance chain, plus the newly added variables and methods. Subclass is a relative term in inheritance. A subclass is probably a superclass inherited from the next level. From top to bottom in the inheritance chain is a process from abstraction to concrete, from common to characteristic. A superclass represents the variables and Methods common to all its child classes. Child classes inherit these attributes and may add variables and methods that represent their own features. This ensures the inheritance efficiency and improves the reusability of the Code.

2. Code Reliability

This feature of code reuse actually provides rare benefits-improving Code reliability. The full use of API classes can improve code reliability.LanguageDevelopers write, modify, improve, and improve the Code over the years. After a wide range of practical applications at different scales and levels, the reliability of the Code should be uncertain. Using APIs in inheritance is like standing on the shoulders of giants. In addition to code reuse, it also greatly improves the reliability of application software.

3. Other benefits

  • Simplified program design-the benefits of inheriting API classes are self-evident. For example, compile a window drawing software with buttons, menus, and mouse control. If it does not inherit APIs such as jframe, jmenu, and graphics provided by JDK, the programming difficulty can be imagined.
  • Improve programming efficiency-simplifying program design will inevitably increase efficiency. In the above example, we no longer have to worry about how to write the jframe code for the display window, nor how to control the mouse movement (mousemovelistener.
  • Enhancing program maintainability-inheritance embodies the essence of object-oriented programming: Modular Design and encapsulation. Updates and improvements to programs only involve related classes, rather than the entire program body. Correct use of inheritance does not produce a "Trigger and trigger" effect on code modifications. The encapsulated function can be used without having to know the details of the specific code, which greatly simplifies and reduces the maintenance objectives and scope of error correction. For example, we will focus on the inherited sub-classes for program maintenance, rather than the super-class API class.
Iii. Limitations of Inheritance

The biggest benefit of using inheritance is that it can achieve the greatest benefit of the Code. However, it is not advisable to use inheritance too much in a program, because it may cause difficulties for subclass changes.

1. Problems arising from multi-level inheritance

In software engineering theory, a software product must be highly cohesive and coupled. Cohesion refers to the degree of closeness between elements in a module, which can be understood as the degree of association between classes in the software. Coupling refers to the degree of association between several different modules in the software. It can be understood as the degree of mutual dependency between classes. The high cohesion and low coupling of a software product can be understood as follows: each class in this software product can independently complete a specific function, there is no excessive dependency between classes. A problem caused by multi-level inheritance is that software product coupling is enhanced.

Multi-level inheritance enhances the dependency between classes. The most obvious problem is that changes to top-level superclasses may lead to incorrect operations on all its related sub-classes. If there are too many inheritance layers and too frequent changes to the super class, it will bring a lot of trouble and problems to the modification of the underlying subclass. Especially in a large application, each software developer usually only completes the corresponding module. If there are too many inheritance layers, if the modification to the parent class causes problems with its subclass and related class functions, the labor and workload required for modification may be quite large.

2. Other limitations

  1. Java only supports single inheritance, but does not allow multiple inheritance. However, you can use interfaces to implement multiple inheritance.
  2. Once the object is instantiated, the role cannot be converted. For example, it is impossible for students to change to teachers.
  3. Data security issues. For example, if an object needs to access the inherited super-class data, it must give all objects access to the data.
  4. In multi-thread parallel processing, subclass objects in inheritance may cause inconsistency between threads.

These limitations undoubtedly put forward higher requirements for programming and coding.

Conclusion

Inheritance presents infinite opportunities for software development, and is accompanied by confusions and challenges. In Java programming, it is necessary to make good use of inheritance, a double-edged sword ".

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.