Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Deriving Classes

Posted on 2003-10-28
6
Medium Priority
?
494 Views
Last Modified: 2010-04-17
How can i make a class not derivable  by another classes?
0
Comment
Question by:kallzz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
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 …
Six Sigma Control Plans

618 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