aspose file tools*
The moose likes Beginning Java and the fly likes Confusion in Method Overriding. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confusion in Method Overriding." Watch "Confusion in Method Overriding." New topic
Author

Confusion in Method Overriding.

Prash Singh
Ranch Hand

Joined: Aug 23, 2009
Posts: 102

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.

Chetan Singh
Greenhorn

Joined: Sep 27, 2010
Posts: 5
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

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Aug 23, 2009
Posts: 102

Hi ,
thanks for reply

but I am still no where near to get the idea

Please explain.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
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


Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Chetan Singh
Greenhorn

Joined: Sep 27, 2010
Posts: 5
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

Joined: Aug 30, 2009
Posts: 217
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

Joined: Sep 27, 2010
Posts: 5
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

Joined: Sep 27, 2010
Posts: 5
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
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Hahaha
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
Hahaha
whats that Campbell Ritchie, there is nothing to laugh......
Prash Singh
Ranch Hand

Joined: Aug 23, 2009
Posts: 102

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
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Vinod Vijay wrote: . . . whats that Campbell Ritchie, . . .
It's about the "lookalike".
Chetan Singh
Greenhorn

Joined: Sep 27, 2010
Posts: 5
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion in Method Overriding.
 
Similar Threads
simple........
Overriding for Static Method
is static methods can't override?
Read file from directory, update contents of the each file
generics doubt