Friday, February 14, 2020

Abstract Class

I  have implemented most of my projects abstract class as a base class and all derived classes must implement abstract definition. we can avoid duplicate code.
·         creating something that provides common functionality to unrelated classes, use an interface
·         Abstract classes allow to provide default functionality for the subclasses.
·         creating something for objects that are closely related in a hierarchy, use an abstract class
If the base class will be changing often and an interface was used instead of an abstract class, we are going to run into problems. Once an interface is changed, any class that implements that will be broken. Now if it’s just you working on the project, that’s no big deal. However, once your interface is published to the client, that interface needs to be locked down. At that point, you will be breaking the clients code.


abstract class TestClass {
 
    protected int myNumber;  
    public abstract int numbers
    {
        get;
        set;
    }
}
 
class absDerived : TestClass {
 
    public override int numbers
    {
        get
        {
            return myNumber;
        }
        set
        {
            myNumber = value;
        }
    }
}
 
class order{
 
    // Main Method
    public static void Main)
    {
        TestDerived d = new TestDerived();
        d.numbers = 7;
        Console.WriteLine(d.numbers);
    }
}

No comments:

Post a Comment