Skip to content

POLYMORPHISM Part-3

Implementing Dynamic Polymorphism using Abstract:

Abstraction in c# is the process of hiding the internal details and showing only the functionality. The abstract modifier indicates incomplete implementation.

Abstract Methods: A abstract method has no “body and declared inside the abstract class only.

Abstract Class: An abstract class can never be instantiated directly. This class must have at least one abstract method.

Some important points

  1. An Abstract class is intended to be used as base classes.
  2. All abstract methods must be overridden in the derived class.
  3. An abstract class can’t be inherited by structures.
  4. It can have constructors, destructors, properties and also non-abstract methods.
using System;

namespace AbstractUnderstanding
{
    public abstract class Shape
    {
        public abstract double Area();
    }

    public class Circle: Shape
    {
        public double Radius { get; set; }

        public Circle()
        {
            Console.WriteLine("Circle Area");
            Console.WriteLine("Please enter the radius");
            Radius = Convert.ToDouble(Console.ReadLine());
        }

        public override double Area() 
        {
            return (3.14) * Math.Pow(Radius,2);
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            Circle c = new Circle();
            Console.WriteLine("Circle {0}", c.Area());

            Shape ns = new Circle();
            Console.WriteLine("Circle {0}", ns.Area());

            Console.ReadKey();
        }
    }
}
Published inC#

2 Comments

  1. SMS SMS

    Very good article! We are linking to this particularly great content on our site. Keep up the great writing.

  2. Great info. Lucky me I recently found your blog by accident (stumbleupon). I’ve bookmarked it for later!

Leave a Reply

Your email address will not be published. Required fields are marked *