Forums Register Login

system.out.println(super) is showing compile time error. why?

+Pie Number of slices to send: Send
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();

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 call

myFunc(super)

and if the compiler doesn't restrict you then which method will it call?
How will it call abstract method?

Thus preventing to violate OOP concept compiler prevents you to do that.
1
+Pie Number of slices to send: Send
 

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 call

myFunc(super)

and if the compiler doesn't restrict you then which method will it call?
How will it call abstract method?

Thus preventing to violate OOP concept compiler prevents you to do that.



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.
+Pie Number of slices to send: Send
 

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??
+Pie Number of slices to send: Send
 

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();



Dear gaurav,
i think, first have a look at the println method.

So at first in your code super is looking like a refrence variable (and super is also a reserved keyword in java so you can't use it as an identifier).
Second thing is if you use any other valid identifier in place of super then you have to declare its type, as the parent type.
After that i think there will be no problem.
1
+Pie Number of slices to send: Send
 

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?



Because that's what we have to pass to that method, and super isn't one of those, so it's not legal.

Don't you find the answer to that in my post??



Your post was mistaken.

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".


If your argument were true, then the following would not be legal:


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.
+Pie Number of slices to send: Send
 

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.!!


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".



super always refers to the members of the parent class.
I agree its not an object. But though if it is considered as object it would refer to parent members..
So no question of relating "this" with "super".
+Pie Number of slices to send: Send
 

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,



Correct.

one of the reason being super can be abstract.!!



No, it cannot. As already stated: 1) super is not a reference to an object, so being abstract or not is not applicable, and 2) There is only one object, and it is concrete, so if super did refer to anything, the only thing is would make sense to refer to is that object.

The one and only reason we cannot pass super as an arg is that it is not an expression of reference type. Anything else is just meaningless speculation (on both my part and yours) about what things "would" be like if the language were different in some hypothetical fashion.

Additionally, you said:


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?



This suggests that the reason that you can't pass super as an arg is because the superclass is abstract. That is not the reason why we can't pass super. We wouldn't be able to pass super even if it were concrete, so your implied reasoning is incorrect.

Additionally, mFunc() is not an abstract method. It is a concrete method on the subclass, and regardless of what we pass as the arg, it is the concrete subclass's concrete implementation of myFunc() that is called.


Jeff Verdegan wrote:



Does super.m2() work for you??



Of course not. But that's totally irrelevant to my point.

No. Obviously it wont, compiler complains. That is what my argument says.



No. Your argument was not about calling an abstract method of super. Your argument was about passing super as a parameter.


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".



What it would refer to if it were a reference is all a matter of speculation. However, your speculation requires the notion of a separation into a "super object" and a "sub object", and that separation does not exist in Java. Therefore, it seems most reasonable to me that if, hypothetically, super did refer to an object, it would refer to the same one as this--the only object that is present.

This part isn't really relevant though.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
No error is produced because when you use "this", you actually pass the current object to println(). And when you use an object in println (instead of a String) the toString() method of that object is called.
+Pie Number of slices to send: Send
 

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.



Yes, it is. That's because the statement

The println method in System.out requires a String as a parameter.



is not correct. System.out is a PrintStream object, and if you check the API documentation for PrintStream you'll see a long list of overloaded println() methods, including a println(Object) method. So "println(this)" is perfectly okay.
+Pie Number of slices to send: Send
 

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.




While this discussion is interesting and all, it is all moot. The fact is .... it doesn't work because there is no specification for it in the Java Language Specification. Period.


This discussion seem to have gone towards ... it should work like "this", which the JLS defines as the current object, except that "super" should be the current object, but with some newly defined feature (property) that the JVM should keep track of (in that to only use the super component of the current object). Interesting debate, but moot. It doesn't work because the JLS says so (or more correctly, doesn't say anything about it).

Henry
+Pie Number of slices to send: Send
 

Henry Wong wrote: Interesting debate, but moot.



Thank you.

It doesn't work because the JLS says so (or more correctly, doesn't say anything about it).



Although it almost certainly doesn't say, "You can't pass super as an arg," I'm sure that it does say exactly what super is, and exactly where and how we can use it, and exactly what can be passed as a method parameter, and that there is no overlap between the two. So in that sense, the JLS explicitly prohibits it.
+Pie Number of slices to send: Send
Just to post links to the pertinent portions of the JLS:

The this keyword is section 15.8.3. It is under section 15.8: Primary Expressions, which, as a quick definition states:

Primary 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.

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 explains

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.



Noticeably absent from section 15.8 is the super keyword which means it isn't a Primary. The super keyword is described in section 15.11.2, under 15.11: Field Access Expressions, and is also mentioned in 15.12: Method Invocation Expressions and is discussed only in terms of access to fields or methods. There is no discussion of the super keyword being used as a primary, or as denoting a value of any sort.

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.
+Pie Number of slices to send: Send
 

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.


The only point to separate the discussion favors. "this" is a reference variable inside instance methods and constructors, while "super" is never and just used to access super class members and hidden fields (!).
+Pie Number of slices to send: Send
 

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();



See JAVA API Documentation to find out the overloaded versions of println method.
void println(Object obj)
|-> Prints an object.
void println(String s)
|-> Prints a string.
among others.

"super" is a reserved keyword that by no way is a reference to any instance/ object. But "this" reserved keyword is. So println(super) leads to CE: Cannot find symbol. While println(this) invokes the println() method that has Object type argument, so its okay. I hope that resolves the dispute.
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1275 times.
Similar Threads
why is this code showing compile time error?
Inner class having a private constructor
exception getting while running a Hibernate program
Overiding problem using private
compiler error
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:40:34.