2

I am clear with using private, default and public access modifiers on a public class's constructor.

If constructor is private, the class object can be created in that class only.

If constructor is default, the class object can be created in that package's classes only.

If constructor is public, the class object can be created in any class.

I am not clear with protected Constructor. Example:

Default Constructor

package com.test;
public class PracticeParent {
    PracticeParent(){       //Constructor with default access modifier
        System.out.println("PracticeParent default constructor");
    }
}

package com.moreTest;
import com.test.PracticeParent;
public class Test extends PracticeParent {  //Error. Implicit super constructor PracticeParent() is not visible for default constructor. Must define an explicit constructor
    public static void main(String[] args) {
        PracticeParent pp=new PracticeParent();     //Error. The constructor PracticeParent() is not visible
    }
}

I understand these errors. Since the ParentPractice class is in other package and its constructor is default, it is not visible to Test class default constructor which call super() implicitly. Also, its object can't be created due to non visibility of constructor.

Protected Constructor

But with class Test extending ParentPractice and ParentPractice having protected constructor, there is no first error i.e. error in calling super() implicitly from Test's default constructor. Implicit super constructor PracticeParent() is visible for Test's default constructor but PracticeParent object can not be created. Error is shown that the constructor PracticeParent() is not visible.

package com.test;
public class PracticeParent {
    protected PracticeParent(){     //Constructor with protected access modifier
        System.out.println("PracticeParent default constructor");
    }
}

package com.moreTest;
import com.test.PracticeParent;
public class Test extends PracticeParent {  //No Error
    public static void main(String[] args) {
        PracticeParent pp=new PracticeParent();     //Error. The constructor PracticeParent() is not visible
            /*Test t=new Test();     outputs "PracticeParent default constructor"*/
    }
}

Why is it that the super() is called but it is not possible to create a new PracticeParent object?

gevorg
  • 4,835
  • 4
  • 35
  • 52
ash
  • 156
  • 3
  • 12

1 Answers1

1
public class Test extends PracticeParent {  //No Error
        public static void main(String[] args) {
            PracticeParent pp=new PracticeParent();     //Error. The constructor PracticeParent() is not visible
            Test t=new Test();     //outputs "PracticeParent default constructor"
        }
    }

What you do here is have Test extend PracticeParent, meaning that Test itself can use the protected constructor, because it inherits it.

PracticeParent pp=new PracticeParent();     //Error. The constructor 

This is just a regular instantiation and still uses, or, tries to use a public constructor, which doesn't exist. It will produce the same effect/result as it would if you had called it anywhere else in your application.

Tim
  • 41,901
  • 18
  • 127
  • 145
  • It means that a subclass can only use the parent class's protected properties that it inherits. No class outside of that subclass can use those inherited properties. Right? – ash Jul 23 '15 at 20:18
  • @Ashish `protected` means only the class itself, and classes that inherit from it can use that method or property. – Tim Jul 23 '15 at 20:22
  • Protected properties of superclass extended by the subclass outside the package becomes implicit to that subclass. It is like the subclass is having its own private properties and no other class can access those properties whatsoever except that subclass. – ash Jul 23 '15 at 20:36
  • @Ashish is that a statement or a question? – Tim Jul 23 '15 at 20:38
  • You can consider it both. Discussion. – ash Jul 23 '15 at 20:41
  • This is not the place to have discussions. It should be done in chat, but I don't have the time right now – Tim Jul 23 '15 at 20:50