Author Topic:   difference between static and final method
Linda Xu
greenhorn
posted February 14, 2000 09:55 AM         
What's the difference between static method and final method? Neither of them can be overridden by subclass. Why we need two kinds fo method have the same character?

NewJava2User
unregistered
posted February 14, 2000 10:05 AM           
Static methods can be overriden, but they cannot be overriden to be non-static,Whereas final methods cannot be overriden.

Lucy C
ranch hand
posted February 14, 2000 10:12 AM             
Static and final methods are very different things! A static method is a method that's invoked through a class, rather than a specific object of that class. Static methods can only access static variables - they can't use anything that's specific to a particular object. Nonstatic methods (or instance methods) must be called on a specific object and can use the object's instance data.

A final method is just a method that cannot be overridden - while static methods are implicitly final, you might also want to create an final instance method.

Lucy C
ranch hand
posted February 14, 2000 10:54 AM             
Static methods can be overriden

Um, no, they 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 if you hide a static method - for example:

class Base {

public static void aMethod(){
System.out.println ("Base aMethod()");
}
}

public class Baselet extends Base {

public static void aMethod (){
System.out.println ("Baselet aMethod()");
}

public static void main (String [] args){

Base theBase = new Baselet();
theBase.aMethod();

}
}


..if you run this, you get the output from the Base version of aMethod, not the Baselet version, even though the object is a Baselet.

Udayan
unregistered
posted February 15, 2000 06:30 PM           
Static methods can be overridden ,but the overriding method cannot be non static. Actually,the method in the subclass hides the one in the superclass. If not overridden, Parent.aMethod() and Child.aMethod() refer to the same method.
Feel free to correct me if I am wrong.

Tony Alicea
sheriff
posted February 15, 2000 06:59 PM             
What happens is that the term override does not have meaning for static methods in Java. Overriding implies dynamic binding and static methods do not participate in that type of binding. They are statically bound at compile type. As Lucy said, they can be hidden instead.

art
unregistered
posted March 19, 2000 09:28 AM           
Which of the following statements are true?

1) static methods do not have access to the implicit variable called this
2) a static method may not be overriden
3) a static method may not be overriden to be non-static
4) a static method may not be overloaded

My answer is 1, 2 and 3. But 2 is not listed. Why? I think static methods cannot be overridden only hidden by the subclass. What should be my choices for the question from exam point of view. Can someone tell me.

Jim Yingst
sheriff
posted March 19, 2000 11:31 AM             
Art- the author of this question is wrong, 2 should be an answer. I suspect that this particular question is too subtle for the actual exam anyway.

Matt Claflin
greenhorn
posted April 17, 2000 10:05 AM             
Hi. My question with regards to the code above then, is, "why would one ever want to hide a static method in a superclass?". If creating an instance of the subclass Baselet{} and subsequently invoking the static method aMethod() causes execution of aMethod() in the superclass Base{}, why would one want to create the aMethod() method in Baselet{}? Perhaps I'm just not seeing how the aMethod() method in
Baselet{} would ever be executed.

-Matt

Matt Claflin
greenhorn
posted April 17, 2000 10:14 AM             
Ahhh - never mind. I see it. The choice of static method depends on the class of the reference variable which calls it.

-Matt

maha anna
bartender
posted April 17, 2000 10:53 AM             
Hello all,
Very good flow of discussion. Here is some more thought.
The main point to note here is final keyword for methods is an EXTRA attribute or EXTRA nature of that method THis means both static and instance level can have 'final ' as an ADD_ON tag. static and final are are not connected. That is we SHOULD NOT say both static and final give the same functionality. static/instance level access is different and final functionality is different. 'static' means the method is OWNED by ALL objects created by from that class.
The 'final' comes into picture whether the subclasses can overrid or hide whatever. It stops there. When you put final to a static level method it means it is STILL static and the subclasses CAN'T EVEN hide. Which means the subclass can't have it's own version of static behaviour by hiding it. Simillarly for instance methods. It is an extra rectriction put on it. By adding the 'final' the superclass says ' Hello ! All my decenders! IF you want you have my method as it is! All just leave it. You CAN'T override or hide my rules. Ok.!

So we should not mix-up the final and static behaviours. Try this.



class Parent {
static void staticMethod() {}
static final void staticFinalMethod() {}

void instanceMethod() {}
final void instanceFinalMethod() {}

}
class Child1 extends Parent {

static void staticMethod() {}
static final void staticFinalMethod() {} // You can't hide like this.
//You violate the law

void instanceMethod() {}
final void instanceFinalMethod() {}
//You can't override like this

}

regds
maha anna

[This message has been edited by maha anna (edited April 17, 2000).]

girish
greenhorn
posted April 19, 2000 05:04 AM             
Maha- but the question still remains can static methods be overridden.

I think the answer to Art's question should be 1 & 3.
Actually I am confused .Please give detailed explaination as always.

rgds
Girish

maha anna
bartender
posted April 19, 2000 07:53 AM             
Girish,
I think the qstn by art is already answered by Jim. Please read Jim's post in replying for Art's post context.

Beleive me. I started answering your post in detail. Then It struck me why am I doing this? Does Girish just wants the answer. No, he has to give his explanation first. Then I can reply for him like that. Do you get Maha's idea? Let us discuss first. Then the answers come automatically. Please take it in the right sense. What's your opinion about Art's qstn?

regds
maha anna

girish
greenhorn
posted April 20, 2000 01:28 AM             
Sorry Anna I broke the tradition.
Ok let us discuss.

According to Jims post , the statement

Static methods may not be overridden is true.

But your code:

/////////////////////////////////
class Parent {
static void staticMethod() {}
static final void staticFinalMethod() {}
void instanceMethod() {}
final void instanceFinalMethod() {}
}

class Child1 extends Parent {
static void staticMethod() {}
static final void staticFinalMethod() {} // You can't hide like this.
//You violate the law
void instanceMethod() {}
final void instanceFinalMethod() {}
//You can't override like this
}

///////////////////////////////////

Here what is the statement:

"static void staticMethod() {}"

in Child1? Is it overriding or hiding.


Morever the compiler error like:

"Cannot override static methods with instance methods "

doesn't it suggest you can override static methods with only static methods.

Actually what I mean is - Is overriding not a subset of hiding.

Hope u understand the question.I think i have messed up many things.

Regards
Girish

maha anna
bartender
posted April 20, 2000 09:39 AM             
Girish,
Now I know what you want.
Overriding a method means what? When you create an object from the subclass which does the overriding, the re-defined (new) behaviour must be kept intact when the method is called on this object right? In other words , there is a base class and it has a method say, void m1() which prints "I like Java". And there is a subclass which is derived from this parent, and it re-defines the beahviour of this inherited method from base class and say, it prints "No I don't like Java". Also lets assume that this method is instance method. So when you create a physical object using Base b = new Base(); and call b.method(), what it will say. Obviously it is real base class object and you would expect it to say "I like Java" Right? Simillarly when you create like this Subclass s = new SubClass(); and call s.method(), it will behave itself and says "I don't like java". Now lets say we do like this Base b = new Subclass(); Now what do u expect? Since this method is instance method, which is overriden by the subclass, eventhough you see through a mirror and see it's another face of Base class, in case of overriding a method, the true face of the overriden method only will be seen. This is oop. Now also it will say "No I don't like Java';

In order to analyze the static concept, let us take the same case as described above. Let's assume Both Base class and subclass has their own version of static void method(); This means what? Each class as a independent class of its own wants to give a COMMON behaviour for all objects created from them. This means when the subclass redefines the same static void method1(), it DOES NOT OVERRIDE. It hides. In the sense, If you apply the same 2 tests as shown above Base b = new Base(); b.method() WILL call Base class version. Subclass s = new Subclass(); s.method() WILL call subclass version. And you know waht? Base b = new Subclass() WILL NOT CALL SUBCLASS VERSION. IT WILL CALL THE BASE CLASS VERSION of the static method. Seeing a subclass object through a base class mirror and when we see a static method, the MIRROR type's static only will be seen. So the mirror beahves differently.

So do you get the point? If the static method WOULD HAVE BEEN OVERRIDEN we MUST see the subclass version right? Then why we see the static base class method? So static method are NOT OVERRIDEN.

For the compiler part, it does say anything about the 'hiding' IT throw s the same error for both instance and static methods. It DOES NOT DISTINGUISH between hiding and overring when it throws the error But it does its work fine.
regds
maha anna

[This message has been edited by maha anna (edited April 20, 2000).]

girish
greenhorn
posted April 20, 2000 10:29 PM             
Thanks Anna ,
That really clears my doubt.So static methods are not overridden.
Thanks once again .
Looking forward for same cooperation ahead.

Regards
Girish

maha anna
bartender
posted April 23, 2000 07:17 PM             
Girish,
Did you really read whole of Maha's above post?.
While writing I did't know this is this big. It didn't put you to sleep right?
regds
maha anna

Nalini Mistry
ranch hand
posted April 25, 2000 02:22 AM             
Reading maha's explanation of instance and static "like java dont like java" explanation does this hold true : in case of static methods that are hidden by the subclass, the static method called is always the one from the reference variable class, not the object class?? what if we take it a level or two further down the inheritance heirarchy chain?? 3-4 subclasses hiding the same static method?? if a 2nd tier class is used as the referencing variable for a 4th tier object and the static method is called which one executes??

maha anna
bartender
posted April 25, 2000 06:28 AM             
Nalini,
If I understand you correctly, by the word tier you mean the depth of the inheritance hirearchy right? ( tier is used in server side processing as different meaning.

When you say



BaseClass
+SubClass1
+SubClass2
+SubClass3
+SubClass4


Subclass2 refVar = new Subclass4();

After the above statement, when you call refVar.someStaticMethod(), the one which is defined in SubClass2 will be called.

regds
maha anna

|