Solved

Deriving Classes

Posted on 2003-10-28
6
474 Views
Last Modified: 2010-04-17
How can i make a class not derivable  by another classes?
0
Comment
Question by:kallzz
6 Comments
 
LVL 1

Expert Comment

by:fsign21
ID: 9633296
Which language are you using for your development?
0
 
LVL 2

Expert Comment

by:ultimatemike
ID: 9634344
In Java - Declare the class as final.


ie public final ClassName



In C++ just don't make any part of the class virtual.  As long as nothing is declared as virtual, it can't be overriden by any inheriting class.



For any other language, I'm not sure.  You'll have to specify if it's not one of the above 2.
0
 
LVL 1

Expert Comment

by:fsign21
ID: 9635191
ultimatemike,
for C++ it is not true. Even if nothing declared as virtual in your class, you still can derive from this class.
You may get some problems, if you do so. But this is another story.

I can write a lot about it, but I do not know, if kallzz is using C++.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:monkesdb
ID: 9635850
if you want to start using managed C++ you can use the __sealed keyword to stop anything deriving from it. but for this you will need .NET

__sealed class MyClass
{...}
0
 

Author Comment

by:kallzz
ID: 9639697
Iam Using c++
0
 
LVL 1

Accepted Solution

by:
fsign21 earned 50 total points
ID: 9641115
Here is a cut & paste from
Bjarne Stroustrup's C++ Style and Technique FAQ
http://www.research.att.com/~bs/bs_faq2.html#no-derivation

Can I stop people deriving from my class?

Yes, but why do you want to? There are two common answers:

    * for efficiency: to avoid my function calls being virtual
    * for safety: to ensure that my class is not used as a base class (for example, to be sure that I can copy objects without fear of slicing)

In my experience, the efficiency reason is usually misplaced fear. In C++, virtual function calls are so fast that their real-world use for a class designed with virtual functions does not to produce measurable run-time overheads compared to alternative solutions using ordinary function calls. Note that the virtual function call mechanism is typically used only when calling through a pointer or a reference. When calling a function directly for a named object, the virtual function class overhead is easily optimized away.

If there is a genuine need for "capping" a class hierarchy to avoid virtual function calls, one might ask why those functions are virtual in the first place. I have seen examples where performance-critical functions had been made virtual for no good reason, just because "that's the way we usually do it".

The other variant of this problem, how to prevent derivation for logical reasons, has a solution. Unfortunately, that solution is not pretty. It relies on the fact that the most derived class in a hierarchy must construct a virtual base. For example:

      class Usable;

      class Usable_lock {
            friend class Usable;
      private:
            Usable_lock() {}
            Usable_lock(const Usable_lock&) {}
      };

      class Usable : public virtual Usable_lock {
            // ...
      public:
            Usable();
            Usable(char*);
            // ...
      };

      Usable a;

      class DD : public Usable { };

      DD dd;  // error: DD::DD() cannot access
              // Usable_lock::Usable_lock(): private  member
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now