why a class can not be subclassed if i declare its default constructor as private

StackOverflow https://stackoverflow.com/questions/23662659

  •  22-07-2023
  •  | 
  •  

Domanda

In a class i can have as many constructors as I want with different argument types. I made all the constructors as private,it didn't give any error because my implicit default constructor was public But when i declared my implicit default constructor as private then its showing an error while extending the class. WHY?

this works fine

public class Demo4  {
    private String name;
    private int age;
    private double sal;

    private Demo4(String name, int age) {
        this.name=name;
        this.age=age;   
    }

    Demo4(String name) {
        this.name=name;
    }

    Demo4() {
        this("unknown", 20);
        this.sal=2000;
    }

    void show(){
        System.out.println("name"+name);
        System.out.println("age: "+age);
    }
}

This can not be inherited

public class Demo4  {
    private String name;
    private int age;
    private double sal;

    private Demo4(String name, int age) {
        this.name=name;
        this.age=age;
    }

    Demo4(String name) {
        this.name=name;
    }

    private Demo4() {
        this("unknown", 20);
        this.sal=2000;
    }

    void show() {
        System.out.println("name"+name);
        System.out.println("age: "+age);
    }
}
È stato utile?

Soluzione

why a class can not be subclassed if i declare its default constructor as private

The constructor in the subclass must call a super constructor (which could be implicit or explicit), in order to fully construct the object. The super constructor call chain goes all the way up till the Object class, the super class of all classes in Java.

If any of the super constructor is not visible to the subclass then there is no way to fully construct then object.

One way to get around this is to make the constructor in the super class protected. That way the super constructors are only visible to the subclasses.

Altri suggerimenti

If ANY of the constructors in the superclass are accessible, you can subclass it, just call the accessible super constructor with super(..) in your subclass's constructor.

I would be able to subclass your second example like this:

     super("A string");

A little bit of extra information, for default constructors there is always an implicit call to super(). Conversely for a non default constructor there should be an explicit call (if there isn't an accessible constructor in the parent class) to a constructor of the parent class that is accessible, or to constructor of the same class. For example

package com.test;

class A {

    private A() {
      //implicit call to java.lang.Object.super()
    }

    public A(String a) {
      //implicit call to java.lang.Object.super()
    }
}

public class B extends A {

    public B(String a, String b) {
        this(a);
    }

    public B(String a) {
        super(a);
    }

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top