Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By Tolls

Thread: Difference between overriding and redefining...

  1. #1
    asif_plz is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Difference between overriding and redefining...

    please see the below example ....And when we make both method static its calls accordingly the reference variable.......But still they override the method so how come the method redefining .....i am not clear with the redefine please let explain it in more details.....


    class Animal
    {
    void doStuff()
    {
    System.out.println("Aniaml");

    }
    }

    class Dog extends Animal

    {
    void doStuff()

    {
    System.out.println("Dog");

    }
    }

    public class TestAnimal
    {
    public static void main(String as[])

    {
    Animal a= new Animal();
    Animal b= new Dog();

    a.doStuff();
    b.doStuff();

    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: Difference between overriding and redefining...

    Make both the doStuff() methods static, ignore the warnings and run your code again. See?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Difference between overriding and redefining...

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Difference between overriding and redefining...

    asif_plz, you didn't bother to reply to the two responses on your earlier thread.
    Autoboxing & Unboxing

    Not the best way to continue to get free help on a forum, that.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Difference between overriding and redefining...

    I didn't realize that an instance of a class could run the static methods of its own class. I always assumed that the only way to call a static method of class Animal is this:

    Java Code:
    Animal.doStuff();
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: Difference between overriding and redefining...

    Quote Originally Posted by awinston View Post
    I didn't realize that an instance of a class could run the static methods of its own class. I always assumed that the only way to call a static method of class Animal is this:

    Java Code:
    Animal.doStuff();
    It's even funnier; try this:

    Java Code:
    Animal foo= null;
    foo.doStuff();
    No null pointer exception, no nothing; except the static method from the Animal class is run ...

    kind regards,

    Jos
    awinston likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Difference between overriding and redefining...

    Quote Originally Posted by awinston View Post
    I didn't realize that an instance of a class could run the static methods of its own class. I always assumed that the only way to call a static method of class Animal is this:

    Java Code:
    Animal.doStuff();
    You ought to continute doing it that way, though, for this very reason.
    Calling a static method from an instance variable tends to lead to confusion.
    Luckily you tend to get a warning if you do it by accident.
    awinston likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. overriding
    By sandeep chopparapu in forum New To Java
    Replies: 4
    Last Post: 05-14-2012, 09:54 AM
  2. Overloading or Overriding...
    By chathura87 in forum New To Java
    Replies: 5
    Last Post: 02-22-2011, 02:46 AM
  3. overriding v/s hiding
    By parminder in forum New To Java
    Replies: 3
    Last Post: 01-17-2011, 05:24 AM
  4. Overriding
    By prasanna1157 in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 08:47 AM
  5. Overriding
    By renuka_renukut in forum Advanced Java
    Replies: 3
    Last Post: 05-21-2010, 09:45 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
  •