Results 1 to 10 of 10
Like Tree4Likes
  • 1 Post By JosAH
  • 1 Post By gimbal2
  • 1 Post By masijade
  • 1 Post By JosAH

Thread: The use of super() in a constructor

  1. #1
    nat
    nat is offline Member
    Join Date
    Dec 2013
    Posts
    56
    Rep Power
    0

    Default The use of super() in a constructor

    Hello,

    I have this code:

    import java.awt.Graphics2D;
    import java.util.ArrayList;
    import java.util.List;

    import javax.xml.bind.annotation.XmlElement;

    public class nameofclass{


    public nameofclass() {

    }

    public nameofclass(String id) {
    super();
    this.id = id;
    }
    Can anybody explain me why do I call the superclass no-argument constructor (super()), and which is the superclass? There isn't any keyword extends and mentioned name of a superclass? Is it the empty constructor mentioned above the one with parameters?

    Thanks in advance

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: The use of super() in a constructor

    In THIS example Object is the "super" class. If your class extended another class THAT extended class would be the super class. And super is ALWAYS called, implicitly, unless another super call (i.e. one with arguments) or a "this" call is made (and it MUST be the FIRST line in the constructor) there is no reason to call it explicitly. Those calls are made to ensure that the super class constructor is called, i.e. that the super class is properly initiated.

  3. #3
    nat
    nat is offline Member
    Join Date
    Dec 2013
    Posts
    56
    Rep Power
    0

    Default Re: The use of super() in a constructor

    Does this mean that super(); calls the empty nameofclass constructor here?
    Thanks

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,712
    Rep Power
    5

    Default Re: The use of super() in a constructor

    No it refers to the Object constructor because Object is the super class (or 'parent class' if you will).
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: The use of super() in a constructor

    I sometimes implement a (protected) method duper() in my super class, just so I can do:

    Java Code:
    super.duper();
    kind regards,

    Jos ;-)
    masijade likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,712
    Rep Power
    5

    Default Re: The use of super() in a constructor

    he he :) Or sonic().

    Java Code:
    super.sonic();
    masijade likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: The use of super() in a constructor

    Or califragilisticexpialidocious ;-)
    JosAH likes this.

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

    Default Re: The use of super() in a constructor

    Yep, and man() and woman() also make fine method names in a super class; I also like to name a class That, just to make the reader lose track completely:

    Java Code:
    class That {
    	That that;
    	boolean them;
    	boolean us= !them;
    	
    	public That(That that) {
    		this.that= this;
    	}
    	
    	protected That duper(That that) {
    		
    		if (That.this == null)
    			that.that.duper(this.that);
    		
    		return (them || us)?That.this:(That)this.that.duper(that.that);
    	}
    }
    kind regards,

    Jos ;-)
    gimbal2 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,712
    Rep Power
    5

    Default Re: The use of super() in a constructor

    Thread derailment: complete
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: The use of super() in a constructor

    Quote Originally Posted by gimbal2 View Post
    Thread derailment: complete
    Waitwaitwait! We didn't discuss Grolsch beer yet or scantily clad women ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. this(), and super() within a method (not a constructor)
    By milleniamisc in forum New To Java
    Replies: 1
    Last Post: 11-21-2012, 03:31 AM
  2. Replies: 1
    Last Post: 05-08-2012, 09:40 PM
  3. Replies: 3
    Last Post: 11-01-2011, 08:24 AM
  4. Replies: 3
    Last Post: 01-21-2011, 01:37 PM
  5. Private constructor and super()
    By Basit56 in forum Advanced Java
    Replies: 9
    Last Post: 08-21-2009, 06:19 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
  •