Inheritance and interface hands-on brain

Source: Internet
Author: User

Class Grandparent {public    grandparent () {        System.out.println ("Grandparent Created.");      Public grandparent (String string) {            System.out.println ("Grandparent created.string:" + String);}        } Package Demo;class Parent extends grandparent{public    parent () {        //super ("hello.grandparent.");        System.out.println ("Parent Created");       Super ("hello.grandparent.");}    } Package Demo;class Child extends Parent {public Child    () {        System.out.println ("Child Created");}    } Package Demo;public class Class {public    static void Main (string[] args) {        //TODO auto-generated method stub child        C = new Child ();    }}

In Java, this usually refers to the current object, and Super refers to the parent class. When you want to refer to something of the current object, such as a method of the current object, or a member of the current object, you can use this for this purpose, and, of course, another use of this is to invoke another constructor of the current object, which will be discussed immediately. If you want to quote something from the parent class, it's not super. Since this and the super have so many similar traits and innate relationships, we're here to discuss it, hoping to help you differentiate and master two of them.
In the general method
The most common scenario is that a parameter name in your method has the same name as a member of the current object, so you need to explicitly use the This keyword to indicate that you want to use a member, using the "this. Member name" instead of the one that is the formal parameter. In addition, you can use the "this. Method name" to refer to a method of the current object, but this is not necessary, you can directly use the method name to access the method, the compiler will know that you want to call the one.

Why do I have to call the construction method of the parent class before the constructor of the subclass is run? Can you turn around? Why?

Solution: Because a constructor is a special method, it is used primarily to initialize an object when it is created, that is, to assign an initial value to an object member variable. is always used with the new operator in the statement that creates the object, and a class can have more than one constructor, and the lesson is differentiated by the number of arguments or by the difference in the parameter type.

Constructs an object, calling its constructor first to initialize its member functions and member variables. Child classes have member variables and member methods of the parent class, and the member variables and member methods inherited from the parent class are not properly initialized.

cannot be called in turn because the parent class does not know what the child class has and the subclass does not get the initialized parent class variable, causing the program to fail.

Inheritance and interface hands-on brain

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.