Is Constructor Overriding Possible?

后端 未结 14 1946
时光说笑
时光说笑 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: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();
        }
    
    }
    

提交回复
热议问题