Mangu Singh Rajpurohit
Mangu Singh Rajpurohit

Reputation: 11420

overriding parameterized constructors in sub-classes in java

i'm just beginning my story with Java, however I have some OOP experience with Python. I have the following class hierarchy :

class A {
   public A() {};
   public A(final java.util.Map dict) {};
}

class B extends A {
}

public class Main {
  public static void main() {
     java.util.Map d = new java.util.HashMap();
     B b = new B(d);
  }
}

The attached code causes the following error:

Main.java:16: error: constructor B in class B cannot be applied to given types;
     B b = new B(d);
           ^
  required: no arguments
  found: Map
  reason: actual and formal argument lists differ in length
1 error

What my expectation is, since required constructors are already defined in the base class, is no-need to define another constructor. Why do I need to define one for sub-class, since they don't do any thing special, apart from calling super() ? Is there any special reason, why java compiler wants me to define constructor in child class ?

Upvotes: 4

Views: 1734

Answers (5)

Wojtek
Wojtek

Reputation: 1308

In Java - when you extend some A-class with a constructor, the B-subclass's constructor always must invoke it. In case A gets 0 arguments - you do not have to write anything - compiller will call it automatically. In case there are more arguments, you have to pass them to base constructor with super(). Here are some examples:

public class A {
    public A(int something) {

    }
}

public class B extends A {
    public B(int something) {
        super(something);
    }
}

public class C extends A {
    public C() {
        super(999);
    }
}

//--------------------------------------------//

public class A {
    public A() {

    }
}

public class B extends A {
    public B(int something) {
        super();
    }
}

public class C extends A {
    public C(int something) {
    }
}

public class D extends A {
    public D() {
        super();
    }
}

public class E extends A {
    public E() {

    }
}

Upvotes: 1

SM ANSARI
SM ANSARI

Reputation: 385

Default constructor is auto created when there is no constructor is defined. you have to add default constructor in B to invoke the A's default constructor.

so your code needs the following

class B extends A
{
    B(){
      super();  // calling super class  default constructor
    }
    B(Map dict) {
        super(dict); // calling super class single parameter(map) constructor
    }
}

So that

B b = new B(); //invokes A's Default Constructor
B b = new B(d); //invokes A's Parameter Consturctor

Hope this is clear

Upvotes: 1

Amer Qarabsa
Amer Qarabsa

Reputation: 6574

Why constructors are not inheritied? constructors are used to initialize the objects, usually when inheriting from a class you tend to use the common functionality yet the creation can differ, so what would be the use of extending the constructor as well, you will be just having a duplicate of the super class, in this case it would be a better practice to have all your common functionality in one class and have a Utility class which contains the different functionalties .

Upvotes: 0

Sibaditya Maiti
Sibaditya Maiti

Reputation: 121

You need to declare the Parametrised constructor in your class B and call super from it if you don't want to initialize any thing in your class B.

Something like :

import java.util.*; 

class B extends A
{
    B(Map dict) {
         super(dict);
     }
}

Upvotes: 1

Eran
Eran

Reputation: 393781

When you instantiate a class, a constructor of that class must be invoked.

When a Java class has no constructor, the compiler automatically generates a parameter-less constructor, which allows you to instantiate the class with no parameters (in your example, it allows new B()).

Since you try to instantiate your B class with a parameter, you must declare an appropriate constructor which invokes the super class constructor of your choice :

public B(Map dict) {
    super(dict);
}

Upvotes: 4

Related Questions