praveen
What is the difference between constructor and method?
By praveen in OOP/OOD on Feb 14 2007
  • Shaik Hanumantha Rao
    Sep, 2007 19

    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)

    • 1
  • Ashish Y
    Jun, 2007 11

    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.

    • 1
  • Rahul Prajapat
    May, 2015 30

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

    • 0
  • Mar, 2007 8

    select * from

    • 0
  • Mar, 2007 8

    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

    • 0
  • Srikanth chennu
    Feb, 2007 20

    Constructor is used to create an instance of  object.

    Method is used to perform some operations.

    • 0
  • SUJITKUMAR PATIL
    Feb, 2007 14

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

    • 0
  • praveen
    Feb, 2007 14

    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. 

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS