C # inheritance


Release date:2023-08-31 Update date:2023-10-13 Editor:admin View counts:341

Label:

C # inheritance

Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define one class based on another, which makes it easier to create and maintain applications. It also helps to reuse code and save development time.

When creating a class, programmers do not need to completely rewrite new data members and member functions, they just need to design a new class thatinherits the members of the existing class. This existing class is called the base class, and this new class is called the derived class.

The idea of inheritance implements the IS-A relationship. For example, mammals belong to (IS-A) animals, dogs belong to (IS-A) mammals, so dogs belong to (IS-A) animals.

Base and derived classes

A class can be derived from multiple classes or interfaces, which means thatit can inherit data and functions from multiple base classes or interfaces.

The syntax for creating a derived class in C # is as follows:

<Access modifier> class <Base class>
{
 ...
}
class <Derived class> : <Base class>
{
 ...
}

Suppose there is a base class Shape derived class is Rectangle :

Example

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 an object
         Console.WriteLine("total area: {0}",  Rect.getArea());
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

total area: 35

Initialization of base class

The derived class inherits the member variables and member methods of the base class. Therefore, the parent object should be created before the subclass object is created. You can initialize the parent class in the member initialization list.

The following program demonstrates this:

Example

using System;
namespace RectangleApplication
{
   class Rectangle
   {
      // Member variables
      protected double length;
      protected double width;
      public Rectangle(double l, double w)
      {
         length = l;
         width = w;
      }
      public double GetArea()
      {
         return length * width;
      }
      public void Display()
      {
         Console.WriteLine("length: {0}", length);
         Console.WriteLine("width: {0}", width);
         Console.WriteLine("area: {0}", GetArea());
      }
   }//end class Rectangle
   class Tabletop : Rectangle
   {
      private double cost;
      public Tabletop(double l, double w) : base(l, w)
      { }
      public double GetCost()
      {
         double cost;
         cost = GetArea() * 70;
         return cost;
      }
      public void Display()
      {
         base.Display();
         Console.WriteLine("cost: {0}", GetCost());
      }
   }
   class ExecuteRectangle
   {
      static void Main(string[] args)
      {
         Tabletop t = new Tabletop(4.5, 7.5);
         t.Display();
         Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5

C # multiple inheritance

Multiple inheritance refers to the function that a category can inherit behaviors and features from more than one parent class at the same time. In contrast to single inheritance, single inheritance means that a class can inherit from only one parent class.

C# does not support multiple inheritance. However, you can use interfaces toimplement multiple inheritance. The following program demonstrates this:

Example

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 an object
         Console.WriteLine("total area: {0}",  Rect.getArea());
         Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Total area: 35
Total Paint Cost: $2450

Powered by TorCMS (https://github.com/bukun/TorCMS).