简体   繁体   中英

Can we override a constructor in Java and can a constructor be private?

I would appreciate an explanation for these questions:

  1. Can we Override a constructor in Java?
  2. Can a Constructor be private?

No, you can't override a constructor. They're not inherited. However, each subclass constructor has to chain either to another constructor within the subclass or to a constructor in the superclass. So for example:

public class Superclass
{
    public Superclass(int x) {}

    public Superclass(String y) {}
}

public class Subclass extends Superclass
{
    public Subclass()
    {
        super(5); // chain to Superclass(int) constructor
    }
}

The implication of constructors not being inherited is that you can't do this:

// Invalid
Subclass x = new Subclass("hello");

As for your second question, yes, a constructor can be private. It can still be called within the class, or any enclosing class. This is common for things like singletons:

public class Singleton
{
    private static final Singleton instance = new Singleton();

    private Singleton()
    {
        // Prevent instantiation from the outside world (assuming this isn't
        // a nested class)
    }

    public static Singleton getInstance() {
        return instance;
    }
}

Private constructors are also used to prevent any instantiation, if you have a utility class which just has static methods.

Constructor is meant for a class. It cant be overridden under any circumstances. Its like wanting to change Ferrari's factory from BMW's factory (which isn't practical). Surely you can overload to get the functionality you need.

Yes Constructor can be private. By making it private you are not letting the outside world to create an object of it directly through constructor, But singleton pattern uses a public static method to call the constructor of the class and object can be created.

  1. No we can't use a constructor out of class because sub class is treat constructor as a method.. without return type.
  2. You can use it as a private but if a constructor of a class is private then you cannot make the obj of the respected class into another class.

You can use callback:

In parent class:

protected void onCreate(){}

SomeClass(){
//Constructor code
onCreate()
}

in child:

@Override
protected void onCreate(){
//Child constructor code
}

1) NO! A constructor belongs to the class in which it is declared. A sub class is a different class and must have its own constructor. So, constructors simply can't be overridden.

2) Yes, that's done usually in case of singletons.

不,我们不能超越承包商,为实施Singleton Pattren,我们应该有一个私人承包商。

1) Is this just homework question, or do you try to reach something? Can you show what you try to reach with an overriding constructor?

Since the parent constructor is called first, you may modify the base class to your needs in your constructor. Of course, just as far as the access to base attributes isn't private. If you extend a class but don't like their might-be-private attributes, deriving from it was an error.

2) Can a constructor be private?

Yes, but do you know what it is good for?

  1. In a derived class you can create a new constructor with the same signature but that is not really overriding since, when initializing the class, the parent class's constructor is still called before the new one.

  2. a class's constructor can be private or protected and of course be public. but if it is protected or private how would you initiate the class? ( actually you could with a static function in that class...)

两者都不是正确的,因为当一个类继承其他类时,它会自动调用其父类,因此我们不能覆盖它们并使它们成为final是有道理的。

You can not override a constructor, however you can make them any access level modifier such as public, private or default. You would want a private constructor for things like a singleton or for a class that's mostly made of static methods and such (ie Java's Math class)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM