Solved

Deriving Classes

Posted on 2003-10-28
6
481 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Starting up a Project

696 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