GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Diffrence b/w constructor and method Breiefly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Diffrence b/w constructor and method Breiefly" Watch "Diffrence b/w constructor and method Breiefly" New topic
Author

Diffrence b/w constructor and method Breiefly

khurram babar
Greenhorn

Joined: May 13, 2003
Posts: 4
can any body gives me three diffrences between constructor and method
atleast 3 diffrences must explain breifly thank you bye
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
-the main difference is that a method is doing some business logic e.g. and a constructor is there to create a object on the heap memory. In the constructor you can set attributes of your object.
- A constructor has no return type.
- When a constructor is called and you do not call explicitly a super constructor then java will call the super constructor of the super class. (depending on what you extend your class, default is java.lang.Object)
- a constructor cannot be inherited
This are the main things i think
Olli
adi peretz
Greenhorn

Joined: Jun 14, 2003
Posts: 5
hi
1)constructors needs to be with the same name as the class
2)they can not return a value
3)you can not inherit a constroctor from a perent calss
tapasvi vaishnav
Greenhorn

Joined: Jun 22, 2003
Posts: 3
third point is tricky...U are actually inheriting the constructors...only difference is that you cant override it in the subclass unlike other public methods.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
third point is tricky...U are actually inheriting the constructors
No. Only members of one class can be inherited by a descendant class. Constructors are not members and they are not inherited.
From The JLS:
Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.

[ June 23, 2003: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Diffrence b/w constructor and method Breiefly