Is Constructor Overriding Possible?

后端 未结 14 1875
时光说笑
时光说笑 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:10

    Because a constructor cannot be inherited in Java and Method Overriding requires inheritance. Therefore, it's not applicable.

    0 讨论(0)
  • 2020-12-08 01:14

    No it is not possible to override a constructor. If we try to do this then compiler error will come. And it is never possible in Java. Lets see the example. It will ask please write a return type o the method. means it will treat that overriden constructor as a method not as a constructor.

    package com.sample.test;
    
    class Animal{
    
        public static void showMessage()
        {
            System.out.println("we are in Animal class");
        }
    }
    
    class Dog extends Animal{
    
        public void DogShow() 
        {
            System.out.println("we are in Dog show class");
        } 
    
        public static void showMessage()
        {
            System.out.println("we are in overriddn method of dog class");
        }
    }
    
    public class AnimalTest { 
    
        public static void main(String [] args)
        {
            Animal animal = new Animal();
            animal.showMessage();
    
            Dog dog = new Dog();
            dog.DogShow();
    
            Animal animal2 = new Dog();
            animal2.showMessage();
        }
    
    }
    
    0 讨论(0)
  • 2020-12-08 01:17

    It is never possible. Constructor Overriding is never possible in Java.

    This is because,

    Constructor looks like a method but name should be as class name and no return value.

    Overriding means what we have declared in Super class, that exactly we have to declare in Sub class it is called Overriding. Super class name and Sub class names are different.

    If you trying to write Super class Constructor in Sub class, then Sub class will treat that as a method not constructor because name should not match with Sub class name. And it will give an compilation error that methods does not have return value. So we should declare as void, then only it will compile.


    Have a look at the following code :

    Class One
            {
             ....
             One() { // Super Class constructor
              .... 
            }
    
            One(int a) { // Super Class Constructor Overloading
              .... 
            }
     }
    
    Class Two extends One
                       {
                        One() {    // this is a method not constructor 
                        .....      // because name should not match with Class name
                       }
    
                        Two() { // sub class constructor
                       ....  
                       }
    
                       Two(int b) { // sub class constructor overloading
                       ....
                      }
     }  
    
    0 讨论(0)
  • 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;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-08 01:19

    Cannot override constructor. Constructor can be regarded as static, subclass cannot override its super constructor.

    Of course, you could call protected-method in super class constructor, then overide it in subclass to change super class constructor. However, many persons suggest not to use the trick, in order to protect super class constructor behavior. For instance, FindBugs will warn you that a constructor calls a non-final method.

    0 讨论(0)
  • 2020-12-08 01:20

    method overriding in java is used to improve the recent code performance written previously .

    some code like shows that here we are creating reference of base class and creating phyisical instance of the derived class. in constructors overloading is possible.

    InputStream fis=new FileInputStream("a.txt");
    int size=fis.available();
    

    size will return the total number of bytes possible in a.txt so

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