• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Static methods and variables.

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Que 1) How does overloading and overriding work in case of static methods and variables ?
Que 2) Can Private methods be overloaded and overriden? What is "shadowing" of methods? Somebody used this term while we were discussing some java issues, is this a valid term and if yes then what does it mean ?
------------------
http://www.mantrotech.com
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer to question no.2
Private methods can be overloaded, but not overridden.
Methods cannot be overridden to have less accessibility.
For eg:- we can override a public method to be private, but not vice versa.
Regards
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer to question 1
You can overload static methods since an overloaded method is really a different method with the same name. But since a method is static (belonging to the class and not to a particular instance) you cannot override it.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if a method is static than overridden method should also be static. Thats what I read somewhere. Is that correct?
 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes static method can be overriden by another static method only.
Look at the following code
class Base
{
static void PrintString()
{
System.out.println("Base Static Method");
}
}
public class Test extends Base
{
public static void main(String argv[])
{
Test objTest = new Test();
Base objBase = objTest;
}
void PrintString()
{
System.out.println("Test Static Method");
}
}

It gave me following compiler error
ERROR:
The instance method void PrintString() declared in class Test cannot override the static method of the same signature declared in class Base. It is illegal to override a static method.
Last sentence in Error message seems confusing as static methods can be overridden as long as overriding method is also static.
------------------
http://www.mantrotech.com
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gaurav, that is not true. You cannot override a static method. If you try to override it with a non-static method, you get a compile time error. If you try to override it with another static method, it compiles and runs fine, but it is not overriding. It is hiding.
The best way to understand this is to think what happens when you override a method. If you have a Parent class name A and a child class that extends A named B, then you create an instance like this:
A a = new B();
perfectly legal and used all the time. Then when you call a method like a.aMethod();, if it is an overridden method the body from class B() runs because of late-binding. But with a static method, you get don't get the method body of B() but of A() because it is not overriding the method, it is hiding it.
Try it out with a sample and see what you get.
Bill
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are not overridden. They are shadowed or hidden. Static methods are resolved at compile time based on the type of the reference variable(which class it is?) and are not subject to late binding(which object it is?).
Private methods are also not overridden, since they are not inherited.
HTH
------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods that are overridden in the derived class do not exhibit polymorphic behavior. The compiler will allow it, but at runtime you cannot invoke the override method in the derived class using a reference from the base class.

Output is:
Base.foo()
Derived.bar()
-Peter

[This message has been edited by Peter Tran (edited February 07, 2001).]
 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bill bozeman:

The best way to understand this is to think what happens when you override a method. If you have a Parent class name A and a child class that extends A named B, then you create an instance like this:
A a = new B();
perfectly legal and used all the time. Then when you call a method like a.aMethod();, if it is an overridden method the body from class B() runs because of late-binding. But with a static method, you get don't get the method body of B() but of A() because it is not overriding the method, it is hiding it.


Hi Bill!
Theoratically I agree to you and that's what I think too. But I don't have enough information to prove the same. I had a debate with a good friend of mine that static methods can't be overridden. Can you provide information in terms of object layout in memory (regd static and non static methods and what happens when we override a method). I know little bit about virtual tables in C++, how does java handle that ?
The following code supports the fact that static methods can't be overridden

Output of the above code
Test Static Method
Base Static Method
Test Method
Test Method
best regards
Gaurav Mantro

[This message has been edited by Gaurav Mantro (edited February 07, 2001).]
[This message has been edited by Gaurav Mantro (edited February 07, 2001).]
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JLS:


8.4.6.1 Overriding (by Instance Methods)
An instance method m1 declared in a class C overrides another method with the same signature, m2, declared in class A if both
1. C is a subclass of A.
2. Either
* m2 is non-private and accessible from C, or
* m1 overrides a method m3, m3 distinct from m1, m3 distinct from m2, such that m3 overrides m2.


What the JLS does say about static methods with the same signature in both Base and Derived class is:


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.


-Peter
[This message has been edited by Peter Tran (edited February 07, 2001).]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I complied Gaurav's program and it is ok that static method is not overridden but what about static variable. Look the following example and run it and static variable is overridden.
I don't understand why this is the difference because both of them are class variable and class method.
class Testthis{
boolean avariable;
static public int i=5;
Testthis(){}; // Constructor 1.

Testthis(boolean avariable, int i){ // Constructor 2.
this.avariable=avariable;
this.i=i; // assigning to the member class/static variable i.

}
}

public class Testoverride extends Testthis{
public static void main( String argv[] ){
Testthis th=new Testthis( true,10 ); // used constructor
// no.2 of superclass.
System.out.println(th.avariable); // avariable is an
// instance variable
// of class Testthis
// super class
System.out.println(th.i);
System.out.println(Testthis.i);

}
}
Output is:
true
10
10
- Golam Newaz

------------------
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi golam ,
I couldnt find any overriding in your code.
u r getting the same output in both the calls because u r assigning 10 to the variable in the constructor.
I didnt get u r doubt
Cherry
 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, Golam you need to modify your code to test overriding for variables.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic