4

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 ?

Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
  • Questions like "Why does the language work this way" can only be answered by the language's designer, not by this community. In Java, constructors are not inherited. That is a fact. Anything beyond that would be speculation. – RealSkeptic Sep 26 '16 at 06:52
  • 1
    Possible duplicate of [Java Constructor Inheritance](http://stackoverflow.com/questions/1644317/java-constructor-inheritance) – shmosel Sep 26 '16 at 06:54
  • "Is there any special reason, why java compiler wants me to define constructor in child class ?" You mean any other reason other than James Gosling and the Java standard committee decided it is better this way? No, there isn't one. – Adrian Colomitchi Sep 26 '16 at 06:54
  • @AdrianColomitchi, I wanted to know the reason why James Gosling and Java Standard committe went with that design design ? – Mangu Singh Rajpurohit Sep 26 '16 at 07:00
  • Side note: don't use raw types. – shmosel Sep 26 '16 at 07:03
  • If you want to know why they decided that, you have to ask them or search for any written material on the Internet or off it. The community here is of programmers, not historians. We know *how* to use the language, not the *why*. – RealSkeptic Sep 26 '16 at 07:03

5 Answers5

4

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);
}
Eran
  • 387,369
  • 54
  • 702
  • 768
1

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);
     }
}
Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
1

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() {

    }
}
Wojtek
  • 1,288
  • 11
  • 16
1

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

SM ANSARI
  • 335
  • 3
  • 13
0

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 .

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43