Learn about the nature of Java inheritance, polymorphism, abstract classes, interfaces, and more (1)

Source: Internet
Author: User

In recent days, the Java programming idea turned over, suddenly found that the so-called XXX from the beginning to the mastery of the series to speak simply can not see, can only be used as an introductory book.

In order to not forget in the future, write some ideas down.

About inheritance and polymorphism

I think inheritance is an essential part of object-oriented language, referring to the idea of Java programming: "When creating a class, always inherit." Inheritance is implemented by the extends keyword, and all domains and methods in the base class are available after implementation.

classfirst{PrivateString s = "Extend"; First () {} Public voidBoth () {System.out.println ("1"); }     Public voidthree () {System.out.println ("2"); }} Public classExtendsdemoextendsfirst{Extendsdemo () {}/*Public Void () {System.out.println ("3");    } public void Three () {System.out.println ("4"); }*/     Public Static voidMain (string[] args) {Extendsdemo d=NewExtendsdemo (); First F=NewFirst ();        D.two ();        D.three ();        F.two ();    F.three (); }        }

When a subclass method overrides a parent class method, the subclass object calls the method of the subclass, and the parent class object created in the subclass calls the method that is still the parent class. If the subclass method does not overwrite the parent class method, then the child class object and the parent class object are called the uniform method of the parent class.

In addition, the private domain of the parent class is inaccessible in subclasses.

In the case of multiple inheritance, the constructor is invoked from the top down, so that the base class has completed initialization before its export class constructor can access it.

classfirst{ PublicFirst () {//base classSystem.out.println ("First ()")); }}classSecondextendsfirst{ PublicSecond () {System.out.println ("Second ()"); }    }classapple{ PublicApple () {System.out.println ("Apple ()"); }}classbanana{ PublicBanana () {System.out.println ("Banana ()"); }} Public classTest1extendssecond{PrivateApple A =NewApple (); PrivateBanana B =NewBanana ();  PublicTest1 () {System.out.println ("Test1 ()");}  Public Static voidMain (string[] args) {Test1 T=NewTest1 (); }}

The above outputs are respectively

First ()
Second ()
Apple ()
Banana ()
Test1 ()

That is, the constructor's invocation order starts from the base class, then the initialization method of the member is called in the order of Declaration, and finally calls the body of the derived class constructor (the construction method).

PS: Bo Master just started to contact Java soon, is learning, there may be a lot of local understanding or problems, I hope the Bo friends do not spray.

Learn about the nature of Java inheritance, polymorphism, abstract classes, interfaces, and more (1)

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.