Results 1 to 5 of 5

Thread: overriding

  1. #1
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default overriding

    i have a question for you guys....help me out, iam a little bit confused
    we have a concept called overriding in java...and in here we over ride the method of a superclass and my question is
    we can do the same by creating a new method instead of overriding existing method and making it loos its functionality.....and i want to know the advantages of using overriding too,since we can achieve the same by creating a new method..
    Last edited by sandeep chopparapu; 05-12-2012 at 02:29 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: overriding

    we can do the same by creating a new method instead of overriding existing method and making it loos its functionality
    So one would only override a method if the superclass' method didn't do what the subclass needs. In that case, you would override to change the functionality a bit. However, if the superclass' method already works just fine, then you are correct, we don't need to override at all.

    You talk about just making a new method to preserve the old one - that implies that the new method would do something different than the one from the super class. If that is the case, then either it provides completely new functionality, in which case it should have a different name anyway, or it provides the same functionally but does it in a different way in which case again it should either have a new name or override the original.

    There are many cases where overriding makes sense, but that's the point too - if what you are about to do doesn't make sense when you say it out loud, you should question doing it at all. It all depends on the context of your design.

  3. #3
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: overriding

    thanks for the replay....

  4. #4
    odd123 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: overriding

    Well, typically if you have a superclass - all the children of that class inherit its methods. The point is, to try to make your code as minimal, yet efficient as possible. Particularly to avoid code duplication. Overriding in Java gives us this tool. Why create another method which does the same as what is happening in the superclass?

    Keep in mind. the method in the superclass (that which is being overriden) is also invoked, but the method in the subclass, is being invoked first. Hope this makes sense.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: overriding

    Quote Originally Posted by odd123 View Post
    Well, typically if you have a superclass - all the children of that class inherit its methods. The point is, to try to make your code as minimal, yet efficient as possible. Particularly to avoid code duplication.
    That's a misuse of inheritance. Also has nothing to do with efficiency.

    Quote Originally Posted by odd123 View Post
    Overriding in Java gives us this tool. Why create another method which does the same as what is happening in the superclass?
    So use an instance of the other class as a field and invoke its method. Your argument isn't a justification for inheritance.

    Quote Originally Posted by odd123 View Post
    Keep in mind. the method in the superclass (that which is being overriden) is also invoked, but the method in the subclass, is being invoked first.
    Totally wrong. A super implementation is only invoked if specifically called from the override in the subclass.

    Quote Originally Posted by odd123 View Post
    Hope this makes sense.
    Not really.

    Subclassing is for specialization of behavior. It's important to understand that every instance of a subtype is-a instance of its supertype(s), all the way up to java.lang.Object.

    Overriding a method, rather than creating a new method with a different signature, allows for runtime polymorphism: the method is invoked on any subtype, or on the supertype itself, but the code that executes is that in the class of the instance on which the method is invoked.

    An example of this can be seen in the Swing component hierarchy, where every Swing component is-a JComponent. When the repaint manager calls the paint(Graphics) method, each component repaints itself appropriately by executing the code in its paint(...) method that overrides paint(Graphics) in JComponent (which incidentally does nothing). If these methods were to be written as paintButton(...), paintLabel(...) etc. it wouldn't be possible to paint any subtype of JComponent with a single, common method invocation.

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

Similar Threads

  1. overriding v/s hiding
    By parminder in forum New To Java
    Replies: 3
    Last Post: 01-17-2011, 04:24 AM
  2. overriding with same code
    By jomypgeorge in forum New To Java
    Replies: 4
    Last Post: 12-31-2010, 06:54 AM
  3. Overriding
    By prasanna1157 in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 07:47 AM
  4. Overriding question
    By alacn in forum New To Java
    Replies: 1
    Last Post: 08-16-2010, 11:04 AM
  5. Overriding
    By renuka_renukut in forum Advanced Java
    Replies: 3
    Last Post: 05-21-2010, 08: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
  •