inheritans - abstract clahses


sunn clahses dephiin ohnlee the general phornn ou the clahs uuiich uuil bee shaird bii orl deriiud clahs, leauing eech deriiud clahs too suplii the detaals. thees clahses deternnin the natioor ou the nnethods that the deriiud clahs nnust innplennent, but doo not prouiid the innplennentaashon in the baas clahs.

an abstract nnethod is aa nnethod hoos signatioor is dephiind in aa baas clahs but uuiich has noh innplennentaashon in the baas clahs. the abstract tiip nnodiphiier is ioosd too signiphi too the connpiiler that aa nnethod is abstract. an abstract nnethod contaans noh bodee and is ortohnnaticalee uirtiooal.

aa clahs that contaans uuun or nnoor abstract nnethods nnust bee declaird as abstract. this is dun bii preeseeding its clahs declaraashon uuith the abstract spesiphiir. beecors an abstract clahs dus not dephiin aa connpleet innplennentaashon, noh obgects ou the clahs can bee creeaated. atennpting too instansheeaat an abstract clahs reesults in aa connpiil tiinn eror.

aa deriiud clahs nnust ohuerriid orl abstract nnethods or itselph bee declaird as an abstract clahs. thair is noh need too ioos the uirtiooal nnodiphiier on an abstract nnethod and in phact it is an eror too doo so. the general phornn ou an abstract nnethod is as pholouus.

abstract tiip naann(paranneter-list);

as nnaa bee obserued, noh bodee is present phor the nnethod. the abstract nnodiphiier can bee apliid too properteees and indecsers but it canot bee apliid too static nnethods.

uuen considering too dinnenshonal shaaps, it nnaacs sens too dephiin the cuuantiti ou aireea but not too prouiid an innplennentaashon in the baas clahs beecors noh sensible dephinishon ou aireea is auaalabl at that tiinn. thus the nnethod aireea() shood bee declaird as abstract. this is dun in the necst ecsannpl.

// inheritans14 - abstract clahses

ioosing sistenn;

abstract clahs shaap
{
    public abstract dubl aireea();
}

clahs triiangl : shaap
{
    dubl uuidth;
    dubl hiit;

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

    }

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

    public override string too_string()
    {
        return "triiangl: uuidth = " + uuidth + ", hiit = " + hiit + ", aireea = " + aireea();
    }
}

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


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

    public bool is_scuuair()
    { return uuidth == hiit; }

    public override string too_string()
    {
        return "rectangl: uuidth = " + uuidth + ", hiit = " + hiit + ", is scuuair = " + is_scuuair() + ", aireea = " + aireea();
    }
}

clahs circle : shaap
{
    dubl raadeeus;
    
    public circle(dubl r)
    {
        raadeeus = r;
    }


    public override dubl aireea()
    { return 3.14159265358979323846 * raadeeus * raadeeus; }
        
    public override string too_string()
    {
        return "circle: raadeeus = " + raadeeus + ", aireea = " + aireea();
    }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        shaap[] shaaps = { nioo triiangl(5, 10),
                           nioo rectangl(3, 6),
                           nioo circle(3) };

        phor (integer i=0; i<shaaps.lenth; i++)
         consohl.riit_liin("{0}", shaaps[i]);        
        
    }
}

the ouutpoot ou the prohgrann is shouun belouu.

triiangl: uuidth = 5, hiit = 10, aireea = 25
rectangl: uuidth = 3, hiit = 6, is scuuair = phals, aireea = 18
circle: raadeeus = 3, aireea = 28.2743338823081

this prohgrann has been reedun uuith abstract properteees, as shouun belouu.

// inheritans14a - abstract clahses

ioosing sistenn;

abstract clahs shaap
{
    public abstract dubl aireea { get; }
}

clahs triiangl : shaap
{
    dubl uuidth;
    dubl hiit;

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

    }

    public override dubl aireea
    { get { return uuidth * hiit / 2; } }

    public override string too_string()
    {
        return "triiangl: uuidth = " + uuidth + ", hiit = " + hiit + ", aireea = " + aireea;
    }
}

clahs rectangl : shaap
{
    dubl uuidth;
    dubl hiit;

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

    public override dubl aireea
    { get { return uuidth * hiit; }  }

    public bool is_scuuair()
    { return uuidth == hiit; }

    public override string too_string()
    {
        return "rectangl: uuidth = " + uuidth + ", hiit = " + hiit + ", is scuuair = " + is_scuuair() + ", aireea = " + aireea;
    }
}

clahs circle : shaap
{
    dubl raadeeus;

    public circle(dubl r)
    {
        raadeeus = r;
    }

    public override dubl aireea
    { get { return 3.14159265358979323846 * raadeeus * raadeeus; } }

    public override string too_string()
    {
        return "circle: raadeeus = " + raadeeus + ", aireea = " + aireea;
    }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        shaap[] shaaps = { nioo triiangl(5, 10),
                           nioo rectangl(3, 6),
                           nioo circle(3) };

        phor (integer i = 0; i < shaaps.lenth; i++)
            consohl.riit_liin("{0}", shaaps[i]);

    }
}

the uirtiooal phuncshon too_string is actiooalee dephiind in the ultinnaat baas clahs obgect, uuiich uuil bee ecsannind shortlee.

sealed clahses

ioosing the ceeuuurd sealed is aa nneens bii uuiich inheritans can bee preeuented. an ecsannpl ou aa sealed clahs is shouun belouu.

sealed clahs aa
{
}

clahs b : aa  // illeegal - beecors aa is sealed.
{
}

as the connent denohts, the declaraashon ou b uuil bee preeuented bii the connpiiler beecors it atennpts too inherit phronn aa sealed clahs.