Reputation: 365
My first class in Object-Oriented Programming.
should I create a constructor method for every class ?
for example
public class Cube2 {
int length;
int breadth;
int height;
public int getVolume() {
return (length * breadth * height);
}
Cube2() {
this(10, 10);
System.out.println("Finished with Default Constructor");
}
Cube2(int l, int b) {
this(l, b, 10);
System.out.println("Finished with Parameterized Constructor having 2 params");
}
Cube2(int l, int b, int h) {
length = l;
breadth = b;
height = h;
System.out.println("Finished with Parameterized Constructor having 3 params");
}
public static void main(String[] args) {
Cube2 cubeObj1, cubeObj2;
cubeObj1 = new Cube2();
cubeObj2 = new Cube2(10, 20, 30);
System.out.println("Volume of Cube1 is : " + cubeObj1.getVolume());
System.out.println("Volume of Cube2 is : " + cubeObj2.getVolume());
}
}
if I want to create a new constructor should I use the same parameters that used in the previous(first) one?
like the constructor Cube2(int l, int b, int h)
has the same to parameters for this one Cube2(int l, int b)
in addition to the variable (int h)
Upvotes: 3
Views: 364
Reputation: 462
If your class has attributes but you won't be passing in new values, or you don't want a programmer to be able to pass any in, at its instantiation then you can just have the default constructor (ie. with no parameters).
every class will (Should) have at least a default constructor like:
public class Cube {
Cube(){}
}
Upvotes: 1
Reputation: 41097
Constructor
has to be created on a as needed basis. A default Constructor
is always provided by the compiler if no other Constructor
is created.
Upvotes: -1
Reputation: 22415
There is no rule a constructor needs to have the same parameters as another constructor. It is just something that ends up happening due to additional constructors usually only needing to differ slightly.
Any class that you do not define a constructor for implicitly has a no-argument constructor. However, you must explicitly declare a no-argument constructor, if you want it, when you declare others. So, unless you need information or logic in order to construct the object, no explicit constructor is needed.
Upvotes: 2
Reputation: 74277
Every class has at least one constructor, whether or not it's explicitly defined.
If you don not declare a constructor explicitly, one is defined for yout: the default parameterless constructor — a no-op — which invokes the parent class' parameterless constructor (super()
).
You will need to declare a constructor — even if it does nothing — if the parent class lacks a parameterless constructor: if the parent class has only constructors with parameters, a constructor for the parent must be explicitly invoked; hence, you'll need to define at least one constructor for your class.
You might also want to define your class' constructor(s) with visibility other than public (e.g., private
) if the class isn't to be instantiated by the general public. Some argue that it's a good practice to always keep the visibility of a class' constructors restricted and only allow public instantiation via factory methods — the consumer shouldn't care what the actual object type is, only that it is a subtype of the desired type. This provides more flexibility down the line.
Don't forget that the class instance should be "ready for use" once the instance is fully constructed. Definition of "ready for use", of course, is a fairly adjustable notion.
Upvotes: 3
Reputation: 1530
Create the constructors that the user of the class will use. This is really just a consequence of the more general idea of designing APIs for the user. Try writing the code that will use your class first, then you will see what methods you need.
If you have a method that is the same as another but adds an argument it is easier to understand if you name and order the other arguments consistently.
Your first constructor is not a default constructor. The default constructor is what you get if you don't write any - it is impossible for you to write one. What you have written is a no-args constructor.
Upvotes: 2
Reputation: 49187
There are many cases when a constructor isn't necessary, in particular if you're planning to use inversion of control, where a single default constructor is desired.
However for your cube example, it would make sence two provide one or two constructors. A default constructor if you feel it's necessary to manipulate the cube parameters after construction, and/or one which takes the the three arguments necessary to actually create a cube.
However initalizing the object using either of your first two constructors doesn't make sence to me in this case since there is usually no such thing as sensible defaults
for a cube. There might be a sensible default for value timeout
of a tcp
connection.
Upvotes: 4