• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

simple overriding of static method

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

Mughal states that static methods can't be overriden (6.2 p.179,180),but the simple code below proves otherwise I think...
class Car
{
static int doors = 2;
}
class Ford extends Car
{
public static void main(String x[])
{
doors = doors + 3;
}
}
class Ranger extends Ford
{
public static void main(String x[])
{
doors = doors - 1;
System.out.println(Ford.doors);
}
}
When Ranger is run the output is 1, not 4 which shows static method main can be overriden because if you comment out the main method in Ranger and put a print statement in Ford's main then run Ranger, you will see that Ranger inherits Ford's main. Therefore normally static main methods are inherited and are invoked when ran unless overriden by subclasse's method...
Whose right, me Java beginner, or Mughal ? Please explain in detail... Thankyou in advance.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main method as ALL SORTS of special code written for it that does not apply to any other method. Mughal explains this in his discussion of main, and did not see fit to re-iterate it in his discussion of static methods.

Try the same thing with any other method and you will get the results that he describes.
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't find anything in Mughal regarding main method that would apply directly to what we are discussing. I did test again with another static method and it was overriden :
class sup
{
static void meth()
{
System.out.println("not overriden");
}
}
class sub extends sup
{
public static void main(String x[])
{
meth();
}
static void meth()
{
System.out.println("overridden");
}
}
prints out "overridden" when sub is run.
 
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
What you are experiencing is not exactly overridding. It is hidden or shadowed. Static methods do not participate in polymorphism (they are resolved at compile time).
Try reading these:
  • overriding , Hiding
  • Overriding static method.
  • plz help

  • [ edited to update links -ds ]
    [ April 14, 2004: Message edited by: Dirk Schreckmann ]
     
    Ranch Hand
    Posts: 399
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    For questions like this, you need to learn to distinguish between hidden and overridden.
    You are already familiar with the notion of hiding, because that is how member variables work in Java.
    Continuing with Ford as a subclass of Car. If Car has a variable instanceVar, and Ford has a variable instanceVar, then if you refer to instanceVar with a reference of type Car, you get the instanceVar defined in Car, and if you refer to instanceVar with a reference of type Ford, you get the instanceVar defined in Ford. And here is the "hiding" part: if code in the subclass (Ford) refers to instanceVar, it gets the instanceVar defined defined in the subclass; the instanceVar defined in the superClass (Car) is hidden. But this is just a consquence of the rule that the reference type determines which variable use used, becases the "this" pointer of the subclass (Ford) is of type Ford, while the "this" pointer of the superclass (Car) is of type (Car).
    Note that all this applies to both instance variables and static variables.
    Static methods work just the same way as variables. They are hidden, not overridden.
    Instance methods, on the other hand, actually override. If you have a Ford (subclass) object and refer to it reference of type Car (superclass) to invoke an instance method, you will get the instance methode defined in the subclass (Ford), not the superclass. Overriding means that no matter which legal reference type you use to refer to the instance method, you get the overriding method. For hiding, what you get depends on what type of reference you use.
    Static methods hide; they don't override. Here is a code fragment that demonstrates this:
    <pre>
    class Car
    {
    static String classVar = "Car static var";
    String instanceVar = "Car instance var";
    static void classMethod() {
    System.out.println("Car static method");
    }
    void instanceMethod() {
    System.out.println("Car instance method");
    }
    }
    class Ford extends Car
    {
    static String classVar = "Ford static var";
    String instanceVar = "Ford instance var";
    static void classMethod() {
    System.out.println("Ford static method");
    }
    void instanceMethod() {
    System.out.println("Ford instance method");
    }
    }
    class Escort extends Ford {
    static String classVar = "Escort static var";
    String instanceVar = "Escort instance var";
    static void classMethod() {
    System.out.println("Escort static method");
    }
    void instanceMethod() {
    System.out.println("Escort instance method");
    }
    public static void main(String[] args) {
    Car[] cars = {
    new Car(),
    new Ford(),
    new Escort()
    };
    System.out.println("References members as Car");
    for (int i = 0; i < cars.length; ++i) {
    System.out.println("Car[" + i + "]");
    Car car = cars[i];
    System.out.println(car.classVar);
    System.out.println(car.instanceVar);
    car.classMethod();
    car.instanceMethod();
    }
    System.out.println("Referencing members as Ford");
    for (int i = 0; i < cars.length; ++i) {
    if (! (cars[i] instanceof Ford)) {
    System.out.println("Car[" + i + "] is not a Ford");
    continue;
    } else {
    System.out.println("Car[" + i + "]");
    }
    Ford ford = (Ford) cars[i];
    System.out.println(ford.classVar);
    System.out.println(ford.instanceVar);
    ford.classMethod();
    ford.instanceMethod();
    }
    }
    }
    </pre>
    [This message has been edited by John Dale (edited February 25, 2001).]
    [This message has been edited by John Dale (edited February 25, 2001).]
    reply
      Bookmark Topic Watch Topic
    • New Topic