[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

Deriving Classes

How can i make a class not derivable  by another classes?
0
kallzz
Asked:
kallzz
1 Solution
 
fsign21Commented:
Which language are you using for your development?
0
 
ultimatemikeCommented:
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
 
fsign21Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
monkesdbCommented:
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
 
kallzzAuthor Commented:
Iam Using c++
0
 
fsign21Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now