d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Some Questions About Constructors
Add Reply New Topic New Poll
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
May 8 2016 12:34am
First, what if you need a different value for each object...

this is the code the book gives me for constructors

http://pastebin.com/8U6BBztT


i mean, doesn't seem very efficient if each object needs different value....
for instance:
minivan.passengers = 7;
sportscar.passengers = 2;



also i was wondering if you could create a constructor like this, for simpler code (if this works?):
http://pastebin.com/Njb837C2
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 8 2016 03:26am
Then you can define a constructor with a parameter.


Code

public class Car
{
private int passengers;

public Car(int passengers)
{
this.passengers = passengers;
}
}
Member
Posts: 3,451
Joined: Feb 26 2010
Gold: 0.20
May 9 2016 02:24pm
Quote (Minkomonster @ May 8 2016 04:26am)
Then you can define a constructor with a parameter.


Code
public class Car
{
private int passengers;

public Car(int passengers)
{
this.passengers = passengers;
}
}


Building off this, you can overoad constructors with different params.

Code
public class Car
{
private int passengers;
private int carType;

public Car(int passengers)
{
this.passengers = passengers;
this.carType = 0; // default value
}

public Car(int passengers, int carType)
{
this.passengers = passengers;
this.carType = carType;
}
}


This post was edited by silvermace on May 9 2016 02:25pm
Member
Posts: 20,253
Joined: Apr 30 2008
Gold: 5,267.97
May 10 2016 02:08am
Additionally, what you tried in your last link is actually correct. You can assign default values to member variables.

Using silvermace's code:

Code
public class Car
{
private int passengers = 4; // at object creation, this variable will be populated with "4" by default.
private int carType = 0; // "0" by default.

public Car(int passengers)
{
this.passengers = passengers; // default variable is overwritten
// Note: carType is not assigned here, which means the default "0" is retained.
}

public Car(int passengers, int carType)
{
this.passengers = passengers; // default variable is overwritten
this.carType = carType; // default variable is overwritten
}
}


This post was edited by howtodisappearcompletely on May 10 2016 02:08am
Member
Posts: 5,354
Joined: Dec 15 2008
Gold: 66.63
May 10 2016 07:30pm
For your minivans vs sportscars example, you would put the Constructor in a Vehicle superclass.

No need to define separate constructors for each subclass, unless it is adding a state that doesn't exist in the superclass.
Even then, you would use the superclass constructor and just add a new variable into it.

So, Constructors (when used correctly):
Help with class abstraction
Makes your code more simple / more reusable
Will create less side effects
Your code will be easier to add new features to (i.e. a Sedan class)

This post was edited by Kilos138 on May 10 2016 07:36pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll