Results 1 to 5 of 5
  1. #1
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default overriding with same code

    hi friends,
    i am confused with polymorphism and static methods. as we know static methods cannot be overridden. so static methods are called based on the type of reference, not on actual object loaded in that reference.

    i invoked a static method from a non static one. as shown below
    class Static1
    {
    int x = 21;
    static void printData()
    {
    System.out.println("From printData of Static1");
    }
    void call()
    {
    printData();
    }
    }

    class Static2 extends Static1
    {
    int x =22;
    static void printData()
    {
    System.out.println("From printData of Static2 ");
    }

    // method same as in base class, but if it is removed output changes.....!
    void call()
    {
    printData();
    }


    }

    class StaticTest
    {
    public static void main(String ... args)
    {
    Static1 obj;

    obj = new Static1();
    obj.call();

    obj = new Static2();
    obj.call();

    }
    }

    it gives output as
    From printData of Static1
    From printData of Static2

    but if i remove call() method from static2 then output will be

    From printData of Static1
    From printData of Static1

    whats happening here? both call() methods are the same, even if it is not typed in second class, it exists there as inherited from base class. so is it same placing same code to override, or leave it for base class implementation? when we place same code to override it, produce different outputs....

    please let me know how its happening??

    thanks in advance......

  2. #2
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    Static2.this.call() calls Static2.printData() if printData() is redefined in
    Static2, otherwise the superclass's [Static1] method is used.

    Static1.this.call() will always call Static1.printData(), because it does not know its subclasses.
    printData() is just a not fully qualified why to say Static1.printData() in Static1.

    Bottom line:
    Static methods are not virtual, only non-static method are virtual. (If not sealed that is, i.e. have the keyword 'final'.)
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  3. #3
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks Hibernate

    but my doubt remains, i override the method in Static2 with the same code in Static1, nothing got changed in Static2, but just placing same method, its behavior changed......

  4. #4
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    Yes, because in Static1, printData() is interpreted as Static1.printData(). And
    and in Static2, printData() is interpreted as Static2.printData(), which is interpreted as Static1.printData() if printData was not redefined in Static2.

    So the code is complied as
    Java Code:
    class Static1
    {
        int x = 21;
        static void printData()
        {
            System.out.println("From printData of Static1");
        }
        void call()
        {
            Static1.printData();
        }
    }
    
    class Static2 extends Static1
    {
        int x = 22;
        static void printData()
        {
            System.out.println("From printData of Static2 ");
        }
    
        @Override  //It is good practice to use @Override on overridden methods (and implemented from interfaces in Java 6)
        void call()
        {
            Static2.printData(); //This is not the same as in Static1
        }
    }

    And since statics can not be overridden Static1.printData() will call
    Static1's printData().
    Last edited by Hibernate; 12-30-2010 at 02:40 PM.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  5. #5
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

Similar Threads

  1. Overriding
    By prasanna1157 in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 08:47 AM
  2. Overriding question
    By alacn in forum New To Java
    Replies: 1
    Last Post: 08-16-2010, 12:04 PM
  3. Overriding
    By renuka_renukut in forum Advanced Java
    Replies: 3
    Last Post: 05-21-2010, 09:45 AM
  4. Overriding equals method
    By sky in forum New To Java
    Replies: 7
    Last Post: 03-12-2010, 04:39 PM
  5. Overriding Methods
    By AndrewM16921 in forum New To Java
    Replies: 2
    Last Post: 09-23-2009, 07:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •