none
Inherit From abstract class and without abstract class RRS feed

  • Question

  • Hello,

    What is differences between inherit property from abstract class and without abstract class?

    Thanks

    using System;
    
    namespace P96_12_11_P1
    {
        class A
        {
            public int MyProperty { get; set; }
        }
        abstract class AA
        {
            public int Abs_MyProperty { get; set; }
        }
        class B : A
        {
            public void TEST()
            {
                MyProperty = 10;
                Console.WriteLine(MyProperty);
            }
        }
        class AB : AA
        {
            public int MyProperty { get; set; }
            public void ATEST()
            {
                MyProperty = 20;
                Console.WriteLine(MyProperty);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
    
                
                new B().TEST();
    
                new AB().ATEST();
    
                Console.ReadKey();
            }
        }
    }
    

    Friday, March 2, 2018 9:17 AM

Answers

  • Hello Arash_89,

    For your code, it doesn't have difference between them. The only point is AA can't create instance because AA was modified by "abstract" keywords. Generally speaking, abstract classes have the following features:

    • An abstract class cannot be instantiated.
    • An abstract class may contain abstract methods and accessors.
    • It is not possible to modify an abstract class with the sealed modifier because the two modifers have opposite meanings. The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
    • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

     Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Arash_89 Monday, March 5, 2018 10:53 AM
    Monday, March 5, 2018 9:09 AM
    Moderator

All replies

  • Nothing, a class is just a way to create an object. Abstract has only the consequence that you cannot do it from that class direct. 

    Success
    Cor



    Friday, March 2, 2018 9:30 AM
  • Hello Arash_89,

    For your code, it doesn't have difference between them. The only point is AA can't create instance because AA was modified by "abstract" keywords. Generally speaking, abstract classes have the following features:

    • An abstract class cannot be instantiated.
    • An abstract class may contain abstract methods and accessors.
    • It is not possible to modify an abstract class with the sealed modifier because the two modifers have opposite meanings. The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
    • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

     Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Arash_89 Monday, March 5, 2018 10:53 AM
    Monday, March 5, 2018 9:09 AM
    Moderator