----inheritance in the Java language (ii)

Source: Internet
Author: User

Day11---Inheritance in the Java language (ii)

I. Overview of Succession:

In this section of today we talk about some of the details of system learning inheritance, using the final keyword, the This keyword, and the constructor. Let's use it for one by one.


Ii. Succession (extends)

1. Use the "final" keyword

We've already talked about the final keyword, which, once the variable is modified, will change to a constant, and the value of the variable cannot be changed in subsequent operations.

If you use the final keyword to decorate a class (final Class), this class is not inherited by the quilt class. Use it to modify it to signify a final meaning. And not be inherited.

If you are using the final keyword to decorate a method inside a class. Then this method will not be able to be rewritten!

2. Example one:

    package www.ex.c1     //defines a parent class.          //If you use the final keyword to decorate the parent class, the parent class cannot be inherited. That doesn't make sense.     /* final */ class username{//defines 3 member variables. string name; string password;  //If the display method is decorated with the final keyword, then the subclass cannot rewrite a method. will automatically error Public  void  display () {System.out.println ("name:" +name); System.out.println ("Password:" +password);}      }     //defines a subclass that inherits the parent class      class  students extends username{//defines a member variable and overrides the display () method in the parent class. Int age;public void display () {super.display (); System.out.println ("Age:" +age);}       }    //This is a main class      public  class extends02 {public static void main (String[] args)  {Students  st = new students (); St.namE= "ls"; st.password= "123456"; St.age=25;st.display ();}     }

3. Use the "This" keyword:

Using the This keyword is a good way to save time and code, through the This keyword can represent the current object, we pay attention to understand some of the meaning of object-oriented. The common use of this keyword is to use the This keyword in cases where the same name is defined between the global variable and the local variable.

4. Example Two

    package www.ex.c1    class Username{         //defined global variables and methods string name; String password;public  void  display () {System.out.println ("name:" +name); System.out.println ("Password:" +password);}      //defines a constructor function. The This keyword is used here. The name of the parameter and the global name are the same.          public username (string name, string  Password) {this.name = name;this.password = password;    }     class students extends username{        int  age;public void display () {        super.display (); System.out.println ("Age:" +age);} constructor with parameters         public students (string name,string  Password,int age) {This.name = name;this.password = password;this.age = age; System.out.println ("constructor of subclass 2");}      //This is a main class      public class extends02 { Public static void main (String[] args)  {    Students s  = new students ("ls",  "ls123456",  24); S.display ();}     }

5, the constructor, in the inheritance can also implement the constructor, but the child class implementation constructor parameters can also be given the constructor parameters in the parent class. form a function of initialization. Specific examples are also described in two.


Third, concluding remarks:

We are finished with the inheritance section here. You have to understand that a core idea is that something in the parent class inherits from the subclass, as long as it excludes a few special cases.

This article from the "Program Ape" blog, reproduced please contact the author!

----inheritance in the Java language (ii)

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.