Constructor Overriding

Is Constructor Overriding Possible?

What you describe isn't overriding. If you don't specify a default constructor, the
compiler will create a default constructor. If it's a subclass, it will call
the default parent constructor(super()), it will also initialize all instance variables to
a default value determined by the type's default value(0 for numeric types, false for booleans, or null
for objects).

Overriding happens when a subclass has the same name, number/type of parameters, and
the same return type as an instance method of the superclass. In this case, the subclass
will override the superclass's method. Information on overriding here.

Java: Calling superclass' constructor which calls overridden method which sets a field of subclass


class Test2 {
public int number = 0;
}

is equivalent to

class Test2 {
public int number;

public Test2() {
super();
number = 0;
}
}

So through invoking the super constructor the field number is set to 1. After the return from the super constructor, your assignment of number to 0 is executed.

Just remove the assignment and it should behave as you expect.

How to override a base class constructor in Javascript

you should not be using

    this.wheels = 2
this.horn = "honk honk"

when already overriding these in super constructor.





class Vehicle {

constructor(color = 'blue', wheels = 4, horn = 'beep beep') {

this.color = color;

this.wheels = wheels;

this.horn = horn;

}


honkHorn() {

console.log(this.horn);

}

}


class Bicycle extends Vehicle {

constructor(wheels = 2, horn = 'honk honk') {

super(undefined, wheels, horn);

}


honkHorn() {

super.honkHorn()

}


}


let by = new Bicycle();

by.honkHorn();

How do you override the parent constructor in Java?


How can I override this statement to keep that from happening?

You can't, unless you can edit Person to provide a constructor that doesn't do the System.out.println call. A subclass must call one of its superclass constructors.¹ Your Person class has only the one, which includes the System.out.println call, so Employee has no choice but to call it.

This is one of several reasons constructors shouldn't have side-effects. Neither Person nor Employee should be calling System.out.println. That should be left to code using the class, or a method (rather than constructor).


¹ If you don't do it explicitly, the compiler will insert super() at the beginning of your constructor. In your case, that would be a compilation error, because Person doesn't have a zero-parameters constructor.

Overriding Constructors

Constructors aren't polymorphic - you don't override them at all. You create new constructors in the subclass, and each subclass constructor must chain (possibly indirectly) to a superclass constructor. If you don't explicitly chain to a constructor, an implicit call to the parameterless superclass constructor is inserted at the start of the subclass constructor body.

Now in terms of overriding methods - an object is of its "final type" right from the start, including when executing a superclass constructor. So if you print getClass() in the Super constructor code, you'll still see Sub in the output. The upshot of that is the overridden method (i.e. Sub.test) is called, even though the Sub constructor hasn't yet executed.

This is fundamentally a bad idea, and you should almost always avoid calling potentially-overridden methods in constructors - or document very clearly that it's going to be the case (so that the subclass code is aware that it can't rely on variables having been initialized appropriately etc).

Can I add more than one override method in a constructor in Java?

I assume your class createData (class names in Java should be capitalised by the way) extends some other class right?

To make it clear, you're not adding overrides in the constructor, you're adding overrides in the class and calling them from the constructor.

So yes, you can do that, you can even do:

public class GenericCreateData
{
public GenericCreateData(DataAdd dataAdd)
{
...
build();
buildAdd();
}

public void build()
{
System.out.println("genericBuild");
}

public void buildAdd()
{
System.out.println("genericBuildAdd");
}
}
public class SpecificCreateData extends GenericCreateData
{
public SpecificCreateData(DataAdd dataAdd)
{
super(dataAdd);
}

@Override
public void build()
{
System.out.println("specificBuild");
}

@Override
public void buildAdd()
{
System.out.println("specificBuildAdd");
}
}

calling new GenericCreateData(dataAdd) will output:

genericBuild
genericBuildAdd

calling new SpecificCreateData(dataAdd) will output:

specificBuild
specificBuildData

Since methods are called on the specific instance type



Related Topics



Leave a reply



Submit