Thread View: aus.computers.java
5 messages
5 total messages
Started by Jonathon Bates
Sun, 23 Apr 2000 00:00
Constructors
Author: Jonathon Bates
Date: Sun, 23 Apr 2000 00:00
Date: Sun, 23 Apr 2000 00:00
35 lines
1695 bytes
1695 bytes
Hi All, I have a quick question in regards to constructors! When compiling a class file, I get the following error: [batesy@batesy assignment2]$ javac as2q1class.java as2q1class.java:68: Duplicate method declaration: as2q1class(int) public as2q1class(int theassign_two) ^ as2q1class.java:77: Duplicate method declaration: void set(int) public void set(int newassign_two) ^ as2q1class.java:83: Duplicate method declaration: as2q1class(int) public as2q1class(int thetest_one) ^ as2q1class.java:92: Duplicate method declaration: void set(int) public void set(int newtest_one) ^ as2q1class.java:98: Duplicate method declaration: as2q1class(int) public as2q1class(int thetest_two) ^ as2q1class.java:107: Duplicate method declaration: void set(int) public void set(int newtest_two) ^ 6 errors Now I am soooo far lostand I dont know why! I have both the class + the client file located at http://batesy.iinet.net.au/java/problem/ If someone could please provide some feedback It would be greatly appreciated! I was under the imporession that the code was all correct! Cheers Jonathon Batesy
Re: Constructors
Author: Tom Davies
Date: Sun, 23 Apr 2000 00:00
Date: Sun, 23 Apr 2000 00:00
46 lines
2474 bytes
2474 bytes
If you have two methods (constructor or not) in a class which both have identical signatures (i.e. same name, return type and arguments) how can a client of the class specify which one to call? It just doesn't make sense. I don't see what you want to accomplish with the code after the //now... line. Tom Jonathon Bates wrote: > > Hi All, > I have a quick question in regards to constructors! > When compiling a class file, I get the following error: > > [batesy@batesy assignment2]$ javac as2q1class.java > as2q1class.java:68: Duplicate method declaration: as2q1class(int) > public as2q1class(int theassign_two) > ^ > as2q1class.java:77: Duplicate method declaration: void set(int) > public void set(int newassign_two) > ^ > as2q1class.java:83: Duplicate method declaration: as2q1class(int) > public as2q1class(int thetest_one) > ^ > as2q1class.java:92: Duplicate method declaration: void set(int) > public void set(int newtest_one) > ^ > as2q1class.java:98: Duplicate method declaration: as2q1class(int) > public as2q1class(int thetest_two) > ^ > as2q1class.java:107: Duplicate method declaration: void set(int) > public void set(int newtest_two) > ^ > 6 errors > > Now I am soooo far lostand I dont know why! > I have both the class + the client file located at > http://batesy.iinet.net.au/java/problem/ > > If someone could please provide some feedback It would be greatly appreciated! > I was under the imporession that the code was all correct! > > Cheers > Jonathon Batesy
Re: Constructors
Author: Paul Murray
Date: Tue, 25 Apr 2000 00:00
Date: Tue, 25 Apr 2000 00:00
68 lines
3054 bytes
3054 bytes
These two constructors
public as2q1class(String thestudent_name, int theassign_one, int
theassign_two, int thetest_one, int thetest_two)
public void set(String newstudent_name, int newassign_one, int
newassign_two, int newtest_one, int newtest_two)
Are identical. Why do you have two? The names that you call the variables
don't matter, only the types do.
How does constructing a student from a "student name" differ from
constructing one from a "new student name"?
---------------------------
If there is a genuine difference, then you need a constructor like this:
public as2q1class(String thestudent_name, int theassign_one, int
theassign_two, int thetest_one, int thetest_two, boolean isNewStudent)
{
if(isnewStudent) {
Do new student construction
}
else {
Do old student construction
}
}
---------------------------
If a new student is identical to an old student but with some extras, you
can do this
public as2q1class(String thestudent_name, int theassign_one, int
theassign_two, int thetest_one, int thetest_two) {
do old student constriuction
}
public as2q1class(String newstudent_name, int newassign_one, int
newassign_two, int newtest_one, int newtest_two, boolean isNewStudent) {
// call oldstudent constructor
this(newstudent_name, newassign_one, newassign_two, newtest_one,
newtest_two);
if(isNewStudent) {
Do extra stuff for new students
}
}
---------------------------
These are also Identical.
public void set(int newassign_one) { assign_one = newaswign_one; }
public void set(int newassign_two) { assign_two = newassign_two; }
Lets put it this way. Take this bit of code:
as2q1class z = new as2q1class("Dave");
z.set(3);
When I call set, how is the class supposed to know whether I am trying to
set assign_one or assign_two?
Repeat after me: "In overloading, the names of the *parameters* don't
count" - only the name of the method and the types of the parameters. The
names of the parameters are just things internal to the method - the caller
of a method does not pass names of parameters, it only passes their values.
BTW: the return type doesn't count either.
--------------------------------------------------------------
http://www.users.bigpond.com/pmurray
ICQ: 26066755
Re: Constructors
Author: Tim Cross
Date: Sun, 30 Apr 2000 00:00
Date: Sun, 30 Apr 2000 00:00
87 lines
4052 bytes
4052 bytes
"Paul Murray" <pmurray@nospam.bigpond.com> writes:
> These two constructors
Umm - I don't think these are both constructors! A constructor cannot
have a return type and they have to have the same name.
> public as2q1class(String thestudent_name, int theassign_one, int
> theassign_two, int thetest_one, int thetest_two)
> public void set(String newstudent_name, int newassign_one, int
> newassign_two, int newtest_one, int newtest_two)
>
> Are identical. Why do you have two? The names that you call the variables
> don't matter, only the types do.
They are not identical - one is a constructor, the other is a normal
> class method.
I'd guess the idea is to have one (the constructor) which is used to
initialize the class instance when it is created and the second one is
used to re-set an existing instance (rather than creating a new
instance) so that it can operate on a new student.
>
> How does constructing a student from a "student name" differ from
> constructing one from a "new student name"?
>
[snip]
>
> These are also Identical.
>
> public void set(int newassign_one) { assign_one = newaswign_one; }
>
> public void set(int newassign_two) { assign_two = newassign_two; }
>
> Lets put it this way. Take this bit of code:
>
> as2q1class z = new as2q1class("Dave");
> z.set(3);
>
> When I call set, how is the class supposed to know whether I am trying to
> set assign_one or assign_two?
>
> Repeat after me: "In overloading, the names of the *parameters* don't
> count" - only the name of the method and the types of the parameters. The
> names of the parameters are just things internal to the method - the caller
> of a method does not pass names of parameters, it only passes their values.
> BTW: the return type doesn't count either.
>
This is quite correct. The compiler, when selecting the method
definition which matches a method call first finds all the methods
with the same name which could be candidates - this is determined by
how the method call is specified (e.g. with or without a class/object
identifier and it takes into account access restrictions e.g. public,
private, protected). It then selects from all the candidates those methods
which have the same number of formal arguments as the method
call. finally, it checks that the types of the arguments in the method
call match up with the formal arguments in the method definitions. If
it finds a method which has the right number of arguments of the right
type, all is OK. If it can't, find one or if it can't select one from
a list of multiple candidates, then an error occurs and the
compilation fails. Both the set() methods have the same number of
arguments of the same types which means there is nothing the compiler
can use to distinguish them and so would raise an error.
You would need to have something like
public void set(int newAssignment, int assignmentNumber)
throws BadAssignmentNumberException {
if (assignmentNumber == 1)
assign_one = newAssignment;
else if (assignmentNumber == 2)
assign_two = newAssignment;
....
else
throw new BadAssignmentNumberException(.....);
}
or something (you could use a switch statement instead of if/else).
Tim
--
Tim Cross
NorthNet Internet Services
mailto:tcross@northnet.com.au
phone:+61 2 6772 5973
Re: Constructors
Author: Paul Murray
Date: Mon, 01 May 2000 00:00
Date: Mon, 01 May 2000 00:00
27 lines
1779 bytes
1779 bytes
"Tim Cross" <tcross@northnet.com.au> wrote in message news:m3zoqcb0xh.fsf@tim.northnet.com.au... > "Paul Murray" <pmurray@nospam.bigpond.com> writes: > > > These two constructors > > Umm - I don't think these are both constructors! A constructor cannot > have a return type and they have to have the same name. > > > public as2q1class(String thestudent_name, int theassign_one, int > > theassign_two, int thetest_one, int thetest_two) > > public void set(String newstudent_name, int newassign_one, int > > newassign_two, int newtest_one, int newtest_two) > > > > Are identical. Why do you have two? The names that you call the variables > > don't matter, only the types do. > > They are not identical - one is a constructor, the other is a normal > > class method. Duh! Pure carelessness on my part. I will have to pay better attention on the exam tomorrow. -------------------------------------------------------------- http://www.users.bigpond.com/pmurray ICQ: 26066755
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads