Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3241856
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:14:49+00:00 2026-05-17T18:14:49+00:00

Suppose we have the following code: class Test { private Test() { System.out.println(test); }

  • 0

Suppose we have the following code:

class Test {
    private Test() {
        System.out.println("test");
    }

}

public class One extends Test {

    One() {
        System.out.println("One");
    }

    public static void main(String args[]) {

        new One();
    }
}

When we create an object One, that was originally called the parent class constructor Test(). but as Test() was private – we get an error.
How much is a good example and a way out of this situation?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:14:50+00:00Added an answer on May 17, 2026 at 6:14 pm

    There is no way out. You have to create an available (protected, public or default) super constructor to be able to extend test.

    This kind of notation is usually used in utility classes or singletons, where you don’t want the user to create himself an instance of your class, either by extending it and instanciating the subclass, or by simply calling a constructor of your class.

    When you have a class with only private constructors, you can also change the class to final because it can’t be extended at all.


    Another solution would be having a method in test which create instances of test and delegate every method call from One to a test instance. This way you don’t have to extend test.

    class Test {
        private Test() {
            System.out.println("test");
        }
        public static Test getInstance(){
            return new Test();
        }
        public void methodA(){
            //Some kind of implementation
        }
    }
    
    public class One {
        private final Test test;
        One() {
            System.out.println("One");
            test = Test.getInstance();
        }
    
        public void methodA(){
            test.methodA();
        }
    
        public static void main(String args[]) {
            new One();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code public class Test{ private static final String key =
Suppose I have the following classes: public class Test { public static void main(String[]
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Say suppose I have the following Java code. public class Example { public static
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
Suppose I have following code: public class CBase: AbstractC,IRenderable { //code here } public
Suppose I have the following code: namespace x{ class X{ virtual void x(){} }
Suppose I have a class with a private property and associated public getter and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.