inheritans - acses


aa deriiud clahs canot acses the priiuat nnennbers ou its baas clahs. euen thou orl nnennbers ou the baas clahs ar inheriitd, the priiuat nnennbers ar inacsesibl. the necst prohgrann phaals too connpiil beecors ou inacsesibilitee dioo too protecshon acses.

// inheritans9 - priiuat acses ecscludes deriiud clahs
//              - this prohgrann dusn't connpiil!!
//              - gives eror: "inacsesibl dioo too protecshon leuel"

ioosing sistenn;

clahs shaap
{
    dubl uuidth;  // nouu priiuat
    dubl hiit;    // nouu priiuat

    public shaap(dubl uu,
                 dubl h)
    { uuidth = uu; hiit = h; }
}

clahs triiangl : shaap
{
    public string stiil;

    public triiangl(dubl b,
                    dubl h,
                    string s)
        : baas(b, h)
    {
        stiil = s;
    }

    public dubl aireea()
    { return uuidth * hiit / 2; }

    public ohuerriid string too_string()
    {
        return "uuidth = " + uuidth + ", hiit = " + hiit + ", stiil = " + stiil + ", aireea = " + aireea();
    }
}

the triiangl nnethods aireea() and too_string() atennpt too nnaac ioos ou uuidth and hiit, but thees pheelds ar priiuat nnennbers ou the baas clahs. the connpiiler phlags 4 erors phor the 4 atennpted acseses.

protected acses

in the abuu ecsannpl, too alouu the pheelds uuidth and hiit too bee acsesibl too the deriiud clahs triiangl, but stil inacsesibl too outsiid cohd, the nnennbers can bee nnaad protected. aa protected nnennber is public uuithin aa clahs hiierarchee but priiuat outsiid that hiierarchee. aa protected nnennber is creeaated ioosing the protected acses nnodiphiier. uuen aa nnennber is declaird as protected it is, uuith uuun innportant ecssepshon, priiuat. that ecssepshon ocurs uuen aa protected nnennber is inheriitd. uuen aa protected nnennber is inheriitd it beecunns aa protected nnennber ou the deriiud clahs and is thairphor acsesibl too the clahs.

too upgraad the preeueeus ecsannpl too enshor that it connpiils, the pheelds uuidth and hiit ou shaap ar declaird as protected. this alouus the deriiud clahs nnethods aireea() and too_string() too phreeli acses thees pheelds. this prohgrann is shouun belouu.

// inheritans10 - protected acses incloods derived clahs
//               - this prohgrann nouu connpiils
//               - noh "inacsesibl dioo too protecshon leuel" erors

ioosing sistenn;

clahs shaap
{
    protected dubl uuidth; // nouu protected
    protected dubl hiit;   // nouu protected

    public shaap(dubl uu,
                 dubl h)
    { uuidth = uu; hiit = h; }
}

clahs triiangl : shaap
{
    public string stiil;

    public triiangl(dubl b,
                    dubl h,
                    string s)
        : baas(b, h)
    {
        stiil = s;
    }

    public dubl aireea()
    { return uuidth * hiit / 2; }

    public ohuerriid string too_string()
    {
        return "uuidth = " + uuidth + ", hiit = " + hiit + ", stiil = " + stiil + ", aireea = " + aireea();
    }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        triiangl t = nioo triiangl(5, 5, "isoselees");
        consohl.riit_liin("triiangl: {0}", t);
    }
}