Article View: aus.computers.java
Article #816Re: Constructors
From: 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
Technical Details
Message-ID:
<m3zoqcb0xh.fsf@tim.northnet.com.au>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3902becc$0$20235@echo-01.iinet.net.au> <g58N4.8005$E4.16113@newsfeeds.bigpond.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,ba8cc3bfd1429785 X-Google-Attributes: gid10d6db,public From: Tim Cross <tcross@northnet.com.au> Subject: Re: Constructors Date: 2000/04/30 Message-ID: <m3zoqcb0xh.fsf@tim.northnet.com.au> X-Deja-AN: 617403685 Sender: tcross@tim.northnet.com.au References: <3902becc$0$20235@echo-01.iinet.net.au> <g58N4.8005$E4.16113@newsfeeds.bigpond.com> X-Complaints-To: abuse@telstra.net X-Trace: nsw.nnrp.telstra.net 957073121 203.57.24.28 (Sun, 30 Apr 2000 15:38:41 EST) Organization: Customer of Telstra Big Pond Direct NNTP-Posting-Date: Sun, 30 Apr 2000 15:38:41 EST Newsgroups: aus.computers.java