• 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

Overriding Constructor

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am new to Java, can we override a constructor..? and what are the uses of overriding it. is it is a good practice of doing it.

Please help me this was a question asked in interview.
Thanks in adv.

Regards
LM.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you cannot override a constructor. You can overload it, but non-static non-final methods are the only members that can be overridden.
 
little master
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class OverRide {
public OverRide(){
System.out.println("from constructor");
}

}


public class OverRidden extends OverRide {

public void OverRide(){
System.out.println("from OveRidden");
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
OverRidden or = new OverRidden();
or.OverRide();

}

public void myJob() {
// TODO Auto-generated method stub

}

}

OUTPUT:
from constructor
from OveRidden

i have the above code can you please look into it.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) read your private messages and follow the instructions inside.
2) UseCodeTags
3) that's constructor chaining. That's something completely different from overriding.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
edit Cross posted
http://forums.sun.com/thread.jspa?threadID=5437288
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Initials aren't enough. You can change your display name here. Thanks!

Now, as to your question: what you have is a method returning void with the same name as a class. That's not a constructor -- constructors don't have a return type, and you also can't call them directly. You can only call them using "new" or "super". In your OverRide class, that is a constructor.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic