简体   繁体   中英

use abstract superclass' constructor by default?

Is something like this possible

abstract class AbstractSuperClass {
  private Entity entity;

  public AbstractSuperClass(Entity entity) {
    this.entity = entity;
  }

  public abstract void operate();
}

public class SubClass extends AbstractSuperClass {
  public void operate() {
    this.entity.doVoidMethod(); // is this.entity defined in instances of SubClass ?
  }
}

// ... somewhere else

Entity instantiatedEntity = new Entity();
SubClass instance = new SubClass(instantiatedEntity);
instance.operate(); // does this call this.entity.doVoidMethod() inside of instance?

I want to be able to skip writing my own constructors in subclasses of an abstract class I'm writing. All of the constructors would be identical in every subclass.

If I skip writing a constructor for a subclass of an abstract class (is this even allowed?) does the abstract class' constructor get used by default?

First of all, if you don't define constructors, a default one with no argument is considered.
In other words, if you want to use new SubClass(instantiatedEntity); you must define the constructor within the SubClass that takes a single argument of type Entity .
In other words, you cannot skip writing your own constructors in case you want to pass a parameter.

Secondly, within your SubClass , you cannot call this.entity.doVoidMethod(); . This because the entity attribute is private within the base class and therefore not accessible by the SubClass.

Furthermore, within AbstractSuperClass you defined public abstract Operate(); . You probably meant public abstract void operate(); . There's a similar mistake in the SubClass.

I want to be able to skip writing my own constructors in subclasses of an abstract class I'm writing. All of the constructors would be identical in every subclass.

You can't. Constructors are not inherited in Java:

Java Constructor Inheritance

The only quasi-exception is the default constructor . However it is strictly speaking not inheritance. If there are no constructors, the default one is added and it will call super() .

If I skip writing a constructor for a subclass of an abstract class (is this even allowed?) does the abstract class' constructor get used by default?

No. Constructors of superclass are neither inherited nor copied.

According to your code above, you are using subclass is extending from an abstract class.

abstract class A{
 A(){
  //todo: some work
 }
 abstract methodA();
 methodB(){
  //todo:something: implementation within the abstract class.
 }
}
class B extends A{
 B(){
 super(this);
  }

}

I believe to use abstract constructor you would use the super keyword.

Some thoughts (1) you should make entity "protected" instead of "private" - that's how you make it available in the subclass, (2) it helps to use accessor methods (getter/setter) which can be inherited by subclass unlike non-defaul constructors, (3) the operate method needs a return type (I used void below), (4) the operate method needs the same signature (type sensitive name and argument types) in order to override (5) it is good practice to annotate overridden methods with @Override.

public abstract class AbstractSuperClass {
  protected Entity entity;

  public AbstractSuperClass() {
  }

  public AbstractSuperClass(Entity entity) {
    this.entity = entity;
  }

  public setEntity(Entity entity) {
    this.entity = entity;
  }

  public abstract void operate();
}

public class SubClass extends AbstractSuperClass {
  @Override
  public void operate() {
    this.entity.doVoidMethod(); // protected entity is available in subclass
  }
}

// ... somewhere else
Entity instantiatedEntity = new Entity();
SubClass instance = new SubClass(); // default constructor
instance.setEntity(instantiatedEntity); // inherited method
instance.operate(); // yes calls entity.doVoidMethod()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM