Tell the difficulties that i am difficult.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:There is nothing wrong with Java
You can call a static method on an instance of a class although its not recommended. Due to this reason you can call a static method on this or super references. You are right that static methods are inherited but they can't be overridden...
Tell the difficulties that i am difficult.
Matthew Brown wrote:You seem to be blaming Java for the fact that you aren't using the correct approach, which is Car.startCar().
It's true that it's a bit unfortunate that Java allows you to call static methods on an object reference, when you should never do that. If they were designing this feature now I suspect it would be changed, but this dates back to Java 1.0 and it would be too big an impact to change it now (because of the number of people who have already written code doing it wrong ).
i know that super keyword is used to call overridden method version of superclass but as universal truth we can't override static method then why it is working here, what is the wrong with java ?
saloni jhanwar wrote:
Ankit Garg wrote:There is nothing wrong with Java
You can call a static method on an instance of a class although its not recommended. Due to this reason you can call a static method on this or super references. You are right that static methods are inherited but they can't be overridden...
Oh come on ,java gives us lecture that make your classes cohesive then what is java doing ?, super is used to call overridden method, where as in context of static method there is no overriding then why anyone like to use super here ? why don't we use directly startCar() or this.startCar() or obj.startCar().It doesn't give fake impression of overriding in static context ? [super.startCar()]
saloni jhanwar wrote:Thanks Matthew, Can you tell me mystery of super keyword.
raj malhotra wrote:
i know that super keyword is used to call overridden method version of superclass but as universal truth we can't override static method then why it is working here, what is the wrong with java ?
It is not possible to override static method. But it is possible to hide the static method by defining another method with the same signature in the subclass. So actually we can call the supper class static method by invoking SUPER_CLASS.staticMethod() or superClassINstance.staticMethod() or super.staticMethod() in subclass.
Even if you write superClassINstance.staticMethod() , compiler will change it to SUPER_CLASS.staticMethod().
Thanks,
Raj
Tell the difficulties that i am difficult.
saloni jhanwar wrote:That is called redefining method not hiding.
Arunkumar Chinnadurai wrote:
saloni jhanwar wrote:
Ankit Garg wrote:There is nothing wrong with Java
You can call a static method on an instance of a class although its not recommended. Due to this reason you can call a static method on this or super references. You are right that static methods are inherited but they can't be overridden...
Oh come on ,java gives us lecture that make your classes cohesive then what is java doing ?, super is used to call overridden method, where as in context of static method there is no overriding then why anyone like to use super here ? why don't we use directly startCar() or this.startCar() or obj.startCar().It doesn't give fake impression of overriding in static context ? [super.startCar()]
ok i have one doubt on this thread., without static keyword what will happen in java..?
your question is good but you should analyse all the face ok.
I just want to access a method in class one . so i created that as static . i think you know very well about static , before instance creation itself static block will load,
or if you want to override mean please be that method as normal only.
Tell the difficulties that i am difficult.
Matthew Brown wrote:
saloni jhanwar wrote:Thanks Matthew, Can you tell me mystery of super keyword.
Well, as I said, it's (unfortunately) legal to call a static method on a reference of that class. Within the Limo class, super is effectively a reference to the Car class. So allowing the calling of a static method on it is just being consistent.
Matthew Brown wrote:
saloni jhanwar wrote:That is called redefining method not hiding.
No, it's definitely hiding.
The super-class version can still be called, using the class name. But calling it just by name in the subclass will call the subclass version, as the superclass version is now "hidden".
Tell the difficulties that i am difficult.
Consider Paul's rocket mass heater. |