Can an abstract class have a constructor?

前端 未结 22 2176
甜味超标
甜味超标 2020-11-22 05:25

Can an abstract class have a constructor?

If so, how can it be used and for what purposes?

相关标签:
22条回答
  • 2020-11-22 06:16

    You would define a constructor in an abstract class if you are in one of these situations:

    • you want to perform some initialization (to fields of the abstract class) before the instantiation of a subclass actually takes place
    • you have defined final fields in the abstract class but you did not initialize them in the declaration itself; in this case, you MUST have a constructor to initialize these fields

    Note that:

    • you may define more than one constructor (with different arguments)
    • you can (should?) define all your constructors protected (making them public is pointless anyway)
    • your subclass constructor(s) can call one constructor of the abstract class; it may even have to call it (if there is no no-arg constructor in the abstract class)

    In any case, don't forget that if you don't define a constructor, then the compiler will automatically generate one for you (this one is public, has no argument, and does nothing).

    0 讨论(0)
  • 2020-11-22 06:16

    In order to achieve constructor chaining, the abstract class will have a constructor. The compiler keeps Super() statement inside the subclass constructor, which will call the superclass constructor. If there were no constructors for abstract classes then java rules are violated and we can't achieve constructor chaining.

    0 讨论(0)
  • 2020-11-22 06:16

    Yes..It is like any other class. It can have a constructor and it is called after creating object for the base class.

    0 讨论(0)
  • 2020-11-22 06:19

    As described by javafuns here, this is an example:

    public abstract class TestEngine
    {
       private String engineId;
       private String engineName;
    
       public TestEngine(String engineId , String engineName)
       {
         this.engineId = engineId;
         this.engineName = engineName;
       }
       //public gettors and settors
       public abstract void scheduleTest();
    }
    
    
    public class JavaTestEngine extends TestEngine
    {
    
       private String typeName;
    
       public JavaTestEngine(String engineId , String engineName , String typeName)
       {
          super(engineId , engineName);
          this.typeName = typeName;
       }
    
       public void scheduleTest()
       {
         //do Stuff
       }
    }
    
    0 讨论(0)
  • 2020-11-22 06:20

    Yes surely you can add one, as already mentioned for initialization of Abstract class variables. BUT if you dont explicitly declare one, it anyways has an implicit constructor for "Constructor Chaining" to work.

    0 讨论(0)
  • 2020-11-22 06:21

    Yes, an abstract class can have a constructor. Consider this:

    abstract class Product { 
        int multiplyBy;
        public Product( int multiplyBy ) {
            this.multiplyBy = multiplyBy;
        }
    
        public int mutiply(int val) {
           return multiplyBy * val;
        }
    }
    
    class TimesTwo extends Product {
        public TimesTwo() {
            super(2);
        }
    }
    
    class TimesWhat extends Product {
        public TimesWhat(int what) {
            super(what);
        }
    }
    

    The superclass Product is abstract and has a constructor. The concrete class TimesTwo has a constructor that just hardcodes the value 2. The concrete class TimesWhat has a constructor that allows the caller to specify the value.

    Abstract constructors will frequently be used to enforce class constraints or invariants such as the minimum fields required to setup the class.

    NOTE: As there is no default (or no-arg) constructor in the parent abstract class, the constructor used in subclass must explicitly call the parent constructor.

    0 讨论(0)
提交回复
热议问题