bulk email server

Results 1 to 6 of 6
  1. #1
    Senior Member
    Join Date
    Jul 2017
    Posts
    425
    Feedback Score
    0

    How can we restrict inheritance for a class?

    VPS Hosting
    How can we restrict inheritance for a class?

  2. #2
    Senior Member
    Join Date
    Sep 2017
    Posts
    401
    Feedback Score
    0
    In VB.NET you use the NotInheritable keyword. In order to prevent a class in C# from being inherited, the keyword sealed is used. Thus a sealed class may not serve as a base class of any other class. It is also obvious that a sealed class cannot be an abstract class.

  3. #3
    Member
    Join Date
    Jan 2018
    Posts
    78
    Feedback Score
    0
    My answer is for php:
    You can force a class to be strictly an inheritable class by using the "abstract" keyword. When you define a class with abstract, any attempt to instantiate a separate instance of it will result in a fatal error. This is useful for situations like a base class where it would be inherited by multiple child classes yet you want to restrict the ability to instantiate it by itself.

    Example........

    <?php

    abstract class Cheese
    {
    //can ONLY be inherited by another class
    }

    class Cheddar extends Cheese
    {
    }

    $dinner = new Cheese; //fatal error
    $lunch = new Cheddar; //works!

    ?>

  4. #4
    Member
    Join Date
    Jan 2018
    Posts
    78
    Feedback Score
    0
    My answer is for php:
    You can force a class to be strictly an inheritable class by using the "abstract" keyword. When you define a class with abstract, any attempt to instantiate a separate instance of it will result in a fatal error. This is useful for situations like a base class where it would be inherited by multiple child classes yet you want to restrict the ability to instantiate it by itself.

    Example........

    <?php

    abstract class Cheese
    {
    //can ONLY be inherited by another class
    }

    class Cheddar extends Cheese
    {
    }

    $dinner = new Cheese; //fatal error
    $lunch = new Cheddar; //works!

    ?>

  5. #5
    Senior Member
    Join Date
    Apr 2018
    Posts
    188
    Feedback Score
    0
    By using interface and abstract class , we can restrict the class from inheritance!

  6. #6
    Junior Member
    Join Date
    Aug 2018
    Location
    Emeryville, CA
    Posts
    14
    Feedback Score
    0

    Restrict Inheritance for a Class

    These are the two ways in which We can restrict inheritance for a class:


    1) Declaring class as final that why it cannot be extended

    2) If class is not declared as final, select its constructor as private and exposed method for creating instance of class.

 

 

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
  •