0
difference between constructor and function?and also one similarity
2 Answers
+ 2
constructor used during creation of java objects. where as methods will be called with reference of object variable. constructor has no return type mentioned including void where as method will have return type mentioned.
Similarity, method and constructor can accept parameters.
0
methods are written by the programmers.
even constructors are.
But there are default constructors too. So even if you don't define a constructor, there is always a default constructor that is being called, whenever an object is created.
Whereas, method has to be defined by the programmers, and must be called by the programmer only cz there is no default calling of methods.
one big difference is: Constructors have the name of the class in which they're being defined. We can't have a method name same as the class name. Also Constructors don't have any return type, whereas methods must have one.