Results 1 to 6 of 6

Thread: Prevent inheritance in C #

  1. #1
    Join Date
    Dec 2010
    Posts
    37

    Prevent inheritance in C #

    Hello my friends, it gives me a great pleasure to write in this technical forum, as this forum is not just technical but solves the programming related query as well, so for that reason I too decided to post in my program related query in this forum, I know a bit about the inheritance( not fully) and that too I had learned from this forum . I just wanted to ask is there any way that will enable a programmer to prevent a class being inherited by some other class , if you are aware about it then please let me know.

  2. #2
    Join Date
    May 2009
    Posts
    529

    Re: Prevent inheritance in C#

    You said you had a bit of knowledge about the inheritance but you are not fully aware of it, so I will recommend you to first learn about the inheritance then learn on how you can stop it. When a class inherits the data members and function privileges of another class by establishing the parent child relation ship then it is aid that the child class or the sub class inherits the data members and member functions of the parent class or the base class. The child class must have the right in order to do so, if you swish that a class must be prevented from being prevented you can use the concept of Sealed class . Although there are many purpose of creating a sealed class , but the primary objective is to prevent it to be inherited from the other class from inheriting it.

  3. #3
    Join Date
    Apr 2009
    Posts
    488

    Re: Prevent inheritance in C #

    I can explain you the how can create a sealed class with the help of a program, in the following code, I create a sealed class named as Seal and use it from demo. Now here in this code if you make any attem to inherit the class which is sealed you will get an error.
    using System;
    class demo
    Code:
    { 
        static void Main(string[] args) 
        { 
            Seal s = new Seal(); 
            int sum = s.Add(4, 5); 
            Console.WriteLine("sum  = " + sum.ToString()); 
        } 
    } 
    // Sealed class 
    sealed class Seal 
    { 
        public int Add(int a, int b) 
        { 
            return a + b; 
        } 
    }

  4. #4
    Join Date
    Apr 2009
    Posts
    569

    Re: Prevent inheritance in C #

    Sealed classes are used to limit the inheritance characteristic of object oriented programming. Once a class is declared as sealed class, this class cannot is not subject to inheritance. In programming languages such as C#, the sealed modifier is mostly used to name a class as sealed. In other languages such as Visual Basic .NET, NotInheritable keyword provides the reason of being sealed. If you attempt to inherit any class from a sealed class, compiler is bound to throw an error. If you have ever observed, structs are always sealed. You cannot inherit a class from a struct.

  5. #5
    Join Date
    May 2009
    Posts
    543

    Re: Prevent inheritance in C #

    The major reason of a sealed class to eliminate the inheritance characteristic from the user so they cannot inherit a class from a sealed class. One of the best practices of sealed classes is when your class has static data members in it . For example, the Pencils and colors classes of the System.painting namespace. The Pencils class stand for the pencils used for normal colors. This class is composed of static members .So when you're developing your application, you will have to bear in mind that you have sealed classes to close user's limitations.

  6. #6
    Join Date
    Sep 2004
    Location
    On your monitor
    Posts
    11

    Re: Prevent inheritance in C #

    as everyone told here..
    It is as simple as adding the sealed keyword..

    sealed class classname
    {

    }

Similar Threads

  1. Replies: 3
    Last Post: 08-01-2011, 06:32 AM
  2. What is an Inheritance in C#?
    By - Empty Shell - in forum Software Development
    Replies: 4
    Last Post: 09-02-2010, 07:03 AM
  3. What does Inheritance mean in java?
    By Mithun Seth in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 11:45 AM
  4. Polymorphism VS Inheritance
    By Taylor D in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 10:12 AM
  5. Genericity and Inheritance
    By Ricky58 in forum Software Development
    Replies: 4
    Last Post: 30-10-2009, 11:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,555,987.01846 seconds with 17 queries