• 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 and Final Overriding

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{
final int i=0;
static int j=0;
static void s(){}
final void f(){}
}
class B extends A{
static int j=1;
final int i=1;
static void s(){}
//final void f(){}(why doesn't it work)
//void s(){} (why doesn't it work)
}
class St{
public static void main(String args[]){
St st=new St();
A a =new A();
B b=new B();
//a=b;
a.j=2;
A a1=new A();
System.out.println(A.j);//What will be the value.?Why
System.out.println(a.i);
System.out.println(b.i);
}
}
Can anyone please answer my questions(commented) or rather give me a good reference
Thanking you,
sandeep
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


//final void f(){}(why doesn't it work)


You cannot override a final method


//void s(){} (why doesn't it work)


You cannot override a static method to be non-static


System.out.println(A.j);//What will be the value.?Why


Value will be 2 because j is a static variable so it belongs to the class. When you changed the value to 2 with the reference a, you changed it for a1 also.
Bill
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this piece of information about overriding in the java's sun tutorial which you might find useful:
"A subclass cannot override methods that are declared final in the superclass (by definition, final methods cannot be overridden). If you attempt to override a final method, the compiler displays an error message similar to the following and refuses to compile the program:
FinalTest.java:7: Final methods can't be overridden.
Method void iamfinal() is final in class ClassWithFinalMethod.
void iamfinal() {
^
1 error

Also, a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. 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. "
When you print A.j the output will be 2 as j is a static variable and it belongs to the class.Every instance of class A share the same value of j.You set a.j=2 for an instance 'a' of class A.Hence j is set to 2 for all instances of class A.A static variable can also be invoked without an instance of the class i.e directly using the class name.Hence invoking a.j or A.j gives you the same result i.e 2.
Hope this helps.
 
sandeep bagati
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks bill.
But it didnt clear my doubts.
First thing is that the extended class B identifies that a Static nethod has been Defined in A that in my opinion only is overriding.Now consider some private member of A say
private z(){} the compiler allows to define a method with same or different modifier in B.Shouldnt then it give a compile time error.The two things seem to me contradicting.
Now the second thing if an object of A(a) can change the value of static variable of A then what is the use of static except that the variable ( here j) can also be referenced by class name (A here).

 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The two are not contraditory. Static methods and private methods are two different things. You can't compare them. With a static method, you cannot override it in a subclass. If you override it to and make it non-static, you will get a compile error.
With private methods, they are not inherited by the subclass, so they are never seen in the subclass. You can declare a method with the same signature but it is not overriding because according to the subclass, the method doesn't exist. So you don't get an error.
As for your second question, pick up a book on static variables. This is the basics of static variables, they are available to all classes.
Bill
 
sandeep bagati
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks bill once again
So static methods can be overriden only if they are defined static in the subclass and private methods are hidden,i mean to say not inherited.
Does it then mean that only the final methods can not be declared in the subclass and hence not overriden.
Sorry, i asked the second part in wrong way.I meant to say whether in my above program it makes any difference in refering to j if I dont write static before j except that you can access it using class name.
[This message has been edited by sandeep bagati (edited March 02, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods belong to the class. Therefore they are resolved at compile time and do NOT participate in polymorphism. The just can NOT be overridden in any way at any time.
In order for polymorphism to occur (in this case overriding methods) the resolution has to be postponed until runtime (dynamic binding).
You can have a static method in B with the same name as the one in A. You can also use both of them at any time even without any instances being created. All you have to do is say A.s() or B.s() and off you go.
Private methods are resolved at runtime, so there IS polymorphism going on. Only what you don't see is that the JVM is keeping track of the "owner" of the method. When you "re-declare" a method in the sub-class the compiler knows that you will not be overriding the parents private class so in the backgound it creates the method and tags it with a different internal name to keep them separate. So you can USE it, it is just not an example of overriding.
Yes, you are right: final methods can not be declared in the sub-class so can not be overridden.
If the j is not static then when is was changed with the a.j=2, only the value for that instance is affected.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy is right. You can NOT override static methods. If you try to override it with a non-static method you will get a compile error. If you try to override it with another static method, you don't get a compile or runtime error, but you are really hiding the method not overriding it. You lose the polymorphism when you override a static with a static.
Kind of confusing but a key point to know.
Bill
 
sandeep bagati
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill,Sai and Cindy.
Yah I was not able to resolve how we can Override a static method since it involves Early Binding and Overriding involves late Binding.But still one doubt why the final methods which involve Early Binding with same signature and parameters are not allowed while static methods and final variables work perfectly.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Final methods do not involve early binding. They just can't be overridden at runtime.
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic