Object Oriented Programing (OOP) Inheritance and Polymorphism

By   Tewodros   Date Posted: Oct. 13, 2021  Hits: 772   Category:  ASP.NET C#   Total Comment: 0             A+ A-


side

Inheritance

One of the basic concepts of OOP is inheritance. Inheritance is a way of sharing a bunch of features that are defined at a super class level from a sub class level. Inheritance defines a child-parent relationship among classes. Child classes can inherit one or more properties and methods from parent class. This means any property or method that is defined in parent class can be reused in child class with out the need for redefining the same thing in the sub class. 

 

Abstract Class

Abstract is like a base/super/parent class with concrete properties and methods but we also have abstract methods in which we delegate the responsibilities of the class to child/sub classes as we don't have the details of the sub class information and it may vary depending on the sub class. 

For example we may have two concreate classes (Circle and Sphere) inheriting from Shapes parent abstract class.

The shape can provide the common pieces for the different shapes that can inherit from this class.

But the shape class has no idea how to compute the Area of shapes that inherit from it and therefore only provide the template for it and leave the implementation details to sub classes. We use the abstract keyword to indicate we are just putting the method signature and only subclasses can provide the implementation. 

public abstract class Shape     {

           public const double PI = Math.PI;

           protected double x, y;

           public Shape()  {

           }

           public Shape(double x, double y)   {

               this.x = x;

               this.y = y;

           }         

           public abstract double Area();        

       }

 

Now the definition of the Circle class is only to define the implementation of the Area class. Every thing else is automatically inherited from parent class Shape. We use the colon : after the name of the class to indicate Circle is a subclass of Shape and we use :base keyword to call parent constructor to initialize the properties of the sub class. We also use the keyword overrride to indicate that we are overriding the abstract method in the subclass.  

 public class Circle : Shape  {

           public Circle(double r) : base(r, 0)   {

           }          

           public override double Area()    {

               return PI * x * x;

           }

       }

Now, let's do the same for Sphere

class Sphere : Shape  {

           public Sphere(double r) : base(r, 0)  {

           }

           public override double Area()  {

               return 4 * PI * x * x;

           }

       }

As you can see, the only difference between the subclasses Circle and Sphere is how they implement the Area() method as we use different formulas to calculate the area of a Circle from that of a Sphere. 

 

Polymorphism 

Polymorphism is dynamically binding methods of a class as if they are present in the super class by calling the appropriate methods of the subclass at run time. 

To understand this let us see how this is done using the following example. 

static void Main()   {

           double r = 3.0, h = 5.0;

           Shape c = new Circle(r);

           Shape s = new Sphere(r);           

                           

           Console.WriteLine("Area of Circle   = " + c.Area());

           Console.WriteLine("Area of Sphere   = "  + s.Area());       

       }

Output:

  • Area of Circle   = 28.27
  • Area of Sphere   = 113.10

       Shape c = new Circle(r);

       c.Area()

How in the whole world does Shape knows how to calculate the area of a circle c. After all we don't see the formula in the Shape class, it is empty. 

Well, that is the magic of Polymorphism. 

Shape c = new Circle(r);

Polymorphism resolves which area we are calculating based on the object type we attached to the shape as shown in bold. 

If c is of type Circle, then I we are calculating the area of the Circle. 

Note. We can always assign a subclass to a super class but not vice versa. 

Therefore,

Shape c = new Circle(r); and  Circle c = new Circle(r);   is a correct way of instantiating a class.

However, we cannot assign a super class object to a subclass.

Circle c = new Shape(r, h);  //Compile Error.

We should never instantiate an abstract class since it doesn't stand by itself due to its abstract methods. 

 Shape s2 = new Shape(r, h);//Compile Error.

 


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* gifjea

Back to Top