aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static overriding" Watch "static overriding" New topic
Author

static overriding

gayathri mukkavilli
Greenhorn

Joined: Mar 09, 2005
Posts: 20
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 ]
sabeer
Ranch Hand

Joined: Mar 15, 2005
Posts: 41
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.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Hi Check this thread


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
gayathri mukkavilli
Greenhorn

Joined: Mar 09, 2005
Posts: 20
Got the point. Thanks
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
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

Joined: Sep 28, 2004
Posts: 1228
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.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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

Joined: Mar 15, 2005
Posts: 41
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.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
I think Mark meant by having first name and last name in the display. I guess Sabeer is just your first name right ?


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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.
Sajid Moinuddin
Ranch Hand

Joined: Mar 19, 2005
Posts: 85
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

Joined: Jul 19, 2004
Posts: 298
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

Joined: Mar 04, 2005
Posts: 91
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

Joined: Mar 04, 2005
Posts: 91
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.
MI Mohammed
Ranch Hand

Joined: Feb 16, 2005
Posts: 146
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:


SCJA(Beta) SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCBCD 5.0 beta <br />The more practice we get, the better we are at the exams and in life in general. Pls join me at My DEN.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static overriding
 
Similar Threads
Inner class constructors and overriding
Static overriding
doubt
inner class
non static method override