Java Fundamentals (ii)-----Inheritance of three major features of Java object-oriented

Source: Internet
Author: User

Java inherits the three main features of object-oriented:


reusing code is one of the many compelling features of Java, but to be a revolutionary language, it's not enough to simply copy and change the code. It must be able to do more. Quoted from theThink in Java and code reuse is a constant pursuit of programmers. In this case, the code is reused in one way Java object-oriented three major features--Inheritance!!
 In object-oriented programming, the inheritance mechanism can effectively organize the structure of classes, determine the relationship between classes, and develop new classes on the basis of existing classes. The inheritance mechanism plays an important role in the reuse of program code, improving the efficiency of software development and reducing the maintenance cost of software system .   in objective things, similar things have both generality and particularity. For example, cars, passenger ships, passenger aircraft are loaded, engine, steering, stop and other commonalities, but each specific thing has its particularity, such as the car has a front and rear gear. The passenger liner has the draught line and the tonnage, the airliner has the flight altitude, the cruising speed and so on. excerpt fromusing cases to learn Java easily。we can do it. Transport is defined as a class, called a generic class. It has a car, passenger ships and passenger aircraft have the engine, the number of manned, steering, stop and other commonalities. Also has its own properties and methods.   If Class B has all the properties and methods of Class A, and has its own unique properties and methods. Class A is referred to as the generic class, and Class B is called Special. the above means of transport are general, cars, passenger ferries, passenger aircraft for special classes.  The inheritance mechanism of object-oriented programming is defined in the general class, and the properties and operations common to the general class and the object instances of all special classes in the class structure formed by the general class and the special class are---. In special classes where properties and operations that are already defined in a generic class are not duplicated, the special class automatically owns the properties and operations defined in the generic class (and all the higher-level generic classes). objects of a particular class have all the properties and operations of an object of its generic class (unless restricted), known as the inheritance of a generic class to a special class .  * Note subclasses can inherit all non-private data members and methods from the parent class as their own members, that is, subclasses cannot directly access private members of the parent class (including data members and member methods) if access to private data members is required, is implemented by getter and setter methods. If you define a class without giving the extends keyword and the parent class name, the default class is the System class object.  If Class B inherits Class A, then Class B has all or part of the properties and methods of Class A. We will inherit the class A called the base class (superclass, parent class), inheriting Class B as a subclass of Class A (derived class). For example:class A{int x=12;//is inherited static double y=34;//is inherited final int xx=123;//is inherited, cannot modify the search value of XX, once initialized it is treated as constant using private float r=12.0f;//cannot be A ()//Cannot be inherited {//==== constructor method body} public void F () {}//is inherited protected void D () {}//inherited private void K () {}//cannot be inherited from final V OID Gg () {}//is inherited, cannot be overwritten void W () {}//is inherited//that is, the constructor and the keyword private adornment cannot be inherited}class B extends a{//========//=======//a class body }The inheritance mechanism can effectively avoid repeating the generality between general class and special class, and the concept scope of each common feature can be clearly expressed through inheritance.   method Overwrite:Methods the method of referring to the subclass is identical with the method of the parent class in the method name, the number of parameters and the parameter type.* Note Method override access control permission for a subclass method cannot be less than the access control permission of the method in the parent class. (such as the parent classWhen the access control permission is public, the subclass can only be public);method overloads:The method name is the same, but the number of parameters or parameter type is different;* Note the difference is that the method overwrite exists between the subclass and the parent class, and overloads exist between the methods inside the class inheritance has the following characteristics:(1) The inherited relationship is transitive.(2) Inheritance simplifies people's understanding and description of objective things, and can clearly reflect the hierarchical relationship between related classes. (3) inheritance provides the ability to reuse software(4) The inheritance reduces the interface and interface between modules, and gaining software is easy to maintain. (5) provides a multi-tier inheritance mechanism (but only single inheritance is allowed.) That is, a subclass can inherit only one parent class) But although inheritance has brought us many benefits, we should use inheritance with caution!! inheritance destroys encapsulation, and the parent's implementation details are transparent to the class carcass. when the parent class changes, the subclass must change, and the inheritance is also a strong coupling relationship. so we have to use inheritance with caution, but are we using inheritance? theThink in Java provides a workaround:when we have to transform from subclass to parent class. Inheritance is necessary, but if it is not needed, you should consider whether you need to inherit. I learn to summarize the use, if there are mistakes errors, please correct me, set an open-minded learning to correct! Thank you. do not find excuses for failure, only to find a way to success!!!

Java Fundamentals (ii)-----Inheritance of three major features of Java object-oriented

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.