Definition : Class which Can't be inherited by other class, that class is called final class. You all knows that final class is inbuilt in java. But in C++ you have to create final class.Two types of Final class, you can create . One who want to create object of final class on Heap and other who wants to create object of Final class on stack. 1. Final class: Object of it will be on heap. Code: class final2 { public: static final2* Create() { return (new final2()) ; } private: ~final2() { ; } }; int main() { final2 *f ; f = final2::Create() ; // Object only on Heap } No one can Inherit this final2 class. Suppose if one is going to inherit it then Example: Code: class child : public final2 { public: child(){ ;} } ; Now Error will come.Child class can'nt inherit final class.. Because destructor of final class is private. 2. Final class: Object of it will be on Stack Code: class temp { private: ~temp() {; } friend class Final; // Due to friend , Final class can use private member functions of temp class }; class Final: virtual public temp { // Define all data members and functions as you want public: Final() { ; } ~Final(); }; Now this final class can't be inherited by other class.Suppose one is going to inherit it to his class then, Example: Code: class Child : public Final { public: Child(){ ;} ~Child() { ;} }; Due to temp class is inherited virtually to Final class. So when child class's constructor called, then first temp class constructor will be called. By Child class's constructor, we are calling temp class constructor. Child class can't call temp class's private destructor because child is not a friend of temp class. This is the rule of C++. Due to this rule in C++, Complier sees that it is violating the rules, then it flashes error. Extra Dose: In Both Example, Example 1. In place of declaring Destructor of final2 class as private and Example 2. In place of declaring Destructor of temp class as private You can declare private as constructor of them. But you know that constructor can be overloaded but Destructor can'nt be Overloaded. So to restrict outside to not use with 100% safe , define Destructor as private than defining it's constructor
so.... by defining destructor as private we can get final class... but can anyone tell me that.. is private member inherited or not...