JAVA_SE basics -- 39. Inheritance

Source: Internet
Author: User

JAVA_SE basics -- 39. Inheritance

In object-oriented programming, a new class can be derived from an existing class. This is called inheritance ).

 

 

Vernacular explanation:

Example 1: inheritance generally refers to the inheritance of property from the elders, or something that the children have given to them by their parents.

Example 2: Both cats and dogs belong to animals and can be described as automatically inherited by cats and dogs in the program. Likewise, coffee cats and Persian Cats inherit from cats, while scalpers and bitdogs inherit from dogs. All animals form an inheritance system, specifically:

The difference is that the entity inherited here is a class rather than a non-human thing, that is, a thing. Inheritance is a member of the Child class that owns the parent class.

 

When a class inherits from another class, we will say that this is a subclass to inherit the parent class. If you want to know whether something should inherit another thing, you can use a IS-A test.

A triangle is a polygon ...... Yes.

A gynecologist is a doctor ...... That's right.

Teddy is a dog ...... Yes.

The tub is a bathroom ...... Error.

Ribs are not cleaned ...... What is this?

 

If you want to know whether the design is correct during future development, such a test will be used for testing. If it is unreasonable, it indicates that your design is faulty.

 

The bathroom and bath are indeed associated, but not inherited. The relationship between the bathroom and the bath is HAS-A. If "there is a bath in the bathroom" is true, this indicates that the bathroom has an instance variable for the bath. That is to say, the bathroom will have a reference, but the bathroom has not inherited the bath.

 

 

Inheritance features

 

1: describes the relationship between classes.

2: Reduce repeated code between classes

1. Reduce the code between objects and reuse static variables.

2: Reduce the code reuse between classes to inherit

 

 

Test code 1 (both the dog class and cat class inherit the Animal class ):

 

Package day08; // defines the Animal class Animal {String name; // defines the method void shout () {System. out. println (this. name +);} // defines the Dog class to inherit from the Animal class Dog extends Animal {public void dogName () {// defines a method for printing name: System. out. println (name = + this. name) ;}} class Cat extends Animal {// defines the public void catName () {System. out. println (name = + this. name) ;}} public class Demo1 {public static void main (String [] args) {Dog dog = new Dog (); // create a Dog class instance object dog. name = husky; // value Dog for the name attribute of the dog class. dogName (); // call the dogName () method Dog of the dog class. shout (); // call the shout () method of the Dog class Cat cat = new Cat (); // create a Cat class instance object cat. name = Persian Cat; // assign a cat value to the name attribute of the Cat class. catName (); // call the Cat class catName () method cat. shout (); // call the shout () method of the Cat class }}
Running result:

 

 

The Dog class in test code 1 inherits the Animal class through the extends keyword, so that the Dog class is a subclass of the Animal class. From the running results, we can see that although the dog class and cat class (subclass) do not define the name attribute and shout () method, they can access these two members. This indicates that when a subclass inherits the parent class, it automatically owns all the members of the parent class.

 

Pay attention to the following issues in class inheritance:

1. in Java, classes only support single inheritance, and multi-inheritance is not allowed. That is to say, a class can only have one direct parent class. For example, test code 2 below is invalid.

 

Test code 2 (the class does not allow many inheritance ):

 

Class A {} class B {} class C extends A, B {}// class C cannot inherit both class A and class B ..

2. Multiple classes can inherit one parent class. For example, test code 3 below. This is allowed.

 

 

Test code 3 (multiple classes can inherit one parent class ):

 

Class A {} // both class B and class C can inherit class a B extends A {} class C extends {}

3. in Java, multi-level inheritance is allowed. That is, the parent class of a class can inherit from another parent class. For example, Class C inherits from Class B, class B can inherit Class A, which is A subclass of class C. In Java, subclasses and parent classes are a relative concept. That is to say, a class is the parent class of a class and can also be a subclass of another class. Class B is A subclass of Class A and the parent class of class C. The following test code 4 is allowed.

 

 

Test code 4 (C inherits B, B inherits ):

 

Class A {}// class A is the parent class B extends A {}// class B of class B and class C inherits class, class B is A subclass of class A. class C extends B {}// class C inherits class B. class C is A subclass of class B and also A subclass of class.

 

 


Inheritance details;

1. Set the class name. The inherited class is called the parent class (base class), and the inherited class is called the subclass.

Ii. subclasses cannot inherit all the members of the parent class.

1: the parent class defines the complete static member, non-static, and constructor. Static variables and static Operators

Both methods can be called successfully in the form of a subclass name. A static member of the parent class.

2: All private members cannot be inherited. private modified members.

3: The constructor cannot be inherited.

Iii. How to Use inheritance

1: do not inherit to use inheritance. Employees and students have common members.

Code to let the workers inherit the students.

 

Override the parent class Method

In the inheritance relationship, the subclass automatically inherits the methods defined in the parent class, but sometimes it needs to modify the inherited methods in the subclass, that is, override the methods of the parent class.

The method to be rewritten must have the same name, parameter list, and return value type as the method to be overwritten by the parent class.

 

The Cat_1 class inherits the shout () method from the Animal class. This method prints the Animal name when it is called. This obviously cannot clearly describe which Animal is called, the Cat object indicates a Cat, and the call should be "meow". To solve this problem, I can rewrite the shout () method in Animal_1 of the parent class in Cat_1.

 

Test code 5 (override the shout () method of the parent class ):

 

Package day08; // defines Animal class Animal_1 {String name; // defines the method void shout () {System. out. println (animal name);} class Cat_1 extends Animal_1 {// define a method for printing name void shout () {System. out. println () ;}} public class Demo2 {public static void main (String [] args) {Cat_1 cat = new Cat_1 (); cat. shout ();}}
Running result:


 

The test code 5Cat_1 class inherits the Animal_1 class. It can be seen from the results that when the shout () method of Cat_1 class object is called, only the method of the subclass rewriting from write will be called, and the shout () method of the parent class will not be called.

 

P.S.

When a subclass overrides the method of the parent class, it cannot use more strict access permissions than the method to be overwritten in the parent class. If the method in the parent class is public, the subclass method cannot be private. For more questions about the access permission, I will elaborate on it in my later blog. Here is an impression.

 

 

 

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.