• 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

static overriding

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Read that "Static methods cannot be overridden" But, the following code is working fine. Is this not the example of the static overriding???

class Test1
{
static void go()
{
System.out.println("Hello, go in super class");
}
}

public class Test extends Test1
{
public static void main(String [] args)
{
Test t = new Test();
t.cgo();
go();
}
static void go()
{
System.out.println("go overridden");
}
void cgo()
{
super.go();
}

}


Thanks in Advance.
[ March 28, 2005: Message edited by: gayathri ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct!! Static methods cannot be overrided but it can be hidden. Thats what happened in your code.

Rule verriding method in sub-class cannot narrow the access modifier.

Change to public in class Test for method go(). Like public static void go(). Still you can compile and get the output. Comment t.cgo(),like //t.cgo in your code, you will get a different result. You should call the static method in superclass only by qualified name such as superclass.method() or super.method() or superclassRefrence.method()

Hope it makes you clear.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Gayathri" and "sabeer sabeer" -
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Check this thread
 
gayathri mukkavilli
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got the point. Thanks
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Srinivasa Raghavan,
I didnot get this comment of yours:

"A subclass cannot override methods that are declared static in the superclass. A subclass can hide a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass."

how is this different from overriding?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rama
Static methods can only be hidden by having a static method of the same name in the subclass. You don't get the polymorphic behavior you get with overriding. Overriding implies dynamic binding , static methods do not participate in that. The above link mentioned in my previous post is an example for it. I hope this should be clear.
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
can i say that the calling the overridden method is based on the object type,
And calling the hidden method is depends on the refernce type.

any comments???
 
sabeer
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mark Spritzler,

Am using my real name as display name. So, am not confront with "JavaRanch naming policy". When I registered with "JavaRanch", by default, my display name is my email-id. But any how I changed to "sabeer".

Nothing to say.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Mark meant by having first name and last name in the display. I guess Sabeer is just your first name right ?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Parameswaran,
U r right


calling the overridden method is based on the object type,
And calling the hidden method is depends on the refernce type.

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am sorry but the above conversation has put confusion in me...what's the difference between method overriding and hiding? plz explain
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did u have a look at the whole post?. U will come to know whats the difference. Just go through it.
And any confusion just post it.
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't get it either. What is method hiding vs overriding?

Also, from the above example, I understand that static methods don't override but the parent's method runs. So, Why are we writting a method with the same signature in the child?
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote some code to get better understanding on this. Hope this would help somebody:

Without static:




With Static







with static methods, the polymorphic behaviour doesn't work.
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rama,
Let me c if i can help.
1. Diff btw hidding and overriding.
Ans.
Overriddind has to do with inheritance.
Some code to show overriding.
class OverTest {
void doStuff() {
System.out.println("super doStuff")
}
}

class TryOver extends OverTest {
void doStuff() {
System.out.println("sub doStuff");
}
public static void main(String [] args) {
OverTest t = new TryOver(); // class is used
t.doStuff(); // prints sub doStuff
}
}
The version to run is chosen at runtime. wao
2. Hidding
It means a veriable or method inherited has been over shadowed
by the subclass.(confused).
so, with static methods,
the super class version is not inherited but just be redeclared.
and if you must redeclare a static method, it must be indicated
else you get compiler error, that you can't override it.
Now the code.
class OverTest {
static void doStuff() {
System.out.println("super doStuff")
}
}

class TryOver extends OverTest {
static void doStuff() {
System.out.println("sub doStuff");
}
public static void main(String [] args) {
OverTest t = new TryOver(); // Reference is used
t.doStuff(); // prints super doStuff
}
}
remove the static from the subclass doStuff()
the compiler give a glommy face.
Hope it helps.

"It is better to bleed more in peace time, so that you sweetless
during war"




:roll:
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic