Ehsan Ghanbari

Experience, DotNet, Solutions

Liskov substitution principle with sample in C#

Liskov substitution principle (LSP) is a simple pattern in object-oriented programming, notice about definition If A is a subtype of B, then object of B could be replaced with an object of A, in another definition subclass can behave like base class. maybe you are using it for years, take a look at  the example:

 

    public class BaseClass
    {
        public string ProductName { get; set; }
        public virtual void Shipping()
        {
            //
        }

        public virtual void Order()
        {
            //
        }
     }

 

 and about the derived class called DerivedClass 

 

    public class DerivedClass :BaseClass
    {
        public string CustomerInfo { get; set; }
        public void DeliveryAddress()
        {
            //
        }
        public override void Shipping()
        {
            base.Shipping();
        }

        public override void Order()
        {
            base.Order();
        }
    }

 

It's clear that derived class override the virtual methods of the base class, and can be used In derived class objects, see the usage

 

    public class Present
    {
        public static void Main()
        {
            var baseClass = new DerivedClass();
            baseClass.ProductName = "Vibrator";
            baseClass.Shipping();
            baseClass.Order();
        }
    }

 



About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to query over Icollection<> of a type with linq
How to use PagedList In asp.net MVC
Domain driven design VS model driven architecture
Using Generic type for type casting in F#
What's the DDD-lite?