Java programmer must read: Basic (7)

Source: Internet
Author: User
Tags abstract define object inheritance variables
Program | programmer Java programmer must read: Basic articlesTime: 2001/09/13 13:31 Author: ZSC Pacific Network College

 
2.5 What is inheritance

A class can inherit state and behavior from its parent class. Inheritance provides a powerful and natural mechanism for organizing and structuring software programs.

Generally speaking, objects are defined in terms of class. You can already know many objects from its class now. Even if you know, if I tell you it's a bike, you know it has two wheels and pedals and so on. Object-oriented systems are more in-depth, allowing classes to be defined in other classes. For example, mountain bikes, racing cars and tandem bicycles are all kinds of bicycles. In object-oriented technology, mountain bikes, racing cars and tandem bicycles are all subcategories of bicycles. Similarly, bicycles are the parents of mountain bikes, racing cars and tandem double bikes. This parent-child relationship can be as shown in Figure 9:

(Figure 9)

Each subroutine inherits the state from the parent class. Mountain bikes, racing cars and tandem double bikes share these states: speed and so on. Similarly, each subclass inherits classes from the parent's methods, mountain bikes, racing cars, and tandem double bikes that share these behaviors: braking, changing foot speed, and so on.

2.5 What is inheritance

However, subclasses cannot be limited by the state and behavior provided by the parent class. Subclasses can add variables and methods to variables and methods that inherit from the parent class. For example, a tandem double bike has two seats, which is not in its parent class.

Subclasses can also overload inherited methods and provide special execution methods for these methods. For example, if you have a mountain bike with an extra gear set, you can reload the gears to make it possible for cyclists to use these new gears.

Nor can you be limited to one level of inheritance. The hierarchical structure of an inheritance tree or class can be very deep. Methods and variables are progressively inherited. In general, the more you have in the hierarchy, the more behavior you have.

If the object class is at the top of the hierarchy, each class is its descendants (either directly or indirectly). A type of object retains a reference to any object, such as a class or an instance of an array. Object provides the behavior that is required to run on a Java virtual machine. For example, all classes inherit the object's ToString method, which returns a String representing the object.

Now, why do we use inheritance, and what are the benefits of that? Some of the benefits are:


Subclasses provide special behavior, which is not in the parent class. By using inheritance, programmers can reuse code in the parent class more than once.


A programmer can perform a parent class, called an abstract class, to define the total behavior. This abstract parent class can define and partially execute the behavior, but the vast majority of the parent classes are undefined and not executed. Other parts are implemented by programmers to implement special subclasses.

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.