Polymorphism | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Polymorphism

if we declare one constructor in parent class and another constructor in child class eg. class A // parent class { public A() { Console.Writeline("I m in A"); } } class B : A // child class { public B() { Console.Writeline("I m in B "); } static void main(string[] args) { A a=new B(); } } output // I m in A I m in B In the above program why it is called both constructor ? explain

21st Mar 2017, 10:20 AM
Punith Kumar C T
3 Answers
+ 5
When a sub/child class is instantiate, it will first call the superclass (which is also base class/parent class) constructor, then follow by the sub class' constructor. This can easily remembered and logic as you need the "base"(base class) before you can do anything more (sub class)
21st Mar 2017, 10:54 AM
Heng Jun Xi
Heng Jun Xi - avatar
+ 5
Btw, this is true for Java, not sure for other languages...
21st Mar 2017, 10:55 AM
Heng Jun Xi
Heng Jun Xi - avatar
0
thank you
21st Mar 2017, 11:29 AM
Punith Kumar C T