• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt in static methods..  RSS feed

 
s.palanivel rajan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we override static methods?

 
Albareto McKenzie
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • 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
  • 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: 304
  • Mark post as helpful
  • send pies
  • 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
  • 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: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the answer in the link I posted before.
 
swapnl patil
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note your code is much harder to read unless you use the CODE button.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!