permaculture playing cards
The moose likes Beginning Java and the fly likes Hide a constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hide a constructor" Watch "Hide a constructor" New topic

Hide a constructor

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Hello Folks

I have a class Notification, which has two constructors and set of Notification Object Parameters

There is a class called NotificationManager, which has a method addNotification which takes in param as a Notification Object. i.e. public void addNotification(Notification notification). Here the Notification object is created using the first Constructor. Then i do some file writing. Notification and Notification manager are in the same package

Then i have a class NavNotificationManager which extends NotificationManager and there is a method in this where i use the second Notification Constructor and store the Notification objects in a list. NavNotificationManager is in a different package.

The issue is that when i do addNotification and pass in the Notification object. I do the following:-

Notification localNotification = new Notification(-----params----);
This option shows that is can take value for both the Notification constructors. I dont want do pass in the values for the second contructor. I tried to made second contuctor private but that does not work.

Can any one advice.


Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

It's hard to advise without understanding the problem.

And your problem is: There are two constructors, and you don't want to use the second constructor? Then my advice would be to not use it. You still have the first constructor. Of course you have said you know that and already use it in some cases, which means I don't understand your problem.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Paul Clapham wrote:It's hard to advise without understanding the problem.

@Aditya:please explain clearly!
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

The constructor that gets called will depend on what parameters you pass in (2 or 3 strings).

Do you mean that your IDE is showing you both options? I tried it in NetBeans, setting the second constructor to private, and it only shows me the first one, not both.
I agree. Here's the link:
subject: Hide a constructor
It's not a secret anymore!