basic java code to understand inheritance

柔情痞子 提交于 2019-12-24 13:16:00

问题


Why does the System.out.println(b.h + " " + b.getH()); prints the following:

Beta 44 <br/>
4 44          (notice this is in the second line)

I was expecting it to print something like this:

4 Beta 44 44   (this one is in one line)

The reason why I thought it would print this way is because we call b.h first which is 4.Then we call b.getH() which will print Beta 44 44

Here is the code:

class Baap{

        int h = 4;
        public int getH(){
                System.out.println("Beta " + h);
                return h;
        }
}

class Beta extends Baap{
        int h = 44;
        public int getH(){
                System.out.println("Beta " + h);
                return h;
        }

        public static void main (String [] args) {
                Baap b = new Beta();
                System.out.println(b.h + " " + b.getH());
        }

}

回答1:


First, the call to getH() prints "Beta 44", since the argument of System.out.println(b.h + " " + b.getH()) is evaluated before println is called.

Then System.out.println(b.h + " " + b.getH()) prints "4 44".

b.h returns 4 because there is no overriding for instance variables, and since b's compile time type is Baap, b.h returns the instance variable of the super class.

b.getH() returns 44 because you b's runtime type is Beta, which overrides getH().




回答2:


The output is because the expression inside System.out.println(b.h + " " + b.getH()); is evaluates first then printed. So the order is

System.out.println(b.h + " " + b.getH());  // calls getH() method
System.out.println("Beta " + h);           //print Beta 44
System.out.println(b.h + " " + b.getH());  //prints 4 44

So the complete o/p is

Beta 44
4 44

b.h is 4 because variables are not inhreited so it called by reference ie h variable of Baap class.
b.getH() give you 44 value returned from Beta class method(h=44 in Beta class).




回答3:


Output is

Beta 44

4 44

To evaluate expression System.out.println(b.h + " " + b.getH()); method b.getH() needs to get evaluated first. As the object is infact of class Beta you see Beta 44 printed in 1st line [Since you are using println method you will have a new line character at the end] and the method returns 44.

Then it's as simple as System.out.println(b.h + " " + 44);

Now as you are just referring to a variable directly using the reference which is of type Baap you will get 4 [Remember variables are not overridden, they are just shadowed]. And hence the 2nd line 4 44




回答4:


System.out.println(b.h + " " + b.getH());

This is executed in the following order:

String temp1 = b.h
String temp2 = temp1 + " "
String temp3 = b.getH () // prints Beta 44
String temp4 = temp2 + temp3
System.out.println (temp4) // prints 4 44



回答5:


It first evaluates the expression in println() statement, because of that Beta44 gets printed first. then it starts printing values in println statement from left to right , then print 4,44. it prints 4 because the instance variable is of Baap type(compile time). So, it picks the value of variable defined in Baap class. Only overridden methods are being called from child class if same method exists in both parent and child. But, variables and static fields are called from instance variable type which is Baap in this case.

Hope it will help.




回答6:


It gives absolutly right answer

Beta 44 
4 44

for this statement: System.out.println(b.h + " " + b.getH());

As programm counter or compiler checked a statement from right to left and in the println() method programm counter first identified and eveluted the method's argument , since it excutes first b.getH() so "Beat 44" is the first part of the answer ,after that it gets a new line for println() and at last it print b.h value (4) and then return value of b.getH() which is 44.




回答7:


It is because same overridden method is used. When an overridden method is used then the class will give preference to its own method rather than parent's class method.



来源:https://stackoverflow.com/questions/29862464/basic-java-code-to-understand-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!