Prev Next

Java / Constructor

Can the inner class with private constructor be extended?

Yes.

package org.javatutorials.accessModifer.protectedPackage;

public class PrivateConstructorInnerClass {

	   class Parent {
	        private Parent() {
	            System.out.println("Hello from Parent");
	        }
	    }

	     class Child extends Parent {
	        public Child() {
	        	System.out.println("Hello from Child");
	        }
	    }

	    public static void main(String[] str) {
	       PrivateConstructorInnerClass pc = new PrivateConstructorInnerClass();
	       pc.new Child();
	    }
}

Output:

Hello from Parent
Hello from Child

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is a Constructor? Rules for creating a Java constructor. Types of Java constructor. Java Default Constructor. Java parameterized constructor. Advantages of parameterized constructor. What happens when the developer does not implement default constructor? Purpose of default constructor. Constructor Overloading in Java. Copy-Constructor. Difference between constructor and method in Java. What are pass by reference and pass by value? Can constructor perform other tasks instead of initialization? Can a constructor call/invoke a static method? Can a constructor invoke a non-static/instance method? Can abstract class have constructors? Can the class with private constructor be extended? What is the access modifier of the Default constructor? Can the inner class with private constructor be extended? Can you use this() and super() both together in a constructor? Can constructor be synchronized in Java? Is constructor inherited? Can a constructor be final? Can we call the constructor of a class more than once for an object in Java? Is constructor called when cloning in Java? Different ways to create an object in Java. Difference between Class.newInstance() and Constructor.newInstance(). How to Initialize class with Class.forName() that has parameterized constructor? Can you define a method with the same name as the class? What happens when we add a return type to the constructor? Is Constructor definition mandatory? If a class has an explicit constructor, does compiler create default constructor? Can we throw exceptions from a constructor? What are the access modifiers that cannot be applied to a constructor? What is constructor chaining? How do I call subclass constructor from superclass constructor? What is No-arg constructor or default constructor.? Can you declare Constructor as final in Java?
Show more question and Answers...

Interface

Comments & Discussions