Characteristics of inheritance in Java

Source: Internet
Author: User
Tags getcolor

Characteristics of inheritance in Java
Inheritance is an important application in Java. So why inherit? We can look at an example: Cat and dog. Cats and dogs are common animals in life, what are the similarities? all have the color (black and white yellow flower etc.), all have the legs, all must eat, all must sleep and so on. So, The number of colors and legs is their properties (member variables), and eating and sleeping are their behaviors (functions). If there is no inheritance, write the cat and dog categories, as follows:

 Public classcat{//Cat ClassString color;//member variable "color"    intNum//member variable "number of legs"    classCat () {}//non-parametric construction    classCat (String color,intNUM) {//with a reference structure         This. color =color;  This. num =num; }     Public voidSetColor (String color) {//Set Method         This. color =color; }     PublicString GetColor () {//Get Method        returncolor; }     Public voidSetnum (intnum) {         This. num=num; }     Public intGetnum () {returnnum; }}

Dogs and cats In addition to the class name, the others are the same, it is a waste of time to write, but also waste memory. Therefore, inheritance can improve the reusability of code. This is similar to Java's cross-platform principle, all for the sake of saving and convenient.
So, in Java, how do you inherit it? That is, the class has a relationship with the class, resulting in a parent-child relationship. That is, one of the two classes that belongs to the other, can inherit. Like cats and dogs, hair doesn't belong to dogs, dogs don't belong to cats, they can't inherit. But cats belong to animals, Dogs also belong to animals. Inheritance can be established between animals and cats. Similarly, an inheritance relationship can be established between animals and dogs. In this way, the general properties and behavior of animals are inherited by cats and dogs.
Java inheritance, there are some of their own characteristics. First: This inheritance is a single-inheritance relationship and does not support multiple inheritance. Single inheritance means that a father can have multiple sons, but a son can have only one father. Multi-inheritance means that a disciple can have a lot of masters (the Monkey King has two masters, one is the Bodhi old ancestor, One is the Tang priest. The Monkey King inherited the Bodhi old ancestor part martial arts, and followed Tang life, finally became Buddha. Two kinds of inheritance each have each characteristic, the single inheritance presents the tree structure, is relatively safe. Multiple inheritance presents a network structure, which is more random and less secure. Java simply chooses single inheritance. At the same time, this kind of inheritance can continue, like the master-father-son-Sun This case, Java is also called multi-layer inheritance.
Second: The parent class's private members cannot inherit, the parent class's constructor cannot inherit, and everything else can be inherited. Why can't we inherit it? Because we have to fully respect the privacy of others, even if this person is your father or son or wife. The parent class lets you inherit, you can inherit, the parent class hides, does not want you to inherit, You can't take it. In addition, the constructor method of the parent class cannot be inherited, because the constructor method has two functions. The first is the initialization of this class of members, the constructor method name and the class name are the same, if inherited, and sub-class names are not the same, easy to confuse. The second is the super () The default access to the parent class's parameterless construction method. Therefore, the subclass and the parent class each have each construction method, but the subclass also relies on its own construction method default accesses the parent class the construction method, is equivalent to the family tree the blood relations, is an invisible link. All things in nature are vested and inherited, as in Java, Each class has an inherited parent class, and if not, it inherits the object class by default.
In addition, the subclass must inherit not only the parent class, but also some development, the so-called pupil surpasses. Inheritance in Java is embodied in the method of rewriting, that is, to create a method in a subclass, like the parent method name, but with its own unique content, so that inheritance can inherit the function of the parent class. It also has its own new features. So what are the requirements in the method rewrite? First: The private method of the parent class cannot be overridden because there is no inheritance at all, and there is no basis for rewriting. Second: The overridden method, access permissions cannot be lower. Good things to learn to share, This will grow. Let's end with a cat and dog case.

/*First step: Create a parent class: Animal class*/classanimal{//Animal ClassString color;//member variable "color"    intNum//member variable "number of legs"     PublicAnimal () {}//non-parametric construction     PublicAnimal (String color,intNUM) {//with a reference structure         This. color =color;  This. num =num; }     Public voidSetColor (String color) {//Set method Setting "Color"         This. color =color; }     PublicString GetColor () {//Get method gets "color"        returncolor; }     Public voidSetnum (intNUM) {//Set method Sets "number of legs"         This. num=num; }     Public intGetnum () {//Get method gets "number of legs"        returnnum; }     Public voidShow () {//show the properties of animal classesSystem. out. println (GetColor () +"..."+getnum ()); }     Public voideat () {System. out. println ("Eat"); }}/*Step Two: Set up two sub-categories, namely Cat class and Dog class*/classCat extends animal{ PublicCat () {}//non-parametric construction     PublicCat (String color,intNUM) {//with a reference structure         This. color =color;  This. num =num; }     Public voidCatchmouse () {//Cat-specific behavior: catching miceSystem. out. println ("Catch the mouse"); }}classDog extends Animal { PublicDog () {}//non-parametric construction     PublicDog (String color,intNUM) {//with a reference structure         This. color =color;  This. num =num; }     Public voidWatchhome () {//Dog-specific behavior: housekeepingSystem. out. println ("Housekeeping"); }}/*Step Three: Build a test class to test*/ Public classtestanimal{ Public Static voidMain (string[] args) {Cat C1=NewCat ("Yellow",4);//Creating ObjectsC1.show ();//calling the parent classC1.eat ();//calling the parent classC1.catchmouse ();//calling subclassesSystem. out. println ("++++++++++++++++"); Dog D1=NewDog ("White",4);//Creating ObjectsD1.show ();//calling the parent classD1.eat ();//calling the parent classD1.watchhome ();//calling subclasses    }}

Characteristics of inheritance in Java

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.