简体   繁体   中英

Can someone explain me this little code?

class Feline {
    public String type = "f";

    public Feline() {
        System.out.println("feline");
    }
}

public class Cougar extends Feline {

    public Cougar() {
        System.out.println("cougar");
    }

    void go() {
        type = "c";
        System.out.println(this.type + super.type);
    }

    public static void main(String[] args) {
        new Cougar().go();
    }
}

In the output of the console I get this:

feline
cougar
cc

I am not sure why I get this output if I am creating a Cougar object and the constructor of the cougar class does not make a super call to the feline class constructor, also I don't understand why I get "CC" when calling the go() method. Sorry if it's a very basic question but I will appreciate your help.

Java objects are constructed from the inside out: When you say new Cougar() , first space is allocated for the object, then a Feline object is constructed in that space by calling a Feline constructor, then finally your Cougar constructor is run.

You can specify which Cougar constructor is run by calling it explicitly with a super(..) call; that's particularly useful if there are several and you want to pick one by specifying arguments. But if you don't invoke one, the compiler will insert the super() call.

As to the 'cc', when type = "c"; was encountered, there was no local variable named "type" defined. That means it's a member variable, so the compiler interprets that as this.type = "c"; . But there's only one member called "type", and that's in Cougar. So this.type and super.type are both the same thing, have been set to "c", and "c" is typed twice.

You have created two classes where Feline is a super class and Cougar is a child class. As Cougar extends Feline , Cougar inherits type variable from Feline .

Now, Feline has a default constructor where feline string is printed. Cougar also has a default constructor. As per the line new Cougar().go(); in the main method, you are creating a new object of Cougar which calls the default constructor and implicitly, it calls super() that calls the constructor of Feline class.

Now new Cougar().go() method is setting the type to "c" which means the value of the variable is changed as super.type and this.type are the same copy. So when you call this method, it prints this way:

feline

cougar

cc

In the main(), when you are creating the object new Cougar() then the constructor public Cougar() is called which inturn calls parent constructor public Feline()

hence first sout of Feline() feline is displayed on console and then sout of Cougar() cougar is displayed on console.

This is popularly known as Instance Control flow in java.

Then cc is displayed in the output because of go() method, as "type" variable of both this operator and super operator points to "c".

Hence, the output is:
feline //because parent constructor is called first in Instance Control Flow.
cougar //because child constructor is called after parent constructor.
cc // since type variable of both this instance and super instance points to "c" object.

When you are creating child object. It automatically call the parent constructor because parent need to initialized for child.

So, when child class constructor is called it automatically call the parent default/non parameter constructor.ie o/p feline cougar

and .go() is changing the value of this.type and super.type and this is representing same value. because type variable is visibility is public, It also accessible in child class and there is no variable in child class with variable name of type.

So, The complete o/p is

feline cougar cc

According to your code: There is a parent class called "Feline" and child class called "Cougar". when it execute, It first go to the parent/super class(Feline) constructor and print : feline.

Then it executes the child class (Cougar) constructor and print : cougar.

After that it executes the main method; In your main method you are referring to another method called "go", then executes the code inside "go" method:

Inside that it overriding the value of variable called "type" to : "C".

So when you are going to print the value of type,

this.type = C and super.type = C (because of the overriding)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM