System.out.println(super); is showing compile time error for some reason. well i think this is supposed to print super class's toString();
and if the compiler doesn't restrict you then which method will it call?myFunc(super)
Nikhil Padharia wrote:It shows you error as you cannot pass super as an argument to any of the method.
Lets look an example
Here suppose if you calland if the compiler doesn't restrict you then which method will it call?myFunc(super)
How will it call abstract method?
Thus preventing to violate OOP concept compiler prevents you to do that.
Jeff Verdegan wrote:
No, that's not it at all. It's simply the fact that super is not a reference expression, even though it looks like one in some of contexts in which it is used.
gauravkv gupta wrote:hi all,
System.out.println(super); is showing compile time error for some reason. well i think this is supposed to print super class's toString();
Nikhil Padharia wrote:
Jeff Verdegan wrote:
No, that's not it at all. It's simply the fact that super is not a reference expression, even though it looks like one in some of contexts in which it is used.
Why the fact lies for super not being a reference expression?
Don't you find the answer to that in my post??
Jeff Verdegan wrote:If your argument were valid, then no subclass could ever call a method on an abstract parent class because "super would be abstract."
And, finally, if your argument were valid, then all we'd need to do is make the superclass concrete, and then we'd be able to pass super as an arg, but if you try that, you'll find it doesn't work.
Jeff Verdegan wrote:
Jeff Verdegan wrote:And if super was a reference expression, it would almost certainly be legal. Your post is based on the assumption that there's a separate "super" object and "child" object. However, there's only one object, and it's a concrete class. If super were valid to pass as a method arg, it would refer to the same object as "this".
Nikhil Padharia wrote:
Jeff Verdegan wrote:If your argument were valid, then no subclass could ever call a method on an abstract parent class because "super would be abstract."
And, finally, if your argument were valid, then all we'd need to do is make the superclass concrete, and then we'd be able to pass super as an arg, but if you try that, you'll find it doesn't work.
Absolutely no question of making the class concrete, Whatever your super class is, irrespective of abstract or concrete, It wont let you pass super as an arg to a method,
one of the reason being super can be abstract.!!
Here suppose if you call
myFunc(super)
and if the compiler doesn't restrict you then which method will it call?
How will it call abstract method?
Jeff Verdegan wrote:
Does super.m2() work for you??
No. Obviously it wont, compiler complains. That is what my argument says.
Jeff Verdegan wrote:And if super was a reference expression, it would almost certainly be legal. Your post is based on the assumption that there's a separate "super" object and "child" object. However, there's only one object, and it's a concrete class. If super were valid to pass as a method arg, it would refer to the same object as "this".
But though if it is considered as object it would refer to parent members..
So no question of relating "this" with "super".
Panagiotis Kalogeropoulos wrote:The println method in System.out requires a String as a parameter. The parameter that you provide is not a String, but a reserved word by Java (such as while, if, transient etc..). This explains the compilation problem. But if you want to evoke a method from a superclass, then you should use super.methodToCall(), where methodToCall is your method. Note that this method must not return void, as you will have again problems with the compiler.
gauravkv gupta wrote:
Panagiotis Kalogeropoulos wrote:The println method in System.out requires a String as a parameter. The parameter that you provide is not a String, but a reserved word by Java (such as while, if, transient etc..). This explains the compilation problem. But if you want to evoke a method from a superclass, then you should use super.methodToCall(), where methodToCall is your method. Note that this method must not return void, as you will have again problems with the compiler.
if println method take string as argument then println(this) should also produce error... but it absolutely fine.
The println method in System.out requires a String as a parameter.
gauravkv gupta wrote:
Panagiotis Kalogeropoulos wrote:The println method in System.out requires a String as a parameter. The parameter that you provide is not a String, but a reserved word by Java (such as while, if, transient etc..). This explains the compilation problem. But if you want to evoke a method from a superclass, then you should use super.methodToCall(), where methodToCall is your method. Note that this method must not return void, as you will have again problems with the compiler.
if println method take string as argument then println(this) should also produce error... but it absolutely fine.
Henry Wong wrote: Interesting debate, but moot.
It doesn't work because the JLS says so (or more correctly, doesn't say anything about it).
So implicitly anything that is a Primary can do all of those things. In the list of primaries is the this keyword which the JLS further explainsPrimary expressions include most of the simplest kinds of expressions, from which all others are constructed: literals, class literals, field accesses, method invocations, and array accesses.
When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked.
I guess less technical, based on all that stuff above, the this keyword is discussed as a keyword which denotes a value of a reference type while the super keyword does not.
gauravkv gupta wrote:hi all,
System.out.println(super); is showing compile time error for some reason. well i think this is supposed to print super class's toString();