answersLogoWhite
notificationBell

Top Answer
User Avatar
Wiki User
Answered 2009-04-03 08:10:52

Nothing Happens. Actually such a constructor is called a Default Constructor. Even if we do not write a constructor for a class, Java would automatically place a default constructor inside the class.

Ex:

Public class Test {

public String getName(){

return "Hi";

}

}

Public class TestEx {

public static void main(String[] args){

Test obj = new Test();

System.out.println(obj.getName());

}

}

Here we were able to instantiate an object of class Test even though we did not define a constructor for that class. This is because Java automatically places a default constructor for the class.

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

Your Answer

Related Questions


When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").


A constructor that has arguments in it.Ex:class Car {Car() { }Car(String s) { }}The line in bold is the argument constructor and the other is the default (no-arg) constructor


Nothing happens. The compiler successfully compiles the class. When a class does not have a specific constructor, the compiler places a default no argument construtor in the class and allows you to compile and execute the class. public class Test { } and public class Test { public Test(){ } } are one and the same.


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.


The first thing to note about constructor overloading is that Java creates a no argument constructor for you if and only if you have not typed a constructor yourself. Every class has a constructor even abstract ones (default no argument constructor). Abstract constructors are always executed. To overload a constructor you can do the following: class Test { String name; Test(String n) { name = n; System.out.println("Constructing Test Object named: " + name); } } In the case above we are overloading the default no argument constructor with a constructor that takes a String parameter. You can write you own no argument constructor as follows: class Test { Test() { System.out.println("Constructing Test Object"); } } To override our own no argument constructor we do this: class Test { Test() { // our no argument constructor System.out.println("Constructing Test Object"); } String name; Test(String n) { // overloading our no argument constructor with this // constructor that take a String parameter name = n; System.out.println("Constructing Test Object named: " + name); } }


Casting is the conversion of one type to another type, better known as a type cast. A class constructor that accepts one argument is a conversion constructor because it converts its argument to an object of the class. However, if the argument is of the same type as the class then it is known as a copy constructor.


Yes. Depending on the signature (argument list) there can be several different constructors. The default constructor takes no arguments, the copy constructor takes one argument of class type, and the conversion constructor takes one argument of non-class type. Other constructors take more than one argument. Any constructor can have default parameters, making it possible for different types of constructors to share the same code body.


An Argument Constructor is one that takes a value as argument and uses it while initializing a class. Ex: class Car { int size; String name; Car(String name, int size) { this.name = name; this.size = size; } }


Whenever a new object is instantiated, the appropriate class constructor for that object is invoked automatically. At compile time, argument-dependent lookup is used to determined which specific constructor to invoke. When passing objects to functions by value, the class copy constructor is invoked. If there is no copy constructor for the class, objects of this class cannot be passed by value. When returning objects from functions by value, the class move constructor is invoked. If there is no move constructor for the class, the class copy constructor is invoked instead. If neither exists, objects of this class cannot be returned by value.


Multiple constructors are overloaded versions of the class constructor. They are chosen based on their argument types. The default constructor takes no arguments. The copy constructor takes one argument of reference type to a const variable of the class. The conversion constructor takes one argument of different type than the copy constructor. Other constructors take two or more arguments and can be used for special initialization. Often, these other constructors can be built as one, using default values for arguments so, for instance, one of them could function as the default constructor if needed.


There is a default constriuctor that takes no argument for every class that extends Object.


If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor)Determine Whether a Default Constructor Will Be CreatedThe following example shows a Lamborghini class with two constructors:class Lamborghini {Lamborghini() { }Lamborghini(String name) { }}Will the compiler put in a default constructor for the class above? "No!" What about for the following variation of the class?class Lamborghini {Lamborghini(String name) { }}Now will the compiler insert a default constructor? "No Again!" What about this class?class Lamborghini { }Now we're talking. The compiler will generate a default constructor for the preceding class, because the class doesn't have any constructors defined. OK, what about this one below?class Lamborghini {void Lamborghini() { }}You might be tempted to say that, the compiler won't create one, since there already is a constructor in the Lamborghini class? Take another look at the Lamborghini class.What's wrong with the Lamborghini() constructor? It isn't a constructor at all! It's simply a method that happens to have the same name as the class. Remember, the return type is a dead straight way to tell us that we're looking at a method, and not a constructor. So, here again the compiler will put the default no-arg constructor in the class.


If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.


A constructor is a class method which initialises an object of the class at the point of instantiation. Specifically, it initialises the base classes (if any) and the non-static data members (if any). Constructors also play a central role in the resource acquisition is initialisation (RAII) paradigm. Objects which have a natural default value have a default constructor. The default constructor is a constructor that has no arguments or where all arguments have default values. Objects which can be copied have a copy constructor. The copy constructor has just one non-default argument, a const l-value reference of the same type as the class. Objects which can be moved have a move constructor. The move constructor has just one non-default argument, a modifiable r-value reference of the same type as the class. All other constructors that have only one argument of a type other than the class itself are known as conversion constructors. Constructors can also have more than one argument. No specific name is given to these constructors. Other than physical memory constraints, there is no limit to the number of constructors that may be defined for a class.


A copy constructor is a type of constructor which constructs the object of the class from another object of the same class. The copy constructor accepts a reference to its own class as a parameter. A valid copy constructor can be specified as, class A { ...... ...... public: A(&A); }


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.


A constructor is not a function. A function is a type, as specified by its return type, and must return a value of that type unless the type is void. A constructor does not return anything, not even void. The purpose of a constructor is to both allocate and initialise memory for an object of the type being constructed. If a valid object cannot be constructed for any reason, the constructor must throw an exception. If the object's class has no data members (attributes), the class does not require a constructor. This is typically the case for most abstract data types and base classes which are used purely as interfaces. Constructors differ from functions in that all constructors have an initialisation section that is used specifically to initialise non-static data members. The body of the constructor is rarely used except to perform initialisations that cannot be more easily performed by the initialisation section. A class may have more than one constructor to provide alternative methods of construction based upon the number and type of arguments supplied (if any). When no arguments are required or all arguments have default values then the constructor is known as the default constructor. If the constructor has only one argument the constructor is known as a conversion constructor (because the argument is converted to an object of the class). However, if the constructor argument is a constant reference to an object of the same class, then it is known as a copy constructor, and when the constructor argument is an rvalue reference, it is known as a move constructor. If copy and/or move constructors are provided for a class, the equivalent assignment operators should also be provided for that class. All other constructors are known as user-defined constructors.


A parameter constructor is a constructor within a class with some arguments in it. class abc { abc()---------------------------------Default Constructor { } abc(int d)---------------------------Parameter Constructor { } }


The compiler places a default no-arg constructor in any java class that does not have an explicit constructor coded into it. for ex: public class Car { ... ... //lots of code but no constructor } In the above case, the compiler will place the below constructor into the code: public Car() { super(); } But, if you have a constructor in your class that takes arguments then the compiler will not put the default constructor. Ex: public class Car { public Car(String name){ ... } ... //lots of code } Above, we have a Car constructor that takes a string name as argument. so, the compiler wont put the default constructor in the code. now, if you try to do: Car obj = new Car(); you will get an error because this constructor is not defined.


Classes in Java inherit constructors from their parent classes. If you don't explicitly define a parent class, then Object is used, which has only the default empty constructor. That "default" constructor is only there when defined by the parent class, so classes which do not have a no-argument constructor will not allow subclasses to automatically use it. This is implemented this way because of the special nature of constructors. Java could not always provide a default constructor because it could not guarantee that all class members would be properly created or initialized.


A constructor is not a mandatory member that we need to code specifically for a class. While creating a class, even if we miss out coding the constructor, Java would create a default constructor all by itself. The constructor is usually the place where we initialize things that are required by the class. Hence it is a good practice to code the constructor for our class. Tip: If you do not want anyone to instantiate your class, you can declare the constructor as private. In that way no other class can instantiate your class.


Yes, you can use, and often you should, use more than one constructor in a class in C++. Normally, there is a default constructor, a copy constructor, and one or more conversion constructors. Sometimes, there are also other constructors, overloaded based on argument signature, if there are complex situations.


A default constructor is generated for a class if we did not specify any constructor for it. A default constructor is a public constructor that takes no arguments. For example: public class Test { } The compiler will turn this into: public class Test { public Test() { ... } }


Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples: class Car { Car() { } Car(String s) { } } The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments.


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.