answersLogoWhite

0


Best Answer

A subclass invokes its base class constructor at the point of instantiation. That is; you cannot instantiate a subclass object without first constructing its base class, which is done automatically.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When a subclass can call a parent's class constructor?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can abstract class have constructors?

A constructor of a class in invoked when a object of that class is created. As an abstract class can't have an object, so we can't create a constructor of the abstract class. But we can create a constructor of a concrete subclass of that abstract class and we have to pass the object of that concrete subclass to the abstract class.


How do you call an abstract class from main method?

An abstract class cannot have a constructor and hence you cannot invoke the constructor of the class - i.e., you can instantiate an abstract class and hence you cannot call the constructor of an abstract class.


How constructor called?

You cannot invoke a constructor explicitly. It will get invoked implicitly when you call the new keyword on the class to create an object of the class. Ex: private ClassExample obj = new ClassExample(); here this new keyword usage on the ClassExample class will invoke the constructor of this class and create an object of that class.


Can you call a constructor from another if a class has multiple constructors?

Yes. All you need to do is to specify the correct number of arguments to invoke the correct constructor.


Need of constructor in c plus plus?

There is no specific keyword for a constructor in C++. Simply define and declare a method of the class with the same name as the class and it will be a constructor. A constructor with no arguments is the default constructor, a constructor with one argument of class type is the copy constructor, and a constructor with one argument of some other type is the conversion constructor. You can provide other overloaded constructors if you want.

Related questions

A subclass can call constructor method defined by its super class?

True


Can abstract class have constructors?

A constructor of a class in invoked when a object of that class is created. As an abstract class can't have an object, so we can't create a constructor of the abstract class. But we can create a constructor of a concrete subclass of that abstract class and we have to pass the object of that concrete subclass to the abstract class.


How do explicitly invoke superclass constructor from a subclass?

Using the super keyword. If you call super() from within your constructor, it will explicitly invoke the superclass version of the constructor.


How do you call an abstract class from main method?

An abstract class cannot have a constructor and hence you cannot invoke the constructor of the class - i.e., you can instantiate an abstract class and hence you cannot call the constructor of an abstract class.


Can you implement a constructor in private part of code?

no we cannot initialize a constructor in private in order to call a constructor from outside of a class it must be a public member.in order to create an object we should call the constructor .so only private members can implement outside of the class.


How does constructor work?

First line in any constructor has to be either super() or this() not both. If any constructor does not contain either of super() and this(), compiler adds super(). When any constructor is called before excuting the code of the constructor, if it founds this(), it will call another constructor else it will call super() which is the call for the constructor of super class, now again from the super class constructor it will call the super class constructor if available. This is continued until it reaches the top of the class hierarchy. ---- Basically, a constructor is a block of code that gets executed each time a particular instance of a class is created. So, say you've designed a class for working with a database of some sort. When you create an instance of that class, copies of all the variables and functions of that class get attached to the instance-object, and if one of the functions is a constructor function, it will be run as soon as the instance-object is created. This lets you automatically set up conditions for the instance (i.e. establishing connections to different databases or reading data from different tables, or etc.). Depending on the language you're using, classes may or may not automatically call the constructor function of a parent or super class (if such exists, and if you do not provide a constructor for the class in question).


How constructor called?

You cannot invoke a constructor explicitly. It will get invoked implicitly when you call the new keyword on the class to create an object of the class. Ex: private ClassExample obj = new ClassExample(); here this new keyword usage on the ClassExample class will invoke the constructor of this class and create an object of that class.


What you call those subclass having more than one super class?

category


What you call those subclass having more than one super-class?

category


Can you call constructor?

yes we can call constructor


Can you call a constructor from another if a class has multiple constructors?

Yes. All you need to do is to specify the correct number of arguments to invoke the correct constructor.


Need of constructor in c plus plus?

There is no specific keyword for a constructor in C++. Simply define and declare a method of the class with the same name as the class and it will be a constructor. A constructor with no arguments is the default constructor, a constructor with one argument of class type is the copy constructor, and a constructor with one argument of some other type is the conversion constructor. You can provide other overloaded constructors if you want.