The process of instantiating an object with a javase-subclass

Source: Internet
Author: User

 Packagecom.btp.t1;//the entire process of instantiating a subclass object/*1. When the bottommost subclass instantiates an object, its parent class, the parent class of the parent class ... Constructors for all classes to the object class are called, except that one is called when a class has more than one constructor. Within the subclass constructor, the parent class constructor is called by default: Super (); when there is a this keyword, the parent class constructor is not called, and other constructors within the same class are called, so a class with N constructors allows only a maximum of n-1 constructors to use the This keyword. At least one constructor is called to the constructor of the upper parent class. When the parent class overloads a constructor, the default invisible parameter constructor disappears, and the parent class does not overload the constructor of another invisible parameter, so the subclass constructor does not use this or the Super keyword, because the subclass constructor does not use the This and Super keywords, By default, the parent class's invisible parameter constructor is called, and the constructor does not exist, and an error is resolved: 1) The parent class declares an invisible parameter of the constructor 2) called the parent class, and another parameter is not NULL. Therefore, it is recommended that you create a constructor for an invisible parameter when you create a class. The object class's invisible parameter constructor is bound to be called. *  */ Public classParent { Public Static voidMain (String args[]) {//Dog D=new Dog ();//D.setage (ten);//d.setname ("Floral");//d.sethostname ("Xiao Ming");Dog d=NewDog ("Little Black", "dog", 12); System.out.println ("Name:" +d.getname () + "Age:" +d.getage () + "owner:" +d.gethostname ()); }}classcreator{Private intAge ;  Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicCreator () {//can try to comment out this constructorSuper(); System.out.println ("This is Creator ' s () constructor"); }     PublicCreator (intAge ) {        Super();  This. age=Age ; System.out.println ("This is Creator ' s (int) constructor"); }    }classAnimalextendscreator{PrivateString name;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicAnimal () {
Default super ();
       System.out.println ("This is Animal ' s () constructor");
    }     PublicAnimal (String name,intAge ) {        Super(age);  This. name=name; System.out.println ("This is Animal ' s (string,int) constructor"); }    }classDogextendsanimal{PrivateString HostName;  PublicString gethostname () {returnHostName; }         Public voidSetHostName (String hostName) { This. HostName =HostName; }     PublicDog () {Super(); System.out.println ("This is the Dog ' s () constructor"); }         PublicDog (String hostname,string name,intAge ) {        Super(Name,age);  This. hostname=hostname; System.out.println ("This is the Dog ' s (string,string,int) constructor"); }}

The process of instantiating an object with a javase-subclass

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.