Tim Driven Development | Test until the fear goes away
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Alfonso Saenz wrote:I don't understand how Minor class can "see" Uber class since static members are not inherited.
Could anyone give me some light?
Alfonso Saenz wrote:but the code compiles fine and prints "2".
In this case there is no instance at all. So... is "y" inherited??
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Alfonso Saenz wrote:Because private variables are not inherited, "y" field is not supposed to be seen by Minor. I guess we have compiler error in line "System.out.print(y)".
Alfonso Saenz wrote:So... can I conclude that static variables are inherited?
Sachin Tripathi wrote:Yes static variables are inherited like their fellow instance variable s
Alfonso Saenz wrote:For the first example: I think count is not accessible from subclass because even though class is public, count variable has package private access. So... taking into account that both classes are in different packages, "count" cannot be seen from subclass.
Second example: "y" is now instance variable, then "System.out.print(y)" will give us error compiling, cause I cannot access to "y" variable without real object (instance).
Ramya Subraamanian wrote:also y cannot be refered while invoking the parent class constructor , so this line super(y) will also throw a compiler error.
Ramya Subraamanian wrote:and you must have a main method in the public Uber class.
That's only true if y is an instance variable. If y is a class (static) variable, the code will compile successfully (no compiler errors).
Why? There's a valid main method in the Minor class to run the application.
Ramya Subraamanian wrote:yeah , but only Uber is public right..
Ramya Subraamanian wrote:javac Uber
java Minor
commands to access that Minor's main method , because Javac Uber command will create Uber.class and Minor.class
Ramya Subraamanian wrote:Instance variables are created when the constructor is called. if you make a call to Super it calls the parent constructor before the child constructor is called, hence the instance variables are not yet created and hence the compiler throws an error.
Ramya Subraamanian wrote:and if that's so why is it not throwing a compiler error in line 9 as well.