Results 1 to 19 of 19
Like Tree4Likes
  • 1 Post By SurfMan
  • 2 Post By gimbal2
  • 1 Post By SurfMan

Thread: super Keyword

  1. #1
    akimo360 is offline Member
    Join Date
    Dec 2014
    Posts
    6
    Rep Power
    0

    Default super Keyword

    I'm kinda starting out on java and fell upon this keyword. I get that this super keyword is used in the subclass to make a reference to the superclass am I right? Like for example there is a method in the superclass printSomething(); and the method with the same name in the subclass printSomething(); so its super.printSomething(); but the only thing that I'm unsure of is the use of this keyword in constructors. They call the constructor from the superclass and then what happens? I'm lost there.

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

    Default Re: super Keyword

    The syntactic structures this( ... ) and super( ... ) in (the first statement of) a constructor, refer to a constructor in 'this' class or a constructor of the superclass respectively; what don't you understand about it?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: super Keyword

    You are right, super is a reference to the superclass, i.e. the class your class inherits from. You will see usages in constructors, but there are other useful applications too. Consider next class:
    Java Code:
    public class MessagePrinter {
       private String name;
       public MessagePrinter(String name) {
          this.name = name;
       }
    
       public void printMessage(String m) {
          System.out.println(name + ": " + m);
       }
    
    }
    This class has one method that prints a String to System.out. Now consider this class:
    Java Code:
    public class ConditionalMessagePrinter extends MessagePrinter {
       
       public ConditionalMessagePrinter() {
          super("Conditional");
       }
    
       @Override
       public void printMessage(String m) {
          if ( m != null ) {
              super.printMessage(m);
          }
       }
    }
    As you can see in the above example, the second class that two super calls:
    1) In the constructor. The super class has no default constructor (without parameters), so we have to call the super's constructor.
    2) In the printMessage() method. If you omit the super keyword, the second class just calls itself again and again, until you get a StackOverflowError. Adding super will call the original method so that we don't have to worry about reimplementing the original method. In this case it's a simple method, but if it's a complex method, copying and pasting would be a sin. (Every time you hit CTRL-V in your editor, God kills a kitten)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    akimo360 is offline Member
    Join Date
    Dec 2014
    Posts
    6
    Rep Power
    0

    Default Re: super Keyword

    Quote Originally Posted by SurfMan View Post
    As you can see in the above example, the second class that two super calls:
    1) In the constructor. The super class has no default constructor (without parameters), so we have to call the super's constructor.
    Ok I get that we have to call the super's constructor so it would work out the constructor from the super but what would that do? By calling the constructor from super what would that exactly do and what effect would it have on objects created?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: super Keyword

    Quote Originally Posted by akimo360 View Post
    Ok I get that we have to call the super's constructor so it would work out the constructor from the super but what would that do? By calling the constructor from super what would that exactly do and what effect would it have on objects created?
    When you extend a class, the super class' default constructor will be implicitly invoked or you need to invoke one yourself via the super keyword. This causes the super class to properly initialize its state. Sometimes there is no state to initialize in the sub class. So the sub class' constructor is used to invoke the super class' constructor via a super() call.

    Read about it here Using the Keyword super (The Java Tutorials > Learning the Java Language > Interfaces and Inheritance)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    akimo360 is offline Member
    Join Date
    Dec 2014
    Posts
    6
    Rep Power
    0

    Default Re: super Keyword

    So when do we normally invoke the super class' constructor in the sub class' ? Do you have a scenario that I can visualize with?

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: super Keyword

    Compile these and see which one works and which one doesn't.

    Example 1:
    Java Code:
    public class BaseExample {
       public BaseExample() {
          //default no-args constuctor
       }
    }
    
    public class SubclassExample extends BaseExample {
       public SubclassExample() {
          //default no-args constuctor
       } 
    }

    Example 2:
    Java Code:
    public class BaseExample {
       public BaseExample(String value) {
           //constructor with one parameter.
       }
    }
    
    public class SubclassExample extends BaseExample {
       public SubclassExample() {
          //default no-args constuctor
       } 
    }
    Also, as an extra exercise, try this one. See if you can set a value for the field name in the subclass:

    Example 3:
    Java Code:
    public class BaseExample {
    
       private String name;
    
       public BaseExample(String name) {
           this.name = name;
       }
    }
    
    public class SubclassExample extends BaseExample {
       public SubclassExample(String name) {
          this.name = name; //does this work???
       } 
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    iamsethi is offline Member
    Join Date
    Dec 2014
    Posts
    19
    Rep Power
    0

    Default Re: super Keyword

    What is the problem in this code?
    class Sort2D
    {

    public static void main(String...s)
    {
    Sort r=new Sort();
    int z[][]=r.sort(new int[][]{{1,0,567,4,3,33},{333,677,34243,8987,3434,324},{4 46,876,23,546565,332}});
    for(int i=0;i<z.length;i++)
    for(int j=1;j<z[i].length;j++)
    System.out.println(z[i][j]);
    }

    int[][] sort(int x[][])
    {int temp;
    for(int i=0;i<x.length;i++)
    {
    for(int j=0;j<(x[i].length)-i;j++)
    {
    if(x[i][j]<x[i][j-1])
    {
    temp=x[i][j];
    x[i][j]=x[i][j-1];
    x[i][j-1]=temp;
    }
    }
    }
    return x;

    }


    }

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: super Keyword

    There is no class Sort; there's a class Sort2D ... and nowhere is there a problem with 'super', so I don't understand your reply.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: super Keyword

    Quote Originally Posted by iamsethi View Post
    What is the problem in this code?
    It is randomly dumped in a random topic of a random user.
    gimbal2 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  11. #11
    akimo360 is offline Member
    Join Date
    Dec 2014
    Posts
    6
    Rep Power
    0

    Default Re: super Keyword

    Quote Originally Posted by SurfMan View Post
    Also, as an extra exercise, try this one. See if you can set a value for the field name in the subclass:

    Example 3:
    Java Code:
    public class BaseExample {
    
       private String name;
    
       public BaseExample(String name) {
           this.name = name;
       }
    }
    
    public class SubclassExample extends BaseExample {
       public SubclassExample(String name) {
          this.name = name; //does this work???
       } 
    }
    I checked and you can't set a value for the field name. So is this where you use the super(name);?

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: super Keyword

    Try to reason WHY you can't set the field from the subclass - that reason makes it clear why you should use super() or not.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    akimo360 is offline Member
    Join Date
    Dec 2014
    Posts
    6
    Rep Power
    0

    Default Re: super Keyword

    Quote Originally Posted by gimbal2 View Post
    Try to reason WHY you can't set the field from the subclass - that reason makes it clear why you should use super() or not.
    Because the field name is private in the superclass am I right?

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: super Keyword

    That is indeed the technical reason why it is not possible to change the property from the subclass. And there is a good reason why you make it private and why subclasses can't directly access those private properties.

    I like to see it like this: each class is responsible for managing its own properties. That responsibility doesn't change when you create subclasses - the parent classes are still responsible for maintaining their own properties as part of the "whole". That's why you invoke super() - to tell the parent class to initialize its own stuff first before the subclass starts to do its own bookkeeping. That gives you a security that at the moment that the subclass starts to do its initialization work, you can be sure that the parent classes are already in a properly initialized state.
    SurfMan and akimo360 like this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    akimo360 is offline Member
    Join Date
    Dec 2014
    Posts
    6
    Rep Power
    0

    Default Re: super Keyword

    Oh I get it now! Thank you so much! :)

  16. #16
    iamsethi is offline Member
    Join Date
    Dec 2014
    Posts
    19
    Rep Power
    0

    Default Re: super Keyword

    I know this isn't related to Super but i am stuck at this problem is showing me array index out of bound exception:-1 error Please help
    class Sort2D
    {

    public static void main(String...s)
    {
    Sort2D r=new Sort2D();
    int z[][]=r.sort(new int[][]{{1,0,567,4,3,33},{333,677,34243,8987,3434,324},{4 46,876,23,546565,332}});
    for(int i=0;i<z.length;i++)
    for(int j=0;j<z[i].length;j++)
    System.out.println(z[i][j]);
    }

    int[][] sort(int x[][])
    {int temp;
    for(int i=0;i<x.length;i++)
    {
    for(int j=0;j<(x[i].length)-i;j++)
    {
    if(x[i][j]<x[i][j-1])
    {
    temp=x[i][j];
    x[i][j]=x[i][j-1];
    x[i][j-1]=temp;
    }
    }
    }
    return x;
    }
    }
    Last edited by iamsethi; 12-15-2014 at 12:33 PM.

  17. #17
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: super Keyword

    Quote Originally Posted by iamsethi View Post
    I know this isn't related to Super but i am stuck at this problem is showing me array index out of bound exception:-1 error Please help
    Create your own thread. There is a huge button that says: + Post New Thread. Click that as hard as you can.
    iamsethi likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  18. #18
    agrawal1294 is offline Member
    Join Date
    Dec 2014
    Posts
    4
    Rep Power
    0

    Default Re: super Keyword


  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: super Keyword

    Quote Originally Posted by agrawal1294 View Post
    That only explains the how like hundreds of other random sites, not the why. The why is far more important.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. I am a super noob with a super noob question.
    By LittleZoppo in forum Java Applets
    Replies: 3
    Last Post: 04-27-2012, 04:50 AM
  2. Super Keyword
    By samuri in forum New To Java
    Replies: 23
    Last Post: 07-29-2011, 04:49 PM
  3. Replies: 2
    Last Post: 06-03-2011, 10:15 PM
  4. rule for using the super keyword
    By javastuden in forum New To Java
    Replies: 2
    Last Post: 10-01-2010, 10:02 AM
  5. calling variable using super super..
    By Stephen Douglas in forum New To Java
    Replies: 7
    Last Post: 08-16-2010, 07:12 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
  •