wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Overriding static functions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overriding static functions" Watch "Overriding static functions" New topic
Author

Overriding static functions

Satish Kumar
Greenhorn

Joined: Feb 18, 2005
Posts: 23
I dont understand , why java doesn't support these 2 cases

CASE 1: Overriding static function of super class . Here is the example :
-----------------------------------------------------------------
class A
{
static void fun1()
{
}
};
class B extends A
{
void fun1()
{
}
};
---------------------------------------------------------------------

CASE 2: Overriding non-static function of super class as static function

--------------------------------------------------------------------------
class A
{
void fun1()
{
}
};
class B extends A
{
static void fun1()
{
}
};
------------------------------------------------------------------

and finally wen i try to override static function , of super class as static function in subclass , i dont get any error . Here is the code
-----------------------------------------------------------
class A
{
static void fun1()
{
}
};
class B extends A
{
static void fun1()
{
}
};
------------------------------------------------------------------------------- ----------------

Can any 1 plzz clear this ambiguity .
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
You can only override a method when its signature (which includes its return type and whether or not it is static) is the same as the one in the superclass.

I guess this is to avoid ambiguity. I mean if a method needs to be static in one class and then non-static in a sub-class, it is unlikely that they have similar functions and therefore should not have the same name.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Please note that the signature comprises the method name and parameter list. The modifiers, return type and throws clause do not form any part of the signature.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Hi, Java Monster

Welcome to JavaRanch! Please adjust your display name to meet the JavaRanch Naming Policy.
You can change it here.

Thanks!


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
Hi java monster
See what is basically a static function...the one which cannot have an access to private members of the class .This function only deals with the static members which are common to all the objects of that class.So if B inherits from A and you create an object of B then object of b is also an object of A ie all the class members an all the class functions of class A also apply to Class B ...So if class A has an static function which means that has to be applied for all the objects of A then how can you override this function without being static ....this means that you are modifying a property (static property of the function) of class A.You can override a function meaning you can change its implemetation but not the property(signature) associated with it.
ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
hi Mithun
your POST Says
.See what is basically a static function...the one which cannot have an access to private members of the class .

see my code

public class A{
private static int i;<<<<<<<-----private member
public static void main(String [] arg)<<<<<----used by static method
{A a= new A();
System.out.println("value of i "+A.i);


}

}
Satish Kumar
Greenhorn

Joined: Feb 18, 2005
Posts: 23
@ Marilyn de Queiroz - > Hey i changed my name , now its my real 1--- LOL

@ archies - > hmm ! thanks a lot , so we can conclude that JAVA doesn's support OVERRIDING of static functions . And in 3rd case OVERRIDING doesn't happen .
Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
Hi ganesh
Thanks for correcting me but what i meant was ....static functions cannot have access to a non static members.
class A
{
private int var;
public static void foo()
{

var=20;//Error trying to access a non static member in a static function
}
public static void main(String a[])
{new A().foo();
}
}
So what i meant was you cannot have access to a non static memebr from a static function....Because static functions are used to deal with static members of the class.(although you can access static members from a non static functions too).What happens is take for example the Maths class if you want a square of a number, the class members need not be set for this you can directly have something being performed locally and then send the result ..
Class Maths
{
int a;
public static int square(int num)
{
return (num*num);
}
}
Now there is no point in making this square function a non static function because it wont change the property of the object who has called it.So basically static functions act upon whole of the class and not individual objects calling them.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

...so we can conclude that JAVA doesn's support OVERRIDING of static functions.
That is correct. Static methods cannot be overridden.

�8.4.6.2 Hiding (by Class Methods): If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by archies:
what i meant was ....static functions cannot have access to a non static members.


First: please change your display name -- you've been asked multiple times now.

In any case: this is also not strictly true. Static methods can indeed access instance members -- they of course need to specify the object whose member is of interest:


[Jess in Action][AskingGoodQuestions]
Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
hi Ernest
The object you have created is an local object where as i was talking about the reference of the calling object ,
class A
{
int var1;

public static foo(A acopy)
{
var1=10;//Error as non static memeber refered by the calling object
acopy.var1=10;//Perfectly valid as object is available locally not via calling reference
}
public static void main(Strign argv[])
{
A a1=new A();
A a2=new A();

a1.foo(a2);


}
}
I hope its clear now....
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"archies_018",
Sorry, but your display name is still invalid. We require display names to be two words: your first name, a space, then your last name. You originally had a valid display name (before changing it to 'archies') and we want you to change it back.

thanks,
Dave.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding static functions