OOP concept by destructors? -


which object oriented programming concept displayed destructors? example, overloading shows polymorphism. please explain reason answer. not find anywhere on web..

most of key oop concepts shown destructors. if consider concepts including inheritance, object, class, encapsulation, method, message passing, polymorphism, abstraction, composition, delegation , open recursion. can show of them being @ play in destructors generally.

now, "destructor" means method defined in class automatically invoked when object destroyed*. covers method, object , class.

destructors encapsulate clean-up logic. consider structure can point structure:

struct somestruct {    somestruct* next; } 

if above written in language didn't support object-oriented design letting define method on somestruct itself, , deletes heap objects global delete() method, clean all memory used somestruct we'd need like:

cleanupsomestruct(somestruct* todelete) {   while(todelete != null)   {     somestruct* deletenext = somestruct->next;     delete(todelete);     todelete = deletenext;   } } 

notably:

  1. we have work clean somestruct outside of somestruct.
  2. there's nothing prevent being duplicated elsewhere. perhaps incorrectly.
  3. we have able access next directly, increasing number of places might unwise it.
  4. if have cases shouldn't delete next, while knowledge stored in somestruct (perhaps ownsnext field), logic of acting on knowledge external somestruct.

if have destructors then:

struct somestruct {    somestruct* next;    ~somestruct()    {      if(next != null) delete(next);    } } 

in contrast above:

  1. the work clean somestruct in somestruct, close other somestruct-related code.
  2. it's possible have concept of access-control, can prevent being duplicated elsewhere.
  3. likewise, access-control can stop other cases of accessing next directly, decreasing number of places might unwise it.
  4. if have cases shouldn't delete next, logic can stored in 1 place internal somestruct.

on flip side, since encapsulation means may not able access next encapsulation means have have destructors able clean objects (or give method explicit "clean up" method, having remember call every time, , know if exists in given case, , it's called drag). example wouldn't matter if had automatic garbage collection, in cases need have destructors of sort if encapsulation blocked outside code necessary clean-up task.

likewise, if have inheritance, need have inheritable destructors can pass clean-up task line either implicitly or explicitly:

struct someotherstruct : somestruct {   somestruct* prev;   ~someotherstruct()   {     if(prev != null) delete(prev);     base.~somestruct(); // possibly implicit in                         // language automatically make                         // call destructor finish call                         // base destructors.   } } 

this requires destructors abstract in general sense of being part of abstract model of object (it may not have abstract keyword used enforce abstraction in other contexts). must polymorphic delete(prev) calls derived destructor in object pointed prev whether ~somestruct(), ~someotherstruct() or destructor of yet derived type, message passing/dynamic dispatch used find correct implementation of abstract/virtual destructor. (a language may enforce this, or may allow non-virtual destructors optimisation).

finally destructors interact encapsulation , open recursion in have (perhaps implicitly) call destructors of objects composed of, , perhaps call methods on this.

*"when object destroyed" plainer concept languages/frameworks using deterministic deletion of objects non-deterministic garbage collection. in later case "destructor" refers method runs when non-deterministic collection happens (assuming ever does) if have separate "disposal" method can deterministic may serve of rĂ´les deterministic destructors serve. in particular while deterministic destructors can useful in providing raii technique, non-deterministic destructors not , use of raii-like approaches have part of deterministic disposal.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -