• Post Reply Bookmark Topic Watch Topic
  • New Topic

using super keyword call non overridden superclass method  RSS feed

 
sakthi vandhana
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I know its possible to call a non overridden superclass method from a subclass method using super keyword.
But will there be any practical need? anyone of you encountered such use?

Thanks
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sakthi vandhana wrote:
But will there be any practical need?


There are many other usage of super keyword, than the one you posted. For e.g.:
  • super is used to chain the super class constructor. You can't ignore this. Either you do it explicitly, or it is done by the compiler implicitly behind the scene. The first statement of every constructor you write, is a chaining to the super class constructor.


  • So, if you write a constructor like this:



    it is translated by the compiler to the following:


    You can go through the JLS Section - 15.11.2, for some details, about super.

     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    sakthi vandhana wrote:I know its possible to call a non overridden superclass method from a subclass method using super keyword.
    But will there be any practical need? anyone of you encountered such use?

    Well, I suppose you could use it to ensure that the superclass's method is always called - even if, at some future date, someone changes this class so that it does override it - but in answer to your last question: no, I've never encountered such use, and I can't really see any practical application for it.

    Winston
     
    Steve Luke
    Bartender
    Posts: 4181
    22
    IntelliJ IDE Java Python
    • Likes 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    sakthi vandhana wrote:I know its possible to call a non overridden superclass method from a subclass method using super keyword.
    But will there be any practical need? anyone of you encountered such use?

    Just to add a bit to what Winston said, the only reason I would think of using it would be code-clarity. If you are calling a method that you inherit, someone reading your code might look for that method and not know where it came from (especially if the class is large). By using super.methodName() you indicate clearly the method being called comes from the super class, and is akin to using this.methodName() for calling methods/using variables in the current class. Neither is necessary (normally) but both can make it easier to read code and know where things are coming from.

    That said, I usually use this.methodName() but don't think I have every used super.methodName() except to specifically call a super-class method that I did override.
     
    sakthi vandhana
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you all.

    I got it.. :-)

     
    Chan Ag
    Rancher
    Posts: 1090
    14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Very nice and interesting suggestions, Steve & Winston. Thank you.
    Added that to my good coding practices' list.
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    The first statement of every constructor you write, is a chaining to the super class constructor.


    Except when you invoke a constructor yourself.

    Compiled from "a.java"
    public class a extends aa{
    int x;

    a();
    Code:
    0: aload_0
    1: iconst_0
    2: invokespecial #1; //Method "<init>"I)V
    5: return

    a(int);
    Code:
    0: aload_0
    1: invokespecial #2; //Method aa."<init>")V
    4: aload_0
    5: iload_1
    6: putfield #3; //Field x:I
    9: return

    public static void main(java.lang.String[]);
    Code:
    0: new #4; //class a
    3: dup
    4: bipush 10
    6: invokespecial #1; //Method "<init>"I)V
    9: astore_1
    10: return

    }


     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!