• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Diffrence b/w constructor and method Breiefly

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any body gives me three diffrences between constructor and method
atleast 3 diffrences must explain breifly thank you bye
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic