• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confusion in Method Overriding.

 
Ranch Hand
Posts: 103
jQuery Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

In the code displayed below,




I know that there is no concept of Static method overriding.
But then why does Line No.15 is showing and error.

The Error is

C.java:15: add() in B cannot override add() in A; overridden method is static
public void add(){
^
1 error


Please tell me , what is happening?


Thanks in advance.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method having same name in the subclass must either be legally Overrided on Overloaded.When you gave same method name(add()) in the subclass without changing the argument list its checked for "Leagal Overriding".ofcource it fails because Its not a valid override for a static method.if you place static in subclass's method add it would compile.You may even change the argument list in subclass version of add (That would be valid overload)

Valid Override



Valid Overload

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Chetan Singh

A static method in a subclass, with the same signature as a static superclass method, is neither overloaded nor overridden. It is hidden. See this FAQ, no 16.
 
Prash Singh
Ranch Hand
Posts: 103
jQuery Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
thanks for reply

but I am still no where near to get the idea

Please explain.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot override a static method. Period.

You can define a new static method with the exact same signature in the subclass, but that is hiding, as mentioned in the link Campbell gave you.
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thumb rule is that, you cannot override a static method in the child class.
You can write the same signature that looks like it is overriding in the child class but actually it is not doing that. Compiler will compile the code correctly.
JVM is actually hidding the static method in child class and calling the static method in the parent class.

see this











Output:-

Class B: start()-static
Class B: stop()-non-static
Class A: start()-static
Class B: stop()-non-static
 
Chetan Singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijaay you are calling
# A a = new B();
# a.start();
# a.stop();
'a' being a reference variable of Class A is bound to call static method of class A.so it dose not make its Point of hiding clear


There Seems To Be No Hiding Here.


Output
D:\Development\Java>java C
A.add() :::: Add in A
B.add() :::: Add in B
 
Vinod Vinu
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chetan and others....even i'm doubtful now.

i have created two different classes.

MethodOverriding.java



ChildClass.java



Output:-

Main method
Child class: start method
Child class: Stop method
Child class: start method
Parent class: Start method
Parent class: Stop method
Parent class: Start method
Parent class: Start method
Child class: Stop method


Now my doubt is when i created the objects of both parent and child classes normally, it worked fine as desired. But when i created the object of parent class by making a call to the child constructor i.e.

MethodOverriding mo = new ChildClass();
mo.start();
mo.stop();


the output came is

Parent class: Start method
Child class: Stop method


Why this time it called the stop method of child class when i have created the object of parent class........please explain

Thanks in advance for your precious time
 
Chetan Singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijay Static methods unlike normal methods are not object specific .
So saying ..........

MethodOverriding mo = new ChildClass();
mo.start();
mo.stop();

Is similar to saying MethodOverriding.start();


mo.start() for compiler is not linked to any object so new ChildClass() is actually ignored by the compiler.

mo.start() is just another way of writing MethodOverriding.start()



BUT THE ORIGNAL QUERY IS STILL IS NOT CLEAR.
 
Chetan Singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Ritchie i read the FAQ and i think .A static method name in subclass having same signature as that of super class
is just an "Override Look Alike". for true override the Object itself must decide which version of the method to invoke (not the reference variable for that Object).

So while the Look alike claims to be a superstar it actually is a ......... LookAlike
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hahaha
 
Vinod Vinu
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hahaha

whats that Campbell Ritchie, there is nothing to laugh......
 
Prash Singh
Ranch Hand
Posts: 103
jQuery Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Every body for your answers, that was quite helpful.

I understand that there is no concept of Static Method Overriding.
But what I want to know is that, why am I not allowed to have same name as the static method in my
child class.

Please help me on this issue.

Thanks
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinod Vijay wrote: . . . whats that Campbell Ritchie, . . .

It's about the "lookalike".
 
Chetan Singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Question : why am I not allowed to have same name as the static method in my child class.



Correct me if i am wrong .If the above code were to compile then at Runtime JVM would say "Well I have two choices for method hot() and I could not turn down any choice(because they are both hot). parentReference.hot(); is a valid way to execute both static method of class Parent and normal method of class Child.

The situation is like You have a room in which a superGeek(JVM ) have to choose between two similar Hot girls(ex phoebe and ursala).Compiler cannot let JVM have them so it wont compile.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic