Inheritance in C#

One of the most important concepts in object-oriented programming is the heritage. In inheritance, another category is allowed to define a class, which makes it easier to create and maintain an application. This provides the opportunity to reuse the functionality of code and to speed up implementation time.

When creating a category, instead of writing completely new data members and member tasks, the programmer can specify that the new class should have members of an existing class. This current class is called the base class, and the new class is referred to as the derived class.

Base and Derived Classes

A class can be obtained from more than one category or interface, which means that it can obtain data and functions from several base classes or interfaces.
The syntax used in C# for creating derived classes is as follows −
<acess-specifier> class <base_class> {
   ...
}

class <derived_class> : <base_class> {
   ...
}

using System;

namespace InheritanceApplication {
   
   class Shape {
      
      public void setWidth(int w) {
         width = w;
      }
      
      public void setHeight(int h) {
         height = h;
      }
      protected int width;
      protected int height;
   }

   // Derived class
   class Rectangle: Shape {
      
      public int getArea() { 
         return (width * height); 
      }
   }
   
   class RectangleTester {
   
      static void Main(string[] args) {
         Rectangle Rect = new Rectangle();

         Rect.setWidth(5);
         Rect.setHeight(7);

         // Print the area of the object.
         Console.WriteLine("Total area: {0}",  Rect.getArea());
         Console.ReadKey();
      }
   }
}

Multiple Inheritance in C#

C # does not support multiple heritage However, you can use the interface to implement multiple heritage.

using System;

namespace InheritanceApplication {
   
   class Shape {
      
      public void setWidth(int w) {
         width = w;
      }
      
      public void setHeight(int h) {
         height = h;
      }
      protected int width;
      protected int height;
   }

   // Base class PaintCost
   public interface PaintCost {
      int getCost(int area);
   }
   
   // Derived class
   class Rectangle : Shape, PaintCost {
      
      public int getArea() {
         return (width * height);
      }
      
      public int getCost(int area) {
         return area * 70;
      }
   }
   
   class RectangleTester {
      
      static void Main(string[] args) {
         Rectangle Rect = new Rectangle();
         int area;
         Rect.setWidth(5);
         Rect.setHeight(7);
         area = Rect.getArea();
         
         // Print the area of the object.
         Console.WriteLine("Total area: {0}",  Rect.getArea());
         Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}


Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment