Can constructor and destructor can be inherited? | SoloLearn: Learn to code for FREE!

+7

Can constructor and destructor can be inherited?

c++

9/7/2020 12:43:23 PM

It's Me

49 Answers

New Answer

+14

In C++ since C++11 a form of constructor inheritence is allowed. See the code. https://code.sololearn.com/c2gy9f3yN53E/?ref=app Destructors are never inherited. In D_Stark code void Parent(String name){ } is not a constructor, but a simple method you can verify public class Test { void Test() { System.out.println("Constructor"); } public static void main(String[] args) { new Test(); // no output will be printed } }

+11

David Carroll sir you can see this.. https://en.m.wikipedia.org/wiki/Civil_Services_Examination_(India) Edit : It's an entrance examination [The Civil Services Examination (CSE)] is a nationwide competitive examination in India conducted by the Union Public Service Commission for recruitment to various Civil Services of the Government of India, including the Indian Administrative Service (IAS), Indian Foreign Service (IFS), and Indian Police Service (IPS) Gratitude Aptitude of Technology (GATE) is an entrance exam for master's in any field in India(for any bachelor program). //No they are not necessary..

+9

D_Stark Yup only C++ as i know of. Java doesn't support it. I haven't checked some of the relatively newer languages like Go, Rust, Dart and few more and i think it's not there in those languages. It appears a group of downvoters has came alive (sic) on this thread 😉 after David Carroll comment.

+8

In situations where both class have a constructor, then the priority is given to the one from the parent class which is called first while in case of destructor it’s the child class which gets the priority. Generally, we add constructor or destructor class in either the child or the parent class only to avoid conflict. Edit : Whether it may upsc or a gate examination it depends on the framework of questions and the nearest option available to be marked as crrt.

+7

David Carroll, D_stark that might be a bit true..but you all have huge knowledge and experience.. you can crack this exam easily 😄

+7

D_Stark I'm only aware of constructor inheritance in C++. Adding to the list by ~ swim ~, C# does not support it either. Ipang Good point about a grace period. @~ swim ~: I love how the downvote abusers think they are protected by anonymity. 😉 I'll leave it at that. 😉

+6

Constructors and destuctors are not members of the class and not inherited but instead automatically invoked if the sub class has no constructor. up to now this excludes c++ which supports constructor inheritance.

+6

It's Me As of now only option 1 is right i.e destructors cannot be inherited. But i doubt that UPSC might be considering option 3 as right answer especially considering students are still taught TurboC/C++ ! It is difficult to say when the question was created. Before C++11 option 3 was right.

+6

David Carroll In other forums thread locking is possible, and I really understand the purpose. But oftenly enough I found (in other forums) that a best answer also has a sort of grace period. I mean an answer that was related, correct, detailed and valid at a time may not be true in the future, as we can see from ~ swim ~'s answer, a new feature in a language changes the definition of what best answer is. Time changes things, apparently : )

+6

🤦‍♂️ Nandi Vardhan Read the past few messages posted. You might find it informative. --- Anyway... I'm unfollowing this post due to the never-ending duplicate wrong answers that will follow. Mention me by name if you want me to see your followup posts.

+6

GUNWAL THE GAMERS , Subani1 please don't post irrelevant answers.. please delete this..so that I can delete this.

+5

The inheritance of constructors and destructors depends on the type of invocation being implemented.

+5

It's Me option 1 : Destructor cannot be inherited. Reason : destructor can't be inherited after the scope of object goes out. //This is what I know..I may be wrong.

+5

David Carroll Here is the information you are looking for https://blog.superprofs.com/upsc/upsc-exam-details/amp/

+5

Aditya / ~ swim ~ Thanks for the clarification. I guess I would no longer be eligible even if I was a citizen of India because I've already "attained an age" that exceeds the maximum eligibility. In closing this tangent, this is the first time I've ever seen the expression "attain the age". 😉👌

+5

David Carroll I too wish for thread locking feature. Till then everyone should add Solved/Resolved to their question once they get satisfactory answer. This shall help to reduce clutter people add without reading previous answers, if not completely stop it.

+5

David Carroll does constructor inheritance apply only to c++ because java doesn't seem to support this.

+4

Option 3. I choose. Both constructor cannot be inherited., automatically invoked. And contrcmuctor can be called explicitly from subclass...

+4

Constructor cannot have return type then how it is called a constructor...? D_Stark

+4

D_Stark Because constructors have no return types. You are creating an object of sub class and since there is no constructor the name member is initialized to null. Try the simple code i posted in my comment and let me know what make you think it's a constructor.