Can an abstract class have a constructor?

前端 未结 22 2728
甜味超标
甜味超标 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:04

    package Test1;

    public class AbstractClassConstructor {

    public AbstractClassConstructor() {
    
    }
    
        public static void main(String args[]) {
           Demo obj = new Test("Test of code has started");
           obj.test1();
        }
    

    }

    abstract class Demo{
        protected final String demoValue;
    
        public Demo(String testName){
            this.demoValue = testName;
        }
    
        public abstract boolean test1();
    }
    
    class Test extends Demo{
    
        public Test(String name){
            super(name);
        }
    
        @Override
        public boolean test1() {
           System.out.println( this.demoValue + " Demo test started");
           return true;
        }
    
    }
    

提交回复
热议问题