Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Subclassing Singleton class

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Subclassing Singleton class

    When i googled with "Subclassing Singleton class" string, many of the blogs said, we cannot extend the Singleton class as the constructor is private. But when i tried it in eclipse, i am able to subclass a singleton class. Would like to know whether can we subclass a singleton class technically?? If yes, will it not create many instances of Singleton class?

    My singleton class
    class SingletonClass {

    private static SingletonClass singletonObject;
    /** A private Constructor prevents any other class from instantiating. */
    private SingletonClass() {
    // Optional Code
    }
    public static synchronized SingletonClass getSingletonObject() {
    if (singletonObject == null) {
    singletonObject = new SingletonClass();
    }
    return singletonObject;
    }
    public Object clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException();
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Subclassing Singleton class

    What happens when you try to compile and execute the testing code?

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Subclassing Singleton class

    There was a small mistake in my singleton class. You r rite, it gives compiler error "Implicit super constructor SingletonClass() is not visible for default constructor. Must define an explicit constructor".
    Thanks for ur help.

Similar Threads

  1. Singleton thread safe
    By LorenzoR in forum Threads
    Replies: 1
    Last Post: October 4th, 2011, 12:41 PM
  2. Replies: 5
    Last Post: September 17th, 2011, 09:05 PM
  3. Optimizing Singleton Design Pattern
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: April 19th, 2011, 02:13 AM
  4. API singleton theory
    By dunnkers in forum Java Theory & Questions
    Replies: 10
    Last Post: February 24th, 2011, 12:57 PM
  5. Singleton pattern: the purpose of empty constructor
    By Asido in forum Java Theory & Questions
    Replies: 1
    Last Post: August 6th, 2010, 08:58 AM