Latest web development tutorials

C # Inheritance

Inheritance is an object-oriented programming is one of the most important concepts. Inheritance allows us to define a class based on another class to define a class, which makes creating and maintaining applications much easier. But also conducive to reuse code and reduce development time.

When you create a class, programmers do not need to completely re-write new data members and member functions, you only need to design a new class that inherits the existing members of the class can be. The existingbase class is called a class, the new class is called the derived class.

Realization of the idea of inheritancebelonging (IS-A) relationship.For example, a mammalbelongs (IS-A) animals, dogs belonging to (IS-A)of mammals, and thereforebelong tothe dog(IS-A)animals.

Base and derived classes

A class can be derived from more than one class or interface, which means that it can inherit multiple base classes or interfaces from data and functions.

Create a derived class in C # syntax is as follows:

<Acess-specifier> class <base_class>
{
 ...
}
class <derived_class>: <base_class>
{
 ...
}

Suppose, there is a base class Shape, which is the derived class Rectangle:

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 area of ​​the 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

Initialize the base class

Derived class inherits member variables and member methods of the base class. Therefore, the parent object should be created before a sub-class object was created. You can initialize the parent class in the member initialization list.

The following program demonstrates this point:

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 ( "Size: {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

C # does not support multiple inheritance.However, you can use the interface to achieve multiple inheritance. The following program demonstrates this point:

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

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

Total area: 35
Paint Total cost: $ 2450