Why default constructor is required in a parent class if it has an argument-ed constructor?

what does this() mean in constructor chaining concept?
default constructor java
select the default constructor signature for a student class
constructor in java
constructor overloading in java
constructors
copy constructor in java
has no default constructor

Why default constructor is required(explicitly) in a parent class if it has an argumented constructor

class A {    
  A(int i){    
  }
}

class B extends A {
}

class Main {    
  public static void main(String a[]){
    B b_obj = new B();
  }
}

This will be an error.

Constructors of Sub and Super Classes in Java?, When you do not explicitly write a no-argument constructor for a class, the compiler won't complain as long as objects are built without  Yes it always calls the base class constructor . The order of constructor execution is : Grandparents ->parent ->child If we are defining the parameterized

Why default constructor is required(explicitly) in a parent class if it has an argumented constructor

I would say this statement is not always correct. As ideally its not required.

The Rule is : If you are explicitly providing an argument-ed constructer, then the default constructor (non-argumented) is not available to the class.

For Example :   
class A {    
  A(int i){    
  }
}

class B extends A {
}

So when you write

B obj_b = new B();

It actually calls the implicit constructor provided by java to B, which again calls the super(), which should be ideally A(). But since you have provided argument-ed constructor to A, the default constructor i:e A() is not available to B().

That's the reason you need A() to be specifically declared for B() to call super().

Why is there no default constructor generated if you define an , No-argument constructor: A constructor that has no parameter is known as default If we don't define a constructor in a class, then compiler creates default as the class within which it defined while it is not necessary for the method in java. 2. parameterized constructor – constructor with parameters is known as parameterized constructor. 2.1 Python – default constructor example. Note: An object cannot be created if we don’t have a constructor in our program. This is why when we do not declare a constructor in our program, python does it for us. Lets have a look at the example

Every subclass constructor calls the default constructor of the super class, if the subclass constructor does not explicitly call some other constructor of the super class. So, if your subclass constructor explicitly calls a super class constructor that you provided (with arguments), then there is no need of no arguments constructor in the super class. So, the following will compile:

class B extends A{
     B(int m){
        super(m);
     }
}

But the following will not compile, unless you explicitly provide no args constructor in the super class:

class B extends A{
     int i; 
     B(int m){
        i=m;
     }
}

Constructors in Java, A Constructor with arguments(or you can say parameters) is known as As we discussed in the Java Constructor tutorial that a. class Example{ //Default constructor Example(){ System.out.println("Default Required fields are marked *. Every class that is derived from some other class must call the base class' constructor. A derived class can only be constructed once all base class' are fully constructed. So it doesn't matter if you call the base class' constructor or not.

Assuming that you meant to write class B extends A:

Every constructor has to call a superclass constructor; if it does not the parameterless superclass constructor is called implicitly.

If (and only if) a class declares no constructor, the Java compiler gives it a default constructor which takes no parameters and calls the parameterless constructor of the superclass. In your example, A declares a constructor and therefor does not have such a default constructor. Class B does not declare a constructor, but cannot get a default constructor because its superclass does not have a parameterless constructor to call. Since a class must always have a constructor, this is a compiler error.

Java - parameterized constructor with example, Default Constructor – A constructor that accepts no parameter is called It is not necessary to have a constructor block in your class definition. The motivation is that, if there's a lot of calculation in the parent class default constructor, then I don't want to have to do those calculation and only have them replaced by those computed by child class right after.

Why default constructor is required(explicitly) in a parent class if it 
has an argumented constructor

Not necessarily!

Now in your class B

class B extends A {
}

you have not provided any constructor in Class B so a default constructor will be placed. Now it is a rule that each constructor must call one of it's super class constructor. In your case the default constructor in Class B will try to call default constructor in class A(it's parent) but as you don't have a default constructor in Class A(as you have explicitly provided a constructor with arguments in class A you will not have a default constructor in Class A ) you will get an error.

What you could possibly do is

Either provide no args constructor in Class A.

A()
{
  //no arg default constructor in Class A
}

OR

Explicitly write no args constructor in B and call your super with some default int argument.

B()
{
    super(defaultIntValue);
}

Bottom line is that for an object to be created completely constructors of each parent in the inheritance hierarchy must be called. Which ones to call is really your design choice. But in case you don't explicitly provide any java will put default constructor super() call as 1st line of each of your sub class constructors and now if you don't have that in superclass then you will get an error.

Parameterized Constructor In Java, Java: Extending a Class that has Explicit Constructors Once you define a constructor in a class, the default constructor is not Otherwise, the default constructor simply invokes the superclass constructor with no arguments. Why default constructor is required (explicitly) in a parent class if it has an argumented constructor I would say this statement is not always correct. As ideally its not required. The Rule is : If you are explicitly providing an argument-ed constructer, then the default constructor (non-argumented) is not available to the class.

Java: Extending a Class that has Explicit Constructors, We can have any number of constructors in a class but we should see their By default, the default constructor is called when we create an object. If we want an argumented constructor of the parent to be called with a child class We need not to exclusively call the constructor, it is automatically called when the object is  A constructor with no parameters is called a default constructor. A default constructor has every instance of the class to be initialized to the same values. The default constructor initializes all numeric fields to zero and all string and object fields to null inside a class.

Parameterized Constructor In Java, This magical act is called inheritance, and the compiler does most of the work. It can reduce overhead in situations where the object doesn't need to be The above example has default constructors; that is, they don't have any arguments. If you don't call the base-class constructor in BoardGame( ), the compiler will  Instead of inheriting constructors by the derived class, it is only allowed to invoke the constructor of base class. In C#, when we are working with the constructor in inheritance there are two different cases arise as follows: Case 1: In this case, only derived class contains a constructor. So the objects of the derived class are instantiated

Thinking in Java, 3rd ed. Revision 2.0: 6: Reusing Classes, The constructor which does not have any parameter. If there is no constructor in a class, Compiler automatically creates a default Why do we need to use a constructor in Java when we can define the same Parameter i z ed Constructor. can I properly call a parent class constructor in the derived class's constructor? Explicit constructors. If a class has a constructor with a single parameter, or if all parameters except one have a default value, the parameter type can be implicitly converted to the class type. For example, if the Box class has a constructor like this: Box(int size): m_width(size), m_length(size), m_height(size){}

Comments
  • My understanding is that the default constructor is an implicit parameterless constructor. It is only automatically added to a class when no other constructors exist. This would indicate an incongruency with your initial sentence: by virtual of what a default constructor is, it cannot be explicit. Jon Skeet really hit the nail on the head with the super() hidden in subclass default constructors.
  • @user358099: To make it clear that it was trying to call the parameterless superconstructor.
  • While not really relevant to OP's question, I believe that in general, a not altogether terrible third option would be to define an explicit parameter-taking constructor in B that passes through its arguments via a call to super to the explicit constructor in A. It seems like defining a parameterless constructor in either A or B might not be so great if no meaningful default value(s) can be determined.
  • Note that the parameterless constructor in A can be protected so that calling code is forced to use the constructor with the parameter.
  • @JonSkeet I have a similar case where I have a class A with a defined parameterized constructor, and a child class B extends A, which when created empty for starters gave me compile-time error and my Eclipse IDE in quick-suggestions told me to create a parameterized-constructor in class B, after which I got no more compile-time errors. I didn't have to create a parameterless constructor (and worry about the default value) in either class A or B as suggested in the answer. Isn't this a better third option? One difference, I do see is that my base class A implements H.
  • @Anurag: The OP's question suggested (the way I read it, anyway) that they wanted new B() to work. Yes, if you're happy to provide a parameterized constructor in B, that's fine.
  • Wouldn't this work when super is replaced with this(m)? I think this(m) would call parameterized constructor of parent class just like super(m).
  • @Shashi this() will only invoke base class constructor. For parent class, we have to use super()
  • its a compiler error my doubt is ...why parent class should have a default constructor explicitly...while it has an argumented constructor
  • @user358099: Because the implicit default constructor is only created if there is not ecplicit constructor. And that's because a programmer may not want his class to have a parameterless constructor, so it would be very bad language design to always implicitly create one.
  • really a valuable answer. But a small clarification why super() is added implicitly by compiler.
  • @user358099 I have added a new answer to your edited question.