• 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

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Marcus Green test 1 question 16, in the answer explanation he states "Static methods cannot be overriden but they can be overloaded." I thought they could be overridden. Can anyone explain?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It happens that the following compiles, but the meaning of "overriden method" doesn't apply here:
<pre>
public class StaticOverride
{
public static void foo(String s)
{
System.out.println(s);
}
}
class StaticOverride1 extends StaticOverride
{
public static void foo(String s)
{
System.out.println("In child class " + s);
}
}
</pre>
-VG
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not? It's seems to be perfect example of overriding. If you please explain it would be helpfull to me very much.. I am new to Java.
 
Vlad G
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, it happens to be more confusing than I thought but I still think I'm right. When you override a method you generally want polymorphism to work. This doesn't happen in case of "overriding" of static methods. Try to run these two examples:
1. "Overriden" static
<pre>
public class StaticOverride
{
public static void foo(String s)
{
System.out.println(s);
}
public void bar()
{
foo("some_string");
}
public static void main(String[] a)
{
StaticOverride so = new StaticOverride1();
so.bar();
}
}
class StaticOverride1 extends StaticOverride
{
public static void foo(String s)
{
System.out.println("In child class " + s);
}
}
</pre>
2. Overriden normal
<pre>
public class StaticOverride
{
public void foo(String s)
{
System.out.println(s);
}
public void bar()
{
foo("some_string");
}
public static void main(String[] a)
{
StaticOverride so = new StaticOverride1();
so.bar();
}
}
class StaticOverride1 extends StaticOverride
{
public void foo(String s)
{
System.out.println("In child class " + s);
}
}
</pre>
After you run it 1 should output:
some_string
and 2 should print:
In child class some_string
This proves that polymorphism doesn't work for "overriden" static methods. In other words, runtime type of an object doesn't define which method will be actually invoked in case of static "overriden" methods. Hopefully I didn't confuse you and myself. Other opinions are welcome.
--VG
[This message has been edited by Vlad G (edited January 03, 2001).]
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess it is just semantics. In this case what do we mean by override. Although I know we dont have polymorphism with static methods, my definition of override was that the method has the same signature as a method inherited from the base class. I guess Mr. Green has a different definition.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
Please see post http://www.javaranch.com/ubb/Forum24/HTML/005307.html ... made when a similar discussion arose in October.
Hope it helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
[This message has been edited by Jane Griscti (edited January 03, 2001).]
 
Ananda Kashyap
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
VG,
Thanks a lot.
You'r right.. I tried the codes and got the proof. I can say, we'r not confused.
- Anand
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Jane. Now I remember reading that in the JLS(I think) but I had forgotten.
 
It runs on an internal combustion engine. This ad does not:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic