answersLogoWhite

Top Answer
User Avatar
Wiki User
Answered 2010-05-15 01:59:13

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.

001
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0
User Avatar

Your Answer

Related Questions


An abstract class(or classes) can't have a constructor and hence you cannot invoke the constructor of the class (for example, you can instantiate an abstract class and hence you cannot call the constructor of an abstract class).


If you do not provide an explicit constructor to the class weather it may be an abstract or normal class , in this situation JVM provides you a default constructor. you can also create an explicit constructor. By using SUPER keyword you might be call constructor of abstract class but it is not possible to call constructor of abstract class using NEW keyword.


No. An abstract class is an incomplete class. It does not have all functionality and features implemented in it. So, you cannot instantiate it. If you try to call the constructor for an abstract class, you will get a compilation error


class base { base():m_data(0){} base(int i):m_data(i){} base(const base& b):m_data(b.m_data){} private: int m_data; }; class derived : public base { derived():base(){} // call base class default constructor derived(int i):base(i){} // call base class overloaded constructor derived(const derived& d):base(d){} // call base class copy constructor };


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.


super keyword is used for two reasons: 1. To Call the super class or base class constructor 2.To call super class methods. If you don't create constructor in a class...Java automatically put for you. Always remember..that the first statement in derived class constructor must be a call to super or this.


The only "feature" of a derived class constructor is that it can, optionally, call any specific base class constructor via the derived class constructor initialisation list, as shown in the following example: class base { public: // or protected constructors base():m_data(0){} base(const base& rhs):m_data(rhs.m_data){} base(const int i):m_data(i){} }; class derived { public: derived():base(){} // calls the base class default constructor derived(const derived& rhs):base(rhs){} // calls the base class copy constructor derived(const int i):base(i){} // calls the base class overloaded constructor }; Note that the initialisation list of a constructor is the most efficient method of initialising member variables. If you choose to initialise members from the body of the constructor, you are effectively initialising the members twice, once via the implicit initialisation list, and then again via explicit initialisation in the body of the constructor. By using explicit initialisation, you only initialise once, at the point of instantiation. By explicitly invoking base class constructors via the initialisation list, you achieve the same efficiency with base class construction. If you do not specify a base class constructor, the default constructor is executed implicitly, which may not be the most efficient method of initialising the base class, especially if you then have to call other base class methods in order to initialise it properly, before initialising your derived class. There will be times when the initialisation list is not sufficient to initialise your classes properly, but you should always try to do as much initialisation as you possibly can via the initialisation list, to ensure the most efficient construction possible.


Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie, you may not call its constructor), abstract class may contain static data. Any class with an abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.


No, a virtual constructor would not only be meaningless, it would be ambiguous. The implication is that calling a base class constructor would somehow call a derived class constructor -- but how is the compiler to determine which derived class it is meant to construct? Derived objects can only be constructed by calling the derived object's constructor directly. In so doing, all base class constructors will be called in sequence, from the least-derived class upwards. Only when all the base classes are fully instantiated can the derived object itself be instantiated. If you need to call specific base class constructors in order to pass parameters to them, you can do so via the derived object's initialisation list(s).


The constructor for a class needs to call the constructor for the super class because otherwise the members of a super class would never get instantiated/initialized.


No. not directly. The static constructor is called by .net framework, not by your code. I always treat static constructor is the class initializer (not the instance one). The initialization routine is only called when the class definition being loaded. When the derived class is loaded, since this class derived from the base, it makes to go up the chain (of the hierarchy) to call those static constuctors. So, when a derived class is loaded, the static constructors of the hierarchy is called up. (any one of them had been loaded, the calling sequence stops). You cannot call those static constructor directly, nor you can skip "not to execute" them.


Yes, a constructor name should always be the same as the class name. You do not directly call the constructor when you instantiate an object, so how the machine knows which method call? Easy, our constructor is always a method with the same name as the class and with no return value.


You cannot actually call any constructor, you can only invoke construction, either by instantiating a static instance of a class or by dynamically creating one with the new operator. In the case of base class construction via a derived class, the base class constructor is invoked by the derived class' initialisation list. Every class constructor has an initialisation list whether you define one or not (the compiler will generate one automatically if you don't). When you derive one class from another, the derived class initialisation list invokes a call to the base class default constructor. The only exception is the compiler-generated copy constructor which automatically calls the base class copy constructor. If you define your own initialisation list, then you can explicitly invoke any base class constructor overload, thus making your construction code all the more efficient. However, copy constructors should always invoke the base class copy constructor, so if you define a copy constructor, you must explicitly invoke the base class copy constructor -- the compiler will not invoke it implicitly from a user-defined copy constructor. While many programmer's use the constructor's body to initialise a class, this is highly inefficient. Even if you don't specify an initialisation list, one is created for you, resulting in every base class and every member variable being initialised twice, which can quickly add up to a substantial cost in performance. The constructor's body should only really be used for initialisation when it would be difficult or impossible to do so from the initialisation list. Remember that your object doesn't physically exist until initialisation is complete, so you may not have access to some members, particularly base class members, at certain points in the initialisation process. Initialisation must be done from the ground up, starting with the base classes and ending with the actual class members, and all in the order they were declared. Note that only direct base classes (or virtual base classes) should be invoked from the initialisation list. The base classes themselves should invoke their own base class constructors, if they have any. Thus no matter which derivative you construct, the least-derived class is always constructed first.


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.


The default constructor is an empty (only call the super constructor) with no parameters constructor inserted by the java compiler when you don't define a constructor in your class. If you write something like this: public class NoConstructorClass{ //no constructor goes here } Then you get something like this: public class NoConstructorClass{ public NoConstructorClass(){ // Default constructor that you didn't write super(); } }


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).


Constructors exist purely to initialise member variables. Although you can use the constructor body to call member methods in order to perform that initialisation, this is highly inefficient, particularly when the member variable is a complex type (non-primitive). Constructors have initialisation lists specifically for the purpose of initialising member variables at the point of instantiation, which is far more efficient than initialising via the constructor body, which would result in complex types being initialised twice (once during implicit initialisation and then again during explicit initialisation in the constructor body). However, if you specify two or more constructors, each has to have its own specific initialisation list, even if those lists are largely the same for all constructors, which not only results in code duplication but also increases code maintenance. When you add a new member, you must update the initialisation lists of all constructors. Thus the advantage of constructors is increased efficiency, while the disadvantage is increased maintenance. A lot of young programmers often counter this by creating a common initialisation member function which is then called from each constructor's body. While this reduces code maintenance, it increases the inefficiency. There's nothing wrong with having a common initialisation function, but you shouldn't call it from the constructor. Initialisation lists also allow derived classes to invoke specific base class constructors. When not specified, a derived class constructor will automatically call the base class' default constructor, which may not be the most efficient method of construction. This is particularly important when defining a copy constructor. In this case you will want to call the base class copy constructor, not the base class default constructor, in order to maintain efficiency. It should be noted that when you do not declare any constructors, the compiler will generate both a default constructor and a copy constructor, both of which will call the appropriate base class constructors if the class is derived. If you define any constructor, you will automatically lose the compiler-generated default constructor and must define your own if you require one. The copy constructor is always generated for you unless you explicitly declare your own, but you must then call the base class copy constructor explicitly, otherwise the base class default constructor is called implicitly.


Constructor will be automatically invoked when an object is created whereas method has to be called explicitly. Constructor needs to have the same name as that of the class whereas functions need not be the same. * There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value. * There is no return statement in the body of the constructor. * The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.


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.


yes we can call constructor


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


You always should define default constructor for your class. You must also define a copy constructor for your class if there are any pointers in the class. While it is not mandatory, failure to provide a default constructor can result in bad behavior, and failure to provide a copy constructor when you have pointers in the class will result in bad behavior. For example, without a default constructor, the compiler will not fully initialize the attributes of the class. It will initialize the virtual function table, and call base class constructors, but that is all - the attributes could be random garbage. For another example, without a copy constructor, the compiler will generate one that simply makes a bit wise copy of the attributes. If these attributes contain pointers, then you have two pointers to the same object, not necessarily a good thing, especially if one of them get "deleted".


These are two different things. "Super" is used in inheritance - if class B inherits from class A, and we want the constructor of B to do the same things the constructor of A does (and maybe more things), we can call A's constructor in B's constructor: public B() { super(); ...... } Notice that it must be the first line in B's constructor. "This" is used in case of overloading, to call a function aith the same name. For example, if we have a constructor that receives one argument, and we it want to have a default value of 0, we can write: public C() { this(0); }


An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


There is no such thing as a virtual constructor. Constructors are local to the class in which they are declared, and therefore cannot be inherited. Derived classes will call the default constructor of the least-derived base class first, unless a specific base class construction overload is called via the derived class's initialisation list. All base class constructors required by derived classes must be declared protected or public (private constructors are inaccessible), but no constructor can be declared virtual. Destructors only need to be declared virtual when there are one or more virtual or pure-virtual methods declared in the class. Derived classes do not need to declare virtual destructors since it is implied by their base class. The only reason for having virtual destructors is to ensure the most-derived class destructor is called first whenever its base class falls from scope. Destruction then cascades up the hierarchy to the base class, which must be destroyed last.



Copyright ยฉ 2021 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.