Results 1 to 10 of 10
  1. #1
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Wink Private constructor and super()

    Hi to all,
    Hope everyone will be fine.Can any one tell me how can i make use of super() in case of private constructor.
    Thanks to everyone.:)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default

    Hi :rolleyes:,
    Bcause this question asked me in an interview and i was unable to answer the question because i don't know :rolleyes:
    Thanks :)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Seems to me you don't have a basic idea at least about the constructors. Looking at the question, either you heard it wrong way or interview panel ask in wrong way to test you.

    Java Constructor Tutorial

  5. #5
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    I'm not sure why you would want to do this but it definitely possible:

    Java Code:
    public class ClassA
    {
       private int val;
       private ClassA(int val)
       {
          this.val = val;
       }
       public int getVal()
       {
          return val;
       }
    }
    
    public class ClassB extends ClassA
    {
       public ClassB(int val)
       {
          super(val);
       }
    }
    
    ...
    ClassB b = new ClassB(4);   
    System.out.println("value of b: " + b.getVal());
    ...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mrmatt1111 View Post
    I'm not sure why you would want to do this but it definitely possible:
    Hey mr matt. Good to see ya posting here!

    Regarding the above, are you sure that it's even possible? The only way I know of getting close to a private constructor is if the super class also has a non-private constructor that calls the private constructor via this(...), ... unless I'm missing something.

    /Pete

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To use your example, for instance,
    Java Code:
    public class ClassA
    {
       private int val;
       
       public ClassA(int val, boolean dummy) {
         this(val);
       }
       
       private ClassA(int val)
       {
          this.val = val;
       }
       public int getVal()
       {
          return val;
       }
    }
    
    class ClassB extends ClassA
    {
       public ClassB(int val)
       {
          super(val, true);
       }
    }

  8. #8
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Ahhh, oops. =)

    It only works with inline classes:

    Java Code:
    public class Class
    {
       public static class ClassA
       {
          private int val;
          private ClassA(int val)
          {
             this.val = val;
          }
          public int getVal()
          {
             return val;
          }
       }
    
       public static class ClassB extends ClassA
       {
          public ClassB(int val)
          {
             super(val);
          }
       }
    
       public static void main(String[] args)
       {
          ClassB b = new ClassB(4);
          System.out.println("value of b: " + b.getVal());
       }
    }

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    To use your example, for instance,
    Java Code:
    public class ClassA
    {
       private int val;
       
       public ClassA(int val, boolean dummy) {
         this(val);
       }
       
       private ClassA(int val)
       {
          this.val = val;
       }
       public int getVal()
       {
          return val;
       }
    }
    
    class ClassB extends ClassA
    {
       public ClassB(int val)
       {
          super(val, true);
       }
    }
    This is excellent Fubarable. I hope it's much clear for our OP.

  10. #10
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    The reason for private constructors is to prevent the class from being instantiated by other classes. Here are a few uses.

    1. The class has a static "factory" method that creates instances. Making the constructor private forces other classes to use the factory method.

    2. The class is a "utility" class, which provides one or more static mehtods that do the work. Making the class abstract and final means that it cannot be constructed or extended. An interface can be used for the same purpose.

    3. The class is a "singleton", which means that one instance of the class does all the work. A static method allows other classes to access the single instance.

    The answer I would give is that a private constructor, by itself, prevents the class from being constructed by other classes. However, it can be be accessed by static methods and from an outer class if the class is an inner class.

Similar Threads

  1. Replies: 3
    Last Post: 03-14-2009, 07:22 AM
  2. this() vs super()
    By Hevonen in forum New To Java
    Replies: 9
    Last Post: 12-04-2008, 01:43 PM
  3. [SOLVED] Accessing private constructor
    By piyu.sha in forum New To Java
    Replies: 2
    Last Post: 10-06-2008, 05:45 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 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
  •