Is Constructor Overriding Possible?

后端 未结 14 1874
时光说笑
时光说笑 2020-12-08 00:31

What i know is, the compiler writes a default no argument constructor in the byte code. But if we write it ourselves, that constructor is called automatically. Is this pheno

14条回答
  •  有刺的猬
    2020-12-08 01:18

    I found this as a good example for this question:

        class Publication {
    
        private String title;
    
        public Publication(String title) {
            this.title = title;
        }
    
        public String getDetails() {
            return "title=\"" + title + "\"";
        }
    
    }
    
    class Newspaper extends Publication {
    
        private String source;
    
        public Newspaper(String title, String source) {
            super(title);
            this.source = source;
        }
    
        @Override
        public String getDetails() {
            return super.getDetails() + ", source=\"" + source + "\"";
        }
    }
    
    class Article extends Publication {
    
        private String author;
    
        public Article(String title, String author) {
            super(title);
            this.author = author;
        }
    
        @Override
        public String getDetails() {
            return super.getDetails() + ", author=\"" + author + "\"";
        }
    
    }
    
    class Announcement extends Publication {
    
        private int daysToExpire;
    
        public Announcement(String title, int daysToExpire) {
            super(title);
            this.daysToExpire = daysToExpire;
        }
    
        @Override
        public String getDetails() {
            return super.getDetails() + ", daysToExpire=" + daysToExpire;
        }
    
    }
    

提交回复
热议问题