-3

how this protected and private constructors combo works?

class ClassA {
    public int numberOfInstances;
    protected ClassA(int numberOfInstances) {
        this.numberOfInstances = numberOfInstances;
    }
}
public class ExtendedA extends ClassA {
    private ExtendedA(int numberOfInstances) {
         super(numberOfInstances);
    }
}
public static void main(String[] args) {
    ExtendedA ext = new ExtendedA(420);
    System.out.print(ext.numberOfInstances);
}
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • does your code compile? –  Aug 23 '14 at 04:53
  • Where did you get this code from? – Nabin Aug 23 '14 at 04:54
  • 1
    This does not work. At first I was thinking that maybe this whole code-snippet was supposed to be wrapped in some sort of `public class Foo { ... }` (in which case any code inside `Foo` could access the `private` members, including constructors, of any class declared inside `Foo`), but then I realized that if that were so, then your static method `main` would not be able to instantiate your non-static nested class `ExtendedA`. So, -1. Please post a minimal, complete, verifiable example that demonstrates your issue. – ruakh Aug 23 '14 at 05:01
  • @ruakh there can not be any other class foo as you said because ExtendedA is already public and no other class can be public – SpringLearner Aug 23 '14 at 05:05
  • @SpringLearner That's not true because if all these classes were wrapped in a `public class Foo { ... }` then `ExtendedA` would be a Nested class and those are permitted to be private, public, protected, or package private. http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html – Kyle Bridenstine Aug 23 '14 at 05:47
  • @Bridenstine have a look at this answer http://stackoverflow.com/questions/13329012/why-cant-two-public-classes-be-defined-in-one-file-in-java' – SpringLearner Aug 23 '14 at 05:50
  • @SpringLearner Those are two stand-alone classes in the same .java file; That is not valid in Java. What I said is a public class inside a public class a.k.a a `Nested` class or an `Inner` class is permitted in Java. The link I gave you is the official documentation from Oracle explaining this. – Kyle Bridenstine Aug 23 '14 at 05:53

1 Answers1

2

I have seen this question and it is one of the many sample question to prepare you for SUN Certication. You can find the question in this doc: Sun Certification.

There are some sites that have this question, however they are missing components or is written incorrectly which will not compile, however the answer is still 420;

I will assume the the OP copy and pasted the code from one of those sites.

The proper code should be two separate class written as:

class ClassA {

    public int numberOfInstances;

    protected ClassA(int numberOfInstances) {

        this.numberOfInstances = numberOfInstances;
    }
}

public class ExtendedA extends ClassA {

    private ExtendedA(int numberOfInstances) {

         super(numberOfInstances);
    }

    public static void main(String[] args) {

        ExtendedA ext = new ExtendedA(420);
        System.out.print(ext.numberOfInstances);
    }
}

To answer the question on how this works:

The child class can access its Parent's constructor by calling super(...) and therefore in this case assigning 420 to numberOfInstances. If you are not familiar with inheritance, I recommend you research this on your own, however, to make is short, the child class or subclass inherits all of the public and protected members of its parent.

So when you call ext.numberOfInstances, the output will be 420. The value is assigned by first calling the subclass contructor, then the subclass constructor call the parent constructor using super(420) which then assign the value to numberOfInstances.

NullEverything
  • 450
  • 2
  • 5