Java Programming
C++ Programming

Difference between constructor and method?


Top Answer
User Avatar
Wiki User
Answered 2012-08-16 17:14:17

Assuming you mean Java: A constructor is very similar to a "normal" method, in that it is a place to type commands. The main differences is that a constructor is invoked automatically every time an object of the specified class is instantiated. With respect to syntax, there are two differences: (1) The constructor must have the same name as the class (including upper- and lowercase), and (2) The constructor has no declared return value, not even void.


Your Answer

Related Questions

really there is no difference between constructor overloading and metho overloading

A constructor is a special method that has the same name as the class name, and it cannot be invoked or called like a method call, it must be invoked by the new operator. For example, new Object();

The constructor is a method that runs to initialize the instance of the class. It runs right after memory allocation. The destructor is a method that runs to deinitialize the instance of the class. If runs right before memory deallocation.

Both are functions, i.e., places where you can write code. A constructor is simply a special method that is invoked automatically when an object is created.

A class is simply the definition of a data type. A constructor is a special method of a class that is automatically invoked whenever an object of that type is instantiated, and is used to initialise the object's data members.

A constructor is called during the creation of an instance of that class and it does not have a return type as a member function. It is not a property of the created object and the constructor can not be accessed by the object. It is like a static method that only gets called during object creation.

Constructor is called when the object is created. Destructor is called when the object is destroyed

Overloading constructor means when you have multiple constructors in a class but with different number of arguments. We cannot override a constructor, but we can override a method having same arguments and same return type. This means when I subclass a particular class then I can call the superclass's class method and override that with subclass's own logic.

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.

JavaThere is really no such thing as a static constructor in Java. The closest thing that comes to mind is a factory class, which calls a private constructor when a static method is called.class A {// This private constructor can't be called by any other class.private A() {}// Here is your "static" constructorpublic static A getInstance() {return new A();}}

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.

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.

The main method is the method called when the Java application is started The constructor is called whenever a new object is instantiated.

A constructor is a special method that gets executed when the object is created.

yes,because in constructor overloading constructor have same and different parameter list. In method overloading method have same name and different parameter list.

Default Constructor will be called first . If you override Validate method , then validate method will be called .

No. If you do not write a constructor for you classes, the default Object constructor can still be used.

Every class has at least one it's ownconstructort. Constructor creates a instance for the class. Constructor initiates (initialize) something related to the class's methods. Constructor is the method which name is same to the class. But there are many difference between the method (function) and the Constructor. In this example we will see that how to to implement the constructor feature in a class. This program is using two classes. First class is another and second is the main class which name is Construct. In theConstruct class two objects (a and b) are created by using the overloaded another Constructor by passing different arguments and calculated the are of the different rectangle by passing different values for the another constructor.

Constructor construct assign the value of class object while destructor delete or free memory which is allocated by constructor.

The differences are:Methods have return types but constructors dontMethods can have any name but constructors name must match exactly with the class name

Difference between Percentage of Completion method and Completed Contract method?

What is the difference between the conventional method of dimensioning and the baseline method of dimension

* A constructor is called when you want to create a new instance of a class. A destructor is called when you want to free up the memory of an object (when you delete it). * A constructor constructs the value of an object . A destructor destructs the value created by the constructor for the object. * Another differentiation is their syntax's : constructor : (Arguments){//body of constructor.} destructor : ~() { }The basic difference between constructor and destructor is constructor is called during the beginning of the program whereas the destructor is called at the end of the program.The former allocates memory whereas the later removes the memory.The constructor can be overloaded however the destructor cannot be overloaded.

what is difference between regular simplex method and dual simplex method

Copyright ยฉ 2020 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.