8
Reply

What is the difference between constructor and method?

praveen

praveen

17y
41.6k
0
Reply

    a constructor is a method in the class which gets executed when its object is created. Usually, we put the initialization code in the constructor. Writing a constructor in the class is damn simple, have a look at the following sample:

    public class Test

    {

        public Test()

        {

            //Initilize code in the constructor.
        }
    }

    • Constructors doesn't returns any value.
    • References and pointers cannot be used on constructors and destructors because their addresses cannot be taken.
    • Constructors cannot be declared with the keyword virtual.
    • If the constructors is defined as private, we can not create instance of that class.

    Method:

    •  In methods we can define variables.These variables scope is within methods only.
    • If you declare a public variable , it will be accessed in all mehtods.
    • Methods may/maynot contains return type.
    • Methods could be inhereted in derived class(Note That mehtod should be Public)

    Constructor is used to create an instance of class. while method is used to perform a specific operation...

    1. constuctor is a method of name ""same of class"" and is automatically called when the object of that class is created.
    2. The methods aren't called when object is created,We have to call by using its class name.

    select * from

    17y
    0

    by default constructor's return type is that of a class in which it is defined so no need to write explicitly the return type. [don't know if u know c or not, if yes then it is mallock type casted to the pointer of that class type] a method must[not can...., as even if it returns nothing it should have a return type of VOID] have a return type. just as methods constructors can be public,private,static etc. but as a general guideline it is advisable to have constructors public. constructor anme should be exactly[case sensitive] same as the calss name last but not the least even if u dont define a default constructor then when you instanciate an object ...it is the system who defines one for you and calls it in order to create an instance of object... Regards Sam

    17y
    0

    Constructor is used to create an instance of  object.

    Method is used to perform some operations.

    ->constructor call automatically . constructor is also public or private. constructor is for memory management ->method call forcefully . method only focus on functionality

    1. constructor name should match with a class name, but for a method a name could be of anything.

    2.constructor don't have any return type, where as a method can have a return type.