Java inheritance (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/11/25/2788247.html

Inheritance

In Java, classes can inherit from other classes and inherit the members and methods of the parent class.

Inheritance is simple but powerful: When you need to create a new class, but there are other classes, it already contains some ofCodeYou can inherit the new class from the existing class. In this way, you can reuse existing class members and methods without writing repeated code and debugging.

The subclass inherits all the members (variables, methods, and internal classes) from the parent class. The constructor is not a member, so it is not inherited. However, the constructor of the subclass can call the constructor of the parent class.

On the Java platform, java. Lang. object is the parent class of all classes. Some classes directly inherit objects, some classes inherit other classes, and others inherit from objects.

 

Inheritance example

Bicycle class

 Public   Class  Bicycle { //  The bicycle class has  //  Three fields      Public   Int  Cadence;  Public   Int  Gear;  Public   Int  Speed;  //  The bicycle class has  // One Constructor      Public Bicycle ( Int Startcadence, Int Startspeed, Int  Startgear) {Gear = Startgear; Cadence = Startcadence; speed = Startspeed ;}  //  The bicycle class has  //  Four Methods     Public   Void Setcadence ( Int  Newvalue) {Cadence = Newvalue ;}  Public   Void Setgear ( Int  Newvalue) {Gear = Newvalue ;}  Public   Void Applybrake ( Int  Decrement) {speed -= Decrement ;}  Public   Void Speedup ( Int  Increment) {speed + = Increment ;}} 

A ClassMountainbikeClass inherited from bicycle:

 Public   Class Mountainbike Extends  Bicycle {  //  The mountainbike subclass adds  // One Field      Public   Int  Seatheight;  //  The mountainbike subclass has one  //  Constructor      Public Mountainbike ( Int  Startheight,  Int  Startcadence,  Int Startspeed,  Int  Startgear ){  Super  (Startcadence, startspeed, startgear); seatheight = Startheight ;}  //  The mountainbike subclass adds  //  One Method      Public   Void Setheight ( Int Newvalue) {seatheight = Newvalue ;}} 

MountainbikeInherit all the variables and methods of the bicycle and addThe seatheight variable and the corresponding set method.MountainbikeThe new class has four variables and five methods, but you do not need to use them all.

If the method of bicycle is complex and requires a lot of time for debugging, this method of code reuse is quite simple and valuable.

 

What sub-classes can do

Subclass inherits all the public and protected members of the parent class, regardless of the parent class package. If the subclass and the parent class are in the same package, it also inherits the package-Private member of the parent class (the members of public, privat, and protected are not modified). For inherited members, you can replace it, hide it, or add new members:

1. inherited member variables can be directly used, just like other members.

2. Declare a new member variable. The member variable name is the same as the name of the parent class, and the member is hidden (not recommended)

3. declare that no new member variable of the parent class exists.

4. The inherited method can be used directly.

5. Implement a method similar to the signature of the parent class in the subclass, which can overwrite the method of the parent class.

6. implement a new static method with the same signature as the parent class in the subclass, which can overwrite the method of the parent class.

7. You can declare a new member method that does not exist in the parent class.

8. Implement the constructor of a subclass and call the constructor of the parent class by Using implicit or super.

 

Private member of the parent class

The subclass does not inherit the Private Members of the parent class. However, if the parent class already has a private member accessed by the public or protected method, you can use the inherited method, you can still indirectly access the private member of the parent class.

The internal class can access all the members of the nested class, including the Private Members of the nested class.

 

Object Conversion

The instantiation of an object may be written as follows:

 
PublicMountainbike mybike =NewMountainbike ();

This indicates that mybike is of the mountainbike type.

MountainbikeDerived fromBicycleAndObject. ThereforeThe mountainbike instance is both a bicycle and an object.

However, the reversal may not be possible: a bicycle may not be mountainbike. Similarly, an object may not beBicycleOr mountainbike.

Type conversion is displayed in the allowed inheritance and implementation. An object is replaced by one type with another. For example:

 
Object OBJ =NewMountainbike ();

In this way, obj is both an object and an object.Mountainbike.

On the other side, we write:

 
Mountainbike mybike = OBJ;

We will encounter a compilation error because for OBJ, the compiler does not know that it is a mountainbike. However, we can tell the compiler to convert OBJ to the type by displaying the conversion.Mountainbike

 
Mountainbike mybike = (mountainbike) OBJ;

When this type of conversion is inserted to a runtime check, the compiler will safely assume that obj is of the mountainbike type, but if obj is not of the mountainbike type, an exception will be thrown during the runtime.

 

Of course, you can use the instanceof operator for logic testing to determine whether the obj is mountainbike type and then convert it.

If(OBJInstanceofMountainbike) {mountainbike mybike=(Mountainbike) OBJ ;}

In this way, we will not throw any running exception when performing type conversion.

Related Article

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.