Learn java-inheritance from the beginning

Source: Internet
Author: User



I. Concept of inheritance


Inheritance is just a literal understanding, a child inherits some of the attributes of the parent, and some "genetic mutation" may occur that can contain methods or properties that the parent class does not have. It is called "pupil surpasses".

Why use inheritance? On the one hand, inheritance can extract the parent class upward, increasing the code reuse rate. For example, the teacher class and the student class can inherit the person class, on the other hand, inheritance can elicit polymorphism, which is an important feature of object-oriented.


An example:

class person{string name; Person (String name) {this.name = name; System.out.println ("person is constructed");} public void Eat () {System.out.println ("I am-a person. I can eat! ");}} Class Student extends person{string num;//study number Student (String num,string name) {//Call parent class constructor Super (name); this.num = num; System.out.println ("Student is constructed");} public void Study () {System.out.println ("I am a student. I can Sutdy ");}} public class InheritsTest1 {public static void main (string[] args) {//Create a Student object Student student1 = new Student ("Zhang", "111" );//Call the Parent class meal Method Student1.eat ();//Call learning Method Student1.study ();}}

The results of the output are as follows:


Points:

1. Inherited syntax class Subclass extends parent class {}

2. Subclasses automatically have members of the parent class public and protected.

3. On the question of permissions:

public outside can access unlimited

Private class is not accessible, only class internal access

Protected inherited subclasses can be accessed (also in different packages)

Default is no write permission, and the classes in the same package are accessible.

4.super () Call the constructor of the parent class, this sentence needs to be in the subclass constructor as the first sentence!!!


Two. Relationship between subclasses and parent-class methods


There are several relationships between a subclass and a method in the parent class, the first being the example above, where the subclass inherits directly from the parent class's method, does not change, can call the parent class's method directly, the subclass can also extend the method of the parent class, that is, add the new method. If the person will eat,student inherit the man class, he will also eat, but add the study method.


The latter two relationships occur in cases where the subclass's method and parent class have the same name.


1. Subclasses overriding methods of the parent class

Example:

class person{string name; Person (String name) {this.name = name; System.out.println ("person is constructed");} public void Eat () {System.out.println ("I am-a person. I can eat rice! ");}} Class Student extends person{string num;//study number Student (String num,string name) {//Call parent class constructor Super (name); this.num = num; System.out.println ("Student is constructed");} public void Eat () {System.out.println ("I am a student. I can eat Brain Platinum ");} public void Study () {System.out.println ("I am a student. I can Sutdy ");}} public class InheritsTest1 {public static void main (string[] args) {//Create a Student object Student student1 = new Student ("Zhang", "111" );//cover the parent class of the Eat Rice method, call the subclass of the Eat Brain Platinum Method Student1.eat ();//Call learning Method Student1.study ();}}
Results:


It is visible that the subclass overrides the method of the parent class. When the subclass is called, the Eat method is called, not the "Eat rice" method of the parent class, but the "eat" method of the subclass.



2. Subclasses overloading a method of a parent class


class person{string name; Person (String name) {this.name = name; System.out.println ("person is constructed");} public void Eat () {System.out.println ("I am-a person. I can eat rice! ");}} Class Student extends person{string num;//study number Student (String num,string name) {//Call parent class constructor Super (name); this.num = num; System.out.println ("Student is constructed");} public void Eat (String food) {if (Food.equals ("Naobaijin")) System.out.println ("I am a student. I can eat Brain Platinum ");} public void Study () {System.out.println ("I am a student. I can Sutdy ");}} public class InheritsTest1 {public static void main (string[] args) {//Create a Student object Student student1 = new Student ("Zhang", "111" );//Call the parent class of the Eat Rice Method student1.eat ();//Call the subclass overloaded eat-brain Platinum Method Student1.eat ("Naobaijin");//Call learning Method Student1.study ();}}

The results are as follows:


So student can eat, but also can eat brain platinum, different in the Eat method to accept the parameters, if there is no parameter, then he executes the parent Eat rice method, if there are parameters, then execute the sub-class overloaded Eat brain Platinum method, eat Brain platinum.



Points:

1. The method relationship between the subclass and the parent class has an extension relationship (calling the parent class method directly or expanding the new method), overriding the relationship (the subclass is the same as the parent method, the subclass method overrides the parent class method), and the overloaded relationship (the subclass method differs from the parent class method parameter, which method is selected by the parameter when called

2. The overlay cannot overwrite the static method, nor can it overwrite the final method.

3. The allowable access scope of the coverage method cannot be less than the original method, and the overridden method throws more exceptions than the original method.

4. The method of the parent class can be called in a subclass, or it can be overridden. The Super keyword is used

Examples are as follows:

Class person1{public void Eat () {System.out.println ("I am a person. I can eat rice! ");}} Class Student1 extends Person1{public void eat () {//can use the Super keyword in subclasses to invoke the method of the parent class, including the overridden Method Super.eat (); System.out.println ("I am a student. I can eat Brain Platinum ");} public void Study () {System.out.println ("I am a student. I can Sutdy ");}} public class InheritsTest2 {public static void main (string[] args) {//Create a Student object Student1 student1 = new Student1 ();//Call the sub-class ea T method Student1.eat ();}}

Results:



Three. Final keyword and object class
The final keyword is a lot of a modifier in Java, which can be literally understood, and ultimately, a constant meaning. There are several uses for final:
1.final decorated classes can not be inherited, that is, "read-only" immutable classes, to prevent arbitrary modification of the class at the time of inheritance. Eg:final class Name {} the 2.final retouching method cannot be overridden. Final return value Method name () {} 3.final modified variables can not be modified, that is, constants. Usually eg:max_value in uppercase letters
About the object class, which is the base class for all classes in Java. If a class does not inherit other classes, it inherits the object class by default. The object class provides several methods that we have previously used, such as the ToString (), Equals () method.



Learn java-inheritance from the beginning

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.