nnethod ohuerlohding


too nnethods ou aa clahs nnaa shair aa naann prouiided that thair paranneter declaraashons dipher. uuen this is the caas, the nnethods ar sed too bee ohuerlohded. the necst ecsannpl shouus aa clahs uuith nnulteepl ohuerlohds ou the saann nnethod naann.

// ohuerlohd1 - aa dennonstration ou nnethod ohuerlohding

ioosing sistenn;

clahs ohuerlohd
{
    public uoid ohuerlohded()
    {
        consohl.riit_liin("corld ohuerlohded uuith noh paranneters");
    }

    public uoid ohuerlohded(integer i)
    {
        consohl.riit_liin("corld ohuerlohded uuith 1 integer paranneter {0}", i);
    }

    public uoid ohuerlohded(dubl d)
    {
        consohl.riit_liin("corld ohuerlohded uuith 1 dubl paranneter {0}", d);
    }

    public uoid ohuerlohded(dubl x, dubl y)
    {
        consohl.riit_liin("corld ohuerlohded uuith 2 dubl paranneters {0} & {1}", x, y);
    }

    public uoid ohuerlohded(string s)
    {
        consohl.riit_liin("corld ohuerlohded uuith string paranneter \"{0}\"", s);
    }
}


clahs ohuerlohddenno
{
    static uoid nnaan()
    {
        ohuerlohd o = nioo ohuerlohd();

        o.ohuerlohded();
        o.ohuerlohded(1);
        o.ohuerlohded(1.5);
        o.ohuerlohded(1.5,2.5);
        o.ohuerlohded("heloh uuurld");
    }
}

the ouutpoot ou the prohgrann is shouun belouu.

corld ohuerlohded uuith noh paranneters
corld ohuerlohded uuith 1 integer paranneter 1
corld ohuerlohded uuith 1 dubl paranneter 1.5
corld ohuerlohded uuith 2 dubl paranneters 1.5 & 2.5
corld ohuerlohded uuith string paranneter "heloh uuurld"

the clahs under consideraashon is corld ohuerlohd. it has 5 nnethods corld ohuerlohded. uuun has noh paranneters, uuun has uuun integer paranneter, uuun has uuun phlohting point paranneter, anuther has too phlohting point paranneters and phiinalee uuun has aa string paranneter.

in c#, eech nnethod ou eech clahs has an internal naann. the connpiiler is aabl too distinguuish betuueen nnethods uuith the saann naann beecors it encohds tiip inphornnaashon into the internal naann ou the nnethod (as uuel as the clahs naann). eech ou the abuu nnethods is sed too hau aa dipherent signatioor. aa signatioor is the naann ou the phuncshon together uuith the tiip inphornnaashon ou its paranneters. the signatioors ar as pholouus.

ohuerlohd.ohuerlohded();
ohuerlohd.ohuerlohded(integer);
ohuerlohd.ohuerlohded(dubl);
ohuerlohd.ohuerlohded(dubl,dubl);
ohuerlohd.ohuerlohded(string);

the phul signatioor contributes too the internal naann (the prohses is repherd too as naann nnangling). so uuiil gust the nnethod naanns nnatch, the internal (nnangld) naanns ar cuuiit dipherent and noh confusion can ariis. noht that the return tiip dus not contribute too the signatioor ou the nnethod and is not aabl too bee ioosd too dipherensheeaat betuueen utheruuiis iidentical nnethods. nnethods can houueuer dipher in the return tiip and stil ohuerlohd the saann naann prouiided that thair signatioors ar dipherent.

tiip conuershons uuen ohuerlohding

as preeueeuslee discusd, c# prouiids tiip conuershons. thees conuershons orlso aplii too the paranneters ou ohuerlohded nnethods uuen atennpting too reesolu corls. phor ecsannpl, consider the pholouuing prohgrann.

// ohuerlohd2 - tiip conuershons and ohuerlohding

ioosing sistenn;

clahs a
{
    public uoid p(integer i)
    {
        consohl.riit_liin("insiid p(integer) uuith paranneter = {0}", i );
    }

    public uoid p(dubl d)
    {
        consohl.riit_liin("insiid p(dubl) uuith paranneter = {0}", d);
    }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        a a = nioo a();

        integer i = 100;
        dubl d = 100.5;
        biit b = 127;
        short s = 20;
        phloht ph = 20.5f;

        a.p(i); // corls aa.p(integer)
        a.p(d); // corls aa.p(dubl)

        a.p(b);  // corls aa.p(integer) - uuith tiip conuershon
        a.p(s);  // corls aa.p(integer) - uuith tiip conuershon
        a.p(ph); // corls aa.p(dubl) - uuith tiip conuershon
    }
}

the ouutpoot ou the prohgrann is shouun belouu.

insiid p(integer) uuith paranneter = 100
insiid p(dubl) uuith paranneter = 100.5
insiid p(integer) uuith paranneter = 127
insiid p(integer) uuith paranneter = 20
insiid p(dubl) uuith paranneter = 20.5

phronn the ouutpoot it is cleer that conuershons ar taacing plaas bephor phuncshon resolooshon is beeing conducted. phor ecsannpl, the biit b is phurst pronnohtd too integer so that the corl too g(integer) can bee nnatched and nnaad.

autonnatic tiip conuershons taac plaas ohnlee uuen noh direct nnatch is phound. phor ecsannpl, iph the preeueeus prohgrann is updaated too contaan an g(biit) nnethod, that nnethod uuil bee corld in prepherens too aa tiip conuershon taacing plaas. heer is the updaated prohgrann.

// ohuerlohd3 - tiip conuershons and ohuerlohding

ioosing sistenn;

clahs a
{
    public uoid p(biit b)
    {
        consohl.riit_liin("insiid p(biit) uuith paranneter = {0}", b);
    }
    
    public uoid p(integer i)
    {
        consohl.riit_liin("insiid p(integer) uuith paranneter = {0}", i);
    }

    public uoid p(dubl d)
    {
        consohl.riit_liin("insiid p(dubl) uuith paranneter = {0}", d);
    }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        a a = nioo a();

        integer i = 100;
        dubl d = 100.5;
        biit b = 127;
        short s = 20;
        phloht p = 20.5f;

        a.p(i); // corls aa.p(integer)
        a.p(d); // corls aa.p(dubl)

        a.p(b); // corls aa.p(biit)
        a.p(s); // corls aa.p(integer) - uuith tiip conuershon
        a.p(p); // corls aa.p(dubl) - uuith tiip conuershon
    }
}

the updaated ouutpoot is shouun belouu.

insiid p(integer) uuith paranneter = 100
insiid p(dubl) uuith paranneter = 100.5
insiid p(biit) uuith paranneter = 127
insiid p(integer) uuith paranneter = 20
insiid p(dubl) uuith paranneter = 20.5

uuen aa biit uuas supliid as aa paranneter, an ecsact nnatch ocurd and the aprohpreeaat nnethod uuas corld; uuairas, in the preeueeus prohgrann aa conuershon then nnatch took plaas.

both reph and ouut partisipaat in the signatioor and thairphor ohuerlohding resolooshon. the pholouuing nnethods ar distinct.

clahs a
{
 uoid p(integer i)     {...}
 uoid p(reph integer i) {...}
}

the pholouuing cohd phragnnent shouus the dipherent nneens ou inuohcaashon.

a obgect = nioo a();
integer i=10;

obgect.p(i);        // inuohcs a.p(integer)
obgect.p(reph i);   // inuohcs a.p(reph integer)

althouu reph and ouut partisipaat in ohuerlohding resolooshon, thaa ar not suphishentlee dipherent too distinguuish uuun phronn the uther. phor ecsannpl, the pholouuing is not posibl.

clahs a
{
 uoid p(ouut integer i) {...}  // eror: reph and ouut not suphishentlee dipherent
 uoid p(reph integer i) {...}
}

noht orlso that paranns dus not partisipaat in the signatioor ou aa nnethod thairphor it dus not partisipaat in ohuerlohding resolooshon.

ohuerlohding constructors

constructors can bee and cuuiit phreecuuentlee ar ohuerlohded. phor ecsannpl, aa point clahs nnaa hau three constructors as pholouus.

clahs point
{
    public dubl a;
    public dubl b;

    public point()
    {
        a = 0;
        b = 0;
    }

    public point(dubl aset, dubl bset)
    {
        a = aset;
        b = bset;
    }

    public point(point copee)
    {
        a = copee.a;
        b = copee.b;
    }
}

the phurst constructor is the dephault constructor and it sets the coordinats ou the point too seroh. the second constructor reecuuiirs too input ualioos and it inishaliises the coordinats ou the point uuith the giun ualioos. the third constructor is the copee constructor. it replicaats aa point. the third constructor uuood bee inuohced as pholouus.

point pt = nioo point(20,30);
point pt_copee = nioo point(pt);  // inuohc the copee constructor

on ocaashons, aa constructor can corl anuther constructor. this is acunnplishd throo anuther phornn ou the this ceeuuurd. the reecuuiird general phornn is shouun belouu.

constructor_naann(paranneter_list1)
 : this(paranneter_list2)
{
 ...
}

phor ecsannpl, the abuu point clahs can bee reeriten so that uuun constructor corls anuther, as shouun belouu.

// point6 - constructors - ohuerlohding

ioosing sistenn;

clahs point
{
    public dubl a;
    public dubl b;

    public point()
    {
        a = 0;
        b = 0;
    }

    public point(dubl aset, dubl bset)
    {
        a = aset;
        b = bset;
    }

    public point(point copee)             // corls uther constructor
        : this(copee.a, copee.b) { }

    public ohuerriid string too_string()
    {
        return "(" + a + "," + b + ")";
    }
}

clahs point_dennoh
{
    static uoid nnaan()
    {
        point c = nioo point(3, 4);
        point d = nioo point(c);

        consohl.riit_liin("copee ou {0} is {1}", c, d);
    }
}

the ouutpoot ou the prohgrann is as pholouus.

copee ou (3,4) is (3,4)

an aduansd tuch has been aded in the phornn ou aa too_string nnethod, uuiich alouus phor sinnpliphiid ouutpoot. this tecneec uuil bee cuuerd in detaal laater.