• 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

doubt in static methods..

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we override static methods?

 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it make sense? How do you access a static method? Have you tried?
 
s.palanivel rajan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually we can access static methods by using the class name..i know that if we write the code as below what will happens..

class rajan
{
public static void sum()
{
System.out.println("i am base class");
}
}
class erode extends rajan
{
public static void sum()
{
System.out.println("i am derived class");
}
}
class rajanerode
{
public static void main(String args[])
{
erode e=new erode();
e.sum();
}
}
 
Albareto McKenzie
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But have you tried?? What do you deduce from the result?

Otherwise take a look at this: http://faq.javaranch.com/view?OverridingVsHiding
 
s.palanivel rajan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually what will be the output for my code because i cant try here..sorry..please give me the output..
 
Albareto McKenzie
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have the answer in the link I posted before.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for this program

class rajan
{
public static void sum()
{
System.out.println("i am base class");
}
}
class erode extends rajan
{
public static void sum()
{
System.out.println("i am derived class");
}
}
class rajanerode
{
public static void main(String args[])
{
erode e=new erode();
e.sum();
}
}

method from class erode will get called .

A static method cannot override an inherited instance method, but it can hide a static method if the exact requirements for overriding
instance methods are fulfilled. A hidden superclass static method is not inherited. The compiler will flag an error if the signatures are the
same but the other requirements regarding return type, throws clause, and accessibility are not met. If the signatures are different, the
method name is overloaded, not hidden.

let me know if you have any quires.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note your code is much harder to read unless you use the CODE button.
 
reply
    Bookmark Topic Watch Topic
  • New Topic