Constructors and Inheritence - java

I was wondering how to use a superclass constructor in a subclass but need to instantiate fewer attributes in the subclass. Below are the two classes. I'm not even sure if I'm doing things right currently. In the second class, there's an error that says "Implicit super constructor PropertyDB() is undefined. Must explicitly invoke another constructor." Note that this code is obviously incomplete and there is code that's commented out.
public abstract class PropertyDB {
private int hectares;
private String crop;
private int lotWidth;
private int lotDepth;
private int buildingCoverage;
private int lakeFrontage;
private int numBedrooms;
private int listPrice;
//public abstract int getPricePerArea();
//public abstract int getPricePerBuildingArea();
public PropertyDB(int newHectares, String newCrop, int newLotWidth, int newLotDepth,
int newBuildingCoverage, int newLakeFrontage, int newNumBedrooms, int newListPrice){
hectares = newHectares;
crop = newCrop;
lotWidth = newLotWidth;
lotDepth = newLotDepth;
buildingCoverage = newBuildingCoverage;
lakeFrontage = newLakeFrontage;
numBedrooms = newNumBedrooms;
listPrice = newListPrice;
}
}
public class FarmedLand extends PropertyDB{
public FarmedLand(int newHectares, int newListPrice, String newCorn){
//super(270, 100, "corn");
hectares = newHectares;
listPrice = newListPrice;
corn = newCorn;
}
}

implicit constructor PropertyDB() exists only if you do not define any other constructors, in which case you will have to explicitly define PropertyDB() constructor.
The reason you see this error "Implicit super constructor PropertyDB() is undefined. Must explicitly invoke another constructor." is that in your public FarmedLand(int newHectares, int newListPrice, String newCorn) constructor, super() is automatically called as the first statement, which does not exist in your superclass.
Here's a simplified example:
public class A { }
can be instantiated by using A a = new A() because public A() { } is an implicit constructor of class A.
public class A {
public A(int z) { /* do nothing*/ }
}
can not be instantiated using A a = new A() because by defining an explicit constructor public A(int z) the implicit one is no longer available.
Moving onto constructors and inheritance, from Java Language Specification section 8.8.7:
If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.
So in your case the first statement executed in public FarmedLand(int newHectares, int newListPrice, String newCorn) constructor is an implicit call to super();, which in your case is not defined implicitly (there's already a public PropertyDB(int, String, ...) constructor defined) or explicitly (it's not in the source code)

When you have a derived class which extends a base class, the base class always gets constructed before the derived class. If you don't explicitly specify which constructor to use for the base class (like in your example), Java assumes you meant the parameterless constructor (in your case the PropertyDB() constructor).
But wait - PropertyDB doesn't have a parameterless constructor! So your only option is to use super so the Java compiler knows which constructor to call for the base class. In your case, there's only one constructor to choose from, so you have to use it with all 8 arguments. If you want to use less arguments, you either have to specify "default" values (like pass a 0), or else define more constructors for PropertyDB which take fewer arguments.

The error you're witnessing is due to the fact that the PropertyDB class doesn't have a default (no-arguments) constructor. Either create it in PropertyDB or call the existing superclass constructor PropertyDB(int newHectares, String newCrop, int newLotWidth, int newLotDepth, int newBuildingCoverage, int newLakeFrontage, int newNumBedrooms, int newListPrice) from the FarmedLand constructor using super.

Use super(newHectares, newCorn, 0, 0, 0, 0, newListPrice); instead. 0 is the default value of the int anyway.

Your superclass only has one constructor, so your subclass constructor must call it. There's no way around this: the superclass has (for example) a lotWidth field, so the subclass necessarily has that field, and the superclass initializes that field in its constructor, so it will necessarily be initialized in the subclass.
So, unless you modify the superclass in some way, you're going to have to call super(...) as the very first thing in the subclass constructor, specifying values for all its parameters.

Related

Difference between super(variableName) ; and super.variableName

What is the Difference between super(variable-name); and super.variableName = something; in a constructor, when you want to initialize the parameters and you wanna assign one of them to a variable of a parent class?
for example i want to implement the constructor of the "Zahnradfraese" and it takes the parameter "int Kennung" and this parameter should be assigned to the attribute "kennung" of the parent class "Produktionmittel"
Should I always use super when I wanna call a variable from this parent class or I just use it if I have another variable with the same name in the child class?
What is the difference between super(variableName); and super.variableName = something;?
method() (here, super(variableName)) is a method invocation (here, a parent's constructor invocation).
super.variableName = something; is an assignment to a parent's field.
Should I always use super when I wanna call a variable from this parent class or I just use it if I have another variable with the same name in the child class?
super(variableName) can initialise the inner state of the parent, particularly super.variableName. It is reasonable to initialise a super.variableName before accessing it. Both ways you listed can be utilised for that. Just make sure there is no code duplication.
I want to implement the constructor of the Zahnradfraese and it takes the parameter int Kennung and this parameter should be assigned to the attribute kennung of the parent class Produktionmittel.
Add a constructor to Produktionmittel which takes an int
public Produktionmittel(int i) {
kennung = i;
}
and call it from the child:
public Zahnradfraese(int kennung) {
super(kennung);
}
super(variable_name) represents a constructor call and should be first line in the constructor. Whereas super.variableName = something; means you are assigning a value to the instance variable of the parent class from the child class using super which is used to refer parent class objects.
Now in your case: as per given class-diagram
the class Zahnradfraese has a constructor which takes int Kennung argument. Also, kennung is the parent-class and has no constructor and instead it has method setKennung(). So you can do super.setKennung(kennung) from inside the constructor of Zahnradfraese class. You can also declare a constructor inside kennung but that would mean deviating from the class-diagram which has setter and getter methods and no constructor.
public class Zahnradfraese extends Kennung{
public Zahnradfraese(int kennung){
super.setKennung(kennung);
}
}
So super(variableName) is invoking your parent class one arg constructor, and that logic gets executes
super.variableName = something; is assigning something value to parent class variable variableName
super() is a keyword which is used to call the constructor in the parent class and it must be called from inside the constructor of the child class. Also it must be the first statement.
Where as super.s is used to set the variable s (which is declared in the parent class) from the child class and it doesn't have restrictions as above.
See below example:
class Test {
int s;
Test(int d) {
}
}
class T extends Test {
T() {
super(8);
int d = 99;
super.s = 00;
}
void ss() {
super.s = 99;
}
}
super(arg) invokes the constructor of the super class, setting the variable just sets the variable. (The constructor might contain more logic than just assigning a variable, which you bypass with the second way)
Simple example:
public class P{
protected String variable1;
private boolean variableInitialized = false;
public P (String s){
this.variable1 = s;
this.variableInitialized=true;
}
}
public class C extends P{
}
calling super("x") within C will also set the boolean flag, as the parent class "might expect" it. Calling super.variable1="x" will not affect the boolean flag, and you can't change it, cause it's private.
As a rule of the thumb i'd say: If there is a dedicated constructor for a certain variable, it seems worth using it, unless you exactly want to override that implementation.

Create default Constructor + parameterized constructor [duplicate]

What exactly is a default constructor — can you tell me which one of the following is a default constructor and what differentiates it from any other constructor?
public Module() {
this.name = "";
this.credits = 0;
this.hours = 0;
}
public Module(String name, int credits, int hours) {
this.name = name;
this.credits = credits;
this.hours = hours;
}
Neither of them. If you define it, it's not the default.
The default constructor is the no-argument constructor automatically generated unless you define another constructor. Any uninitialised fields will be set to their default values. For your example, it would look like this assuming that the types are String, int and int, and that the class itself is public:
public Module()
{
super();
this.name = null;
this.credits = 0;
this.hours = 0;
}
This is exactly the same as
public Module()
{}
And exactly the same as having no constructors at all. However, if you define at least one constructor, the default constructor is not generated.
Reference: Java Language Specification
If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.
Clarification
Technically it is not the constructor (default or otherwise) that default-initialises the fields. However, I am leaving it the answer because
the question got the defaults wrong, and
the constructor has exactly the same effect whether they are included or not.
A default constructor is created if you don't define any constructors in your class. It simply is a no argument constructor which does nothing. Edit: Except call super()
public Module(){
}
A default constructor is automatically generated by the compiler if you do not explicitly define at least one constructor in your class. You've defined two, so your class does not have a default constructor.
Per The Java Language Specification Third Edition:
8.8.9 Default Constructor
If a class contains no constructor
declarations, then a default
constructor that takes no parameters
is automatically provided...
Hi. As per my knowledge let me clear the concept of default constructor:
The compiler automatically provides a no-argument, default constructor
for any class without constructors. This default constructor will call
the no-argument constructor of the superclass. In this situation, the
compiler will complain if the superclass doesn't have a no-argument
constructor so you must verify that it does. If your class has no
explicit superclass, then it has an implicit superclass of Object,
which does have a no-argument constructor.
I read this information from the Java Tutorials.
Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided.
The only action taken by the implicit default constructor is to call the superclass constructor using the super() call. Constructor arguments provide you with a way to provide parameters for the initialization of an object.
Below is an example of a cube class containing 2 constructors. (one default and one parameterized constructor).
public class Cube1 {
int length;
int breadth;
int height;
public int getVolume() {
return (length * breadth * height);
}
Cube1() {
length = 10;
breadth = 10;
height = 10;
}
Cube1(int l, int b, int h) {
length = l;
breadth = b;
height = h;
}
public static void main(String[] args) {
Cube1 cubeObj1, cubeObj2;
cubeObj1 = new Cube1();
cubeObj2 = new Cube1(10, 20, 30);
System.out.println("Volume of Cube1 is : " + cubeObj1.getVolume());
System.out.println("Volume of Cube1 is : " + cubeObj2.getVolume());
}
}
General terminology is that if you don't provide any constructor in your object a no argument constructor is automatically placed which is called default constructor.
If you do define a constructor same as the one which would be placed if you don't provide any it is generally termed as no arguments constructor.Just a convention though as some programmer prefer to call this explicitly defined no arguments constructor as default constructor. But if we go by naming if we are explicitly defining one than it does not make it default.
As per the docs
If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.
Example
public class Dog
{
}
will automatically be modified(by adding default constructor) as follows
public class Dog{
public Dog() {
}
}
and when you create it's object
Dog myDog = new Dog();
this default constructor is invoked.
default constructor refers to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors.
If there's no constructor provided by programmer, the compiler implicitly declares a default constructor which calls super(), has no throws clause as well no formal parameters.
E.g.
class Klass {
// Default Constructor gets generated
}
new Klass(); // Correct
-------------------------------------
class KlassParameterized {
KlassParameterized ( String str ) { //// Parameterized Constructor
// do Something
}
}
new KlassParameterized(); //// Wrong - you need to explicitly provide no-arg constructor. The compiler now never declares default one.
--------------------------------
class KlassCorrected {
KlassCorrected (){ // No-arg Constructor
/// Safe to Invoke
}
KlassCorrected ( String str ) { //// Parameterized Constructor
// do Something
}
}
new KlassCorrected(); /// RIGHT -- you can instantiate
If a class doesn't have any constructor provided by programmer, then java compiler will add a default constructor with out parameters which will call super class constructor internally with super() call. This is called as default constructor.
In your case, there is no default constructor as you are adding them programmatically.
If there are no constructors added by you, then compiler generated default constructor will look like this.
public Module()
{
super();
}
Note: In side default constructor, it will add super() call also, to call super class constructor.
Purpose of adding default constructor:
Constructor's duty is to initialize instance variables, if there are no instance variables you could choose to remove constructor from your class. But when you are inheriting some class it is your class responsibility to call super class constructor to make sure that super class initializes all its instance variables properly.
That's why if there are no constructors, java compiler will add a default constructor and calls super class constructor.
When we do not explicitly define a constructor for a class, then java creates a default constructor for the class. It is essentially a non-parameterized constructor, i.e. it doesn't accept any arguments.
The default constructor's job is to call the super class constructor and initialize all instance variables. If the super class constructor is not present then it automatically initializes the instance variables to zero. So, that serves the purpose of using constructor, which is to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object.
Once we define our own constructor for the class, the default constructor is no longer used. So, neither of them is actually a default constructor.
When you don’t define any constructor in your class, compiler defines default one for you, however when you declare any constructor (in your example you have already defined a parameterized constructor), compiler doesn’t do it for you.
Since you have defined a constructor in class code, compiler didn’t create default one. While creating object you are invoking default one, which doesn’t exist in class code. Then the code gives an compilation error.
When you create a new Module object, java compiler add a default constructor for you because there is no constructor at all.
class Module{} // you will never see the default constructor
If you add any kind of constructor even and non-arg one, than java thing you have your own and don't add a default constructor anymore.
This is an non-arg constructor that internelly call the super() constructor from his parent class even you don't have one. (if your class don't have a parent class than Object.Class constructor will be call)
class Module{
Module() {} // this look like a default constructor but in not.
}
A default constructor does not take any arguments:
public class Student {
// default constructor
public Student() {
}
}
I hope you got your answer regarding which is default constructor.
But I am giving below statements to correct the comments given.
Java does not initialize any local variable to any default value. So
if you are creating an Object of a class it will call default
constructor and provide default values to Object.
Default constructor provides the default values to the object like 0,
null etc. depending on the type.
Please refer below link for more details.
https://www.javatpoint.com/constructor

How to force subclasses to set a variable in java?

I have a class which defines all of the basic parameters for a given screen. From here every screen in the application is a subclass of this. I need every screen (i.e. subclass) to set the value of a variable in its implementation (namely, each screen must define what level it is in a navigation tree).
Also, ideally, this variable should be final when it is set in the sub classes (I realise this probably isn't possible).
What is the best way to go about this? Is there a way to correctly enforce this type of behaviour in Java?
#pst's comment lead to this solution.
This can't be done with a variable. But an abstract class can require that a particular method is implemented: this method could return the applicable value
From declaring an abstract function to set or return the variable, you can force any subclass to implement it correctly.
Next, the function must be called by every single subclass of the outer class. This implies that it must be done somewhere in the outer class. This can be done in the no-argument constructor of the outer class without having to worry about subclasses calling super:
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
(Java docs: Super)
Based on that, this solution will hold up and correctly force the variable to be set as long as either:
No other constructor is created in the superclass (hence super can't be used in a subclass to call a different constructor instead)
All other constructors in the superclass still call the default constructor internally
The code:
Superclass:
public abstract class SuperClass {
// Variable all inner classes must set
static int myVar = 0;
public SuperClass() {
myVar = giveValue();
}
public abstract int giveValue();
}
Subclass:
public class SubClass extends SuperClass {
#Override
public int giveValue() {
return 5; // individual value for the subclass
}
}
Rather than enforce that child class instances initialize the fields, you could follow a strategy of composition by having your parent class constructor take a parameter implementing an interface that provides the fields you wish to have initialized.
class Screen {
final Configuration config;
Screen(Configuration config) {
this.config = config;
}
// or
Screen(ConfigFactory configFactory) {
this.config = configFactory.make();
}
}
interface ConfigFactory {
Configuration make();
}
I would caution against requiring a subclass instance initializing the configuration, say using an abstract method implementation. The assignment in the parent class constructor occurs before the subclass instance is initialized, implicitly making proper computation of the configuration static.
If the computation isn't static, you risk null references or NullPointerExceptions by developers following you (or yourself if your memory is less than perfect). Make it easier on your collaborators (and yourself) and make the constraints explicit.
As mentioned by #Ketan in #B T's answer, invoking an overridable method from constructor is not especially a good practice (https://help.semmle.com/wiki/display/JAVA/Non-final+method+invocation+in+constructor)
One way to avoid this problem consists in having an abstract (protected) getter for the field. Hence the superclass doesn't have the field anymore, but it is still accessible in the super class using the getter. Each subclass is forced to declare the field because it must override the abstract getter.
Superclass:
public abstract class SuperClass {
public SuperClass() {}
protected abstract int getMyVar();
public void functionUsingMyVar(){
int a = 12 + getMyVar();
}
}
Subclass1:
public class SubClass1 extends SuperClass {
private int myVar;
public SubClass1() {
super();
myVar = 1;
}
#Override
protected int getMyVar(){
return myVar;
}
}
Subclass2:
public class SubClass2 extends SuperClass {
private int myVar;
public SubClass2() {
super();
myVar = 1;
}
#Override
protected int getMyVar(){
return myVar;
}
}
instead of having for the superclass (where giveValue() is overridable and called in the constructor) :
public abstract class SuperClass {
private int myVar;
public SuperClass() {
myVar = giveValue();
}
protected abstract int giveValue();
public void functionUsingMyVar(){
int a = 12 + myVar;
}
}

Constructor usages in Java

Can anybody explain the differences between these constructors. See this;
public MyClass() {
}
and
public MyClass() {
this();
}
and
public MyClass() {
super();
}
Second is not allowed to be declared in java because you are making recursive call to default constructor only. Also by default constructor has always super() method which is inserted by compiler if not specified explicitly.
public MyClass() { }
declares an empty no-arg constructor. If you declare no constructor for a class, the compiler effectively inserts this conrtsuctor for the class.
public MyClass() { this(); }
will not compile.
public MyClass() { super(); }
is a constructor which invokes the default constructor of the super-class. This will compile only if the super-class has a default constructor, or no constructor (in which case, the compiler inserts a default constructor anyways, as mentioned above). Also, leaving out the super(); call has the same effect.
I think that in the second case you probably meant to write:
public MyClass() {
super(); // not "this();"
}
Either way, your second case is a compilation error. The JLS says this:
"It is a compile-time error for a constructor to directly or indirectly invoke itself through a series of one or more explicit constructor invocations involving this." JLS 8.8.7
In your example, this() is an explicit constructor invocation that directly invokes the same constructor.
Finally, there is no semantic difference between the first and third forms. The first form has an implicit invocation of the no-args constructor of the superclass, and the third for just makes that invocation explicit.
Some people prefer to write the form with an explicit invocation for the sake of readability, but IMO it doesn't really make any difference.
It is very simple
Let me make you understand
first
public MyClass() { }
is simply a default public constructor
But when you write this
public MyClass() { this(); }
that means you are applying constructor chaining. Constructor Chaining is simply calling another constructor of the same class. This must be the first statement of the Constructor. But in your scenario you are passing nothing in this(); that means you are again calling the same constructor which will result in infinite loop. your class may have another constructor like this
public MyClass(int a) { }
then you may have called this
public MyClass(){ this(10); }
the above statement will make you to jump to the constructor receiving the same arguments that you have passed
Now,
public Myclass(){ super(); }
signifies that you are calling the constructor of the super class which is inherited by the class MyClass. the same scenario occurs here, you have passed nothing in the super(); which will call the default constructor of the Super class.
Lets look at the three type of constructor calls one by one.Each one has special purpose associated with them. But the concept is simple and very interesting to explore.
Case 1 :- This is default constructor with no arguments
public class MyClass{
public MyClass(){
}
}
The compiler will automatically supply a no-argument constructor for you if you do not write one.Thus if you write public class MyClass{ } .
This is equivalent to writing
public class MyClass{
MyClass(){ }
}
Points to be noted :-
Compiler automatically provide "super" when you do not use super "as the first line of the constructor". NOTE:- For "super" and "this" when you use in constructors always write them in the first line of your constructor code otherwise compiler will give error.
Even when you do not extend MyClass here compiler will give call to constructor to super Object class which is the root class of every class you create.
Case 2:- The use of "this".
Constructors use "this" to refer to another constructor in the same class with a different parameter list.
public MyClass() {
this();
}
So the above code will give compilation error "Recursive Constructor Invocation" as it is calling the same constructor and will end up in an infinite loop. To make this work you can instead write
public MyClass(String str){
System.out.println("constructor with arg "+ str);
}
public MyClass(){
this("Cons"); // note that "this" has to be at first line otherwise compiler gives error
System.out.println("constructor with no arg");
}
Here you can see the importance of "this" in constructors. It is used to call constructors from another constructor in the same function.
Case 3:- Use of "Super". Super is used to make a call to no-arg constructor of super call. Rest information is mentioned in Case 1 already.
public MyClass() {
super();
}
In the first case the constructor will insert a silent call to no argument constructor of the super-class which I guess in this case will be the Object class. In the second case the this() call will call the no argument constructor of class. It can sometimes cause an error too.
The first is the default empty constructor. It simply does nothing. If you don't have other constructors (with arguments), you don't have to define this sort of constructor at all. It is implicitly made default if no constructors are defined.
The second calls itself which causes a recursive loop, hence is forbidden.
The third calls the default no-args constructor of the super class but does nothing more. The super class is the one that your class extends from, or Object (implicitly extended if you don't specify the extends keyword).
From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation
http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 0, 0);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
But second one leads to recursive constructor invocation and not allowed.
public MyClass() { }
--> this is normal constructor also called default constructor.
public MyClass() {
this();
}
--> You are making constructor recursive. Don't use this way. Well you can make use of this() like :
public MyClass() {
this(1); // always intitalize to 1
}
public MyClass(int i) {
this.i=i;
}
The second one will surely cause an OutOfMemoryError exception due to a stack overflow not compile due to a Recursive constructor invocation error as you're calling the same constructor from itself. Did you mean to write
super();
? In that case both forms are identical.
public MyClass() {
}
Will call the default no-args constructor of the base class implicitly, if it exists.
this() will be used to call MyClass() No-Args Constructor.
Though it will give Recurssion constructor error during Compilation
Eg:
public MyClass{
public MyClass(){
this(5); // Here this(5) will be used to call the MyClass Constructor with
// int value as Parameter
}
public MyClass(int i){
}
}
default constructor doing nothing special. each class in java has it per default until you write your own constructor, then you have to define it by your own again
public A() {}
public A(int value) {}
is the default constructor with a recursive call on it (invalid and shouldnt be used). with this() you call default constructor. for example with this(5) you call a defined constructor which takes an int value as parameter like the example from 1.
public A() { this(5); }
public A(int value) {}
default constructor which calls the default constructor from your super class
abstract class A {
public A() {}
}
class B extends A {
public B() {
/* calls A() */
super();
}

Why default constructor is required in a parent class if it has an argument-ed 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.
There are two aspects at work here:
If you do specify a constructor explicitly (as in A) the Java compiler will not create a parameterless constructor for you.
If you don't specify a constructor explicitly (as in B) the Java compiler will create a parameterless constructor for you like this:
B()
{
super();
}
(The accessibility depends on the accessibility of the class itself.)
That's trying to call the superclass parameterless constructor - so it has to exist. You have three options:
Provide a parameterless constructor explicitly in A
Provide a parameterless constructor explicitly in B which explicitly calls the base class constructor with an appropriate int argument.
Provide a parameterized constructor in B which calls the base class constructor
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;
}
}
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().
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.
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.
There are a few things to be noted when using constructors and how you should declare them in your base class and super class. This can get somewhat confusing solely because there can be many possibilities of the availability or existence of constructors in the super class or base class. I will try to delve into all the possibilities:
If you explicitly define constructors in any class(base class/super class), the Java compiler will not create any other constructor for you in that respective class.
If you don't explicitly define constructors in any class(base class/super class), the Java compiler will create a no-argument constructor for you in that respective class.
If your class is a base class inheriting from a super class and you do not explicitly define constructors in that base class, not only will a no-argument constructor be created for you (like the above point) by the compiler, but it will also implicitly call the no-argument constructor from the super class.
class A
{
A()
{
super();
}
}
Now if you do not explicity type super(), (or super(parameters)), the compiler will put in the super() for you in your code.
If super() is being called (explicitly or implicitly by the compiler) , the compiler will expect your superclass to have a constructor without parameters. If it does not find any constructor in your superclass without parameters, it will give you a compiler error.
Similary if super(parameters) is called, the compiler will expect your superclass to have a constructor with parameters(number and type of parameters should match). If it does not find such a constructor in your superclass, it will give you a compiler error. ( Super(parameters) can never be called implicitly by the compiler. It has to be explicitly put in your code if one is required.)
We can summarize a few things from the above rules
If your superclass only has a constructor with parameters and has no no-argument constructor, you must have an explicit super(parameters) statement in your constructor. This is because if you do not do that a super() statement will be implicitly put in your code and since your superclass does not have a no-argument constructor, it will show a compiler error.
If your superclass has a constructor with parameters and another no-argument constructor, it is not necessary to have an explicit super(parameters) statement in your constructor. This is because a super() statement will be implicitly put in your code by the compiler and since your superclass has a no-argument constructor, it will work fine.
If your superclass only has a no-argument constructor you can refer to the point above as it is the same thing.
Another thing to be noted is if your superclass has a private constructor, that will create an error when you compile your subclass. That is because if you don't write a constructor in your subclass it will call the superclass constructor and the implicit super() will try to look for a no-argument constructor in the superclass but will not find one.
Say this compiled, what would you expect it to print?
class A{
A(int i){
System.out.println("A.i= "+i);
}
}
class B extends A {
public static void main(String... args) {
new B();
}
}
When A is constructed a value for i has to be passed, however the compiler doesn't know what it should be so you have specify it explicitly in a constructor (any constructor, it doesn't have to be a default one)
Of course its an error if written like this it's not JAVA.
If you would have use JAVA syntax it wouldn't be an error.
Class A and B knows nothing about each other if in separate files/packages.
Class A doesn't need a default constructor at all it works fine with only a parameter constructor.
If B extends A you simple use a call to super(int a) in B's constructor and everything is fine.
for constructors not calling a super(empty/or not) extending a super class the compiler will add a call to super().
For further reading look at Using the Keyword super
I would guess that its because when you have an empty parameter list the super variable can't be instantiated. With empty parameter list I mean the implicit super() the compiler could add if the super class had a nonparametric constructor.
For example if you type:
int a;
System.out.print(a);
You will get an error with what I think is the same logic error.
When we have parameter constructor. we explicit bound to consumer by design. he can not create object of that class without parameter. some time we need to force user to provide value. object should be created only by providing parameter(default value).
class Asset
{
private int id;
public Asset(int id)
{
this.id = id;
}
}
class Program
{
static void Main(string[] args)
{
/* Gives Error - User can not create object.
* Design bound
*/
Asset asset1 = new Asset();/* Error */
}
}
Even child class can not create. hence it is behavior of good design.
When extending a class, the default superclass constructor is automatically added.
public class SuperClass {
}
public class SubClass extends SuperClass {
public SubClass(String s, Product... someProducts) {
//super(); <-- Java automatically adds the default super constructor
}
}
If you've overloaded your super class constructor, however, this takes the place of the default and invoking super() will thus cause a compile error as it is no longer available. You must then explicitly add in the overloaded constructor or create a no-parameter constructor. See below for examples:
public class SuperClass {
public SuperClass(String s, int x) {
// some code
}
}
public class SubClass extends SuperClass {
public SubClass(String s, Product... someProducts) {
super("some string", 1);
}
}
OR...
public class SuperClass {
public SuperClass() {
// can be left empty.
}
}
public class SubClass extends SuperClass {
public SubClass(String s, Product... someProducts) {
//super(); <-- Java automatically adds the no-parameter super constructor
}
}
Because if you want to block creation of objects without any data in it, this is one good way.

Categories