• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

overriding static methods

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read somewhere that static methods can't be overriden, but they can be shadowed. So that
public class A {
public static void someMethod(){}
}
public class B extends A {
public static void someMethod(){}
}
will compile. And the following code will execute A's someMethod.
A a = new B();
a.someMethod();
How come the overriding rules apply to someMethod() if I try a change scope?
public class A {
public static void someMethod(){}
}
public class B extends A {
protected static void someMethod(){}
}
Compiler complains that can't make access modifier more restrictive when overriding. But I thought that it wasn't overriding?
 
John Towell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so I searched the JLS and found that it's actually called "hiding" not "shadowing".
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#227928
Which means this statement in the FAQ is wrong.
http://www.javaranch.com/certfaq.jsp#q18
Anyways it still doesn't make sense to me why you would need to enforce the overriding rules? Since it is not possible to call the child method polymorphically(word?), meaning you can't call the child method when you have a super class reference. So why would it matter if you changed the access modifier's scope?
Anyways maybe I'm just ranting.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rant. I do that all time. It's my hobby...
Let's call the variable that holds an object (as opposed to a variable that holds an int, for example) an object reference. Let's also create two classes A and B. B is a subclass of A.

Class A has a whoAmI() method that is overriden by B's whoAmI() method. The first constructor call and the first invocation of whoAmI() works as expected.
Now, note that the assignment
A a2 = new B();
is also possible because the object reference a2 can "hold" objects that are either of type A or subclasses of A. Now when a2 invokes whoAmI(), the JVM checks the actual type of the object being held by a2. If it had been of type A, then it would have printed "A". Since, the object is of type B, then "B" is printed on the screen.
As far as access modifiers are concerned, the overriding method must be at least as restrictive as the method being overriden, or less restrictive. So if whoAmI() of A had been declared with "protected" accessibility, then whoAmI() of B must be declared as either equally "protected" or public, but not "default", since this is more restrictive.
Finally, methods with "private" accessibility cannot be overriden.
Rant ends.
[ July 31, 2002: Message edited by: Anthony Villanueva ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you John. Maybe the compiler just checks the constraints without taking notice about the two methods being static or not.
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O/P is
Inside A
Inside A
Can anybody explain while calling static methods which object type is used?I mean weather compile time object type or runtime object type?Hope I make sense :roll:
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When invoking a static method, the compile-time type is always used.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no special purpose on calling a static method via an object reference, or the class itself.
is that your question?
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Corey.This static method overriding really confused me how the javm makes decision about which instance variable to call.Just for clarification I am listing again.
In general
*To call static methods & variables compiletime object type is used
*To call non static methods & variables runtime object type is used.
Am I right?
Thanks again
Veena
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Veena Point:
Thanks Corey.This static method overriding really confused me how the javm makes decision about which instance variable to call.Just for clarification I am listing again.
In general
*To call static methods & variables compiletime object type is used
*To call non static methods & variables runtime object type is used.
Am I right?
Thanks again
Veena


Veena,
It would be more correct to say that the reference to a static class member is resolved at compile time based on the type of the reference. In your earlier post you used a reference of type A to access a static method on two objects--one object was an instance of A and the other was an instance of the subclass B. In both cases the static method of class A was invoked, because the reference was of type A.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veena,
Here's an example that illustrates the point that the static method that is invoked at run-time is determined by the compile-time type of the reference.

The above program prints CBA.
In all three cases the type of the object is C; but the method in class C is only invoked when the reference type is C. When the reference has type B, then the method from class B is invoked regardless of the type of the run-time object. Similarly, when the reference type is A, the method from class A is invoked even though the object has type C.
 
John Towell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
true true. Also remember Veena that member variables act the same way both static and non static. They use the reference type not instance type. So...
public class A {
public String member = "A";
}
public class B extends A {
public String member = "B";
}
A a = new B();
System.out.println(a.member);
prints A not B. So your statement
"*To call non static methods & variables runtime object type is used." isn't really true.
Member variables hide on another as do static methods. However you can change the access modifier on a member variable. Which brings me back to my original point, there is no good reason that static methods should follow overriding rules correct? That's just the way Java does it I guess.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jhon,
I agree with you."There is no good reason that static methods should follow overriding rules correct? That's just the way Java does it"
Dan,
Wanted to clarify all these points.
*Reference to a static members/methods & non static members is resolved at compile time based on the type of the reference.
*Invoking non static overriden methods is done at runtime based on the type of instance.

Veena
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Veena Point:

Dan,
Wanted to clarify all these points.
*Reference to a static members/methods & non static members is resolved at compile time based on the type of the reference.
*Invoking non static overriden methods is done at runtime based on the type of instance.
Veena


I agree, but I would insert the word "variables" after the term "non static member".
The above facts are a good example of why it is a good idea to make your member variables private and force clients to access them only through instance methods.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This discussion of inheritance and member variables has been inspiring. Try this one just for fun.

[ July 31, 2002: Message edited by: Dan Chisholm ]
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,
I tried above code,how come the o/p is ABB ?It should be ABC right?coz statement a=b=c=new C() is same as the following block of code
c=new C();
b=c;
a=b;
In that case when u say m1(a);m1(b);m1(c) it should print ABC,coz during compile time Object is referrence type are A,B,C respectively.
Veena
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veena,
You are correct. The output should be "ABC".
If it is printing ABB on your system, then I would first check to make sure that you didn't introduce a typo when you pasted the code into your text editor.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right dan,there was typing mistake.Thanks.
Veena
 
This is my favorite show. And this is my favorite tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic