.

Inheritance in C sharp Assignment Help

INHERITANCE

  • The process of deriving a new classNamefrom an existing classNameis called inheritance
  • It is one of the important concepts of object oriented programming.
  • It is the vital concept of code reusability
  • To avoid the repetition of code , inheritance can be used
  • The already existing classNameis called the base className, and the new classNamewhich is derived from the base classNameis referred to as the derived className.

Inheritance in C sharp Assignment Help By Online Tutoring and Guided Sessions from assignmenthippo.com


  • The inheritance implements the ‘is a’ relationship.

e.g.

  • Four wheeler is a vehicle, car is a four wheeler therefore car is a vehicle as well a four wheeler

Basic Concept of Inheritance: Base and Derived Classes

  • To inherit the existing properties of pre existing className, a classNamecan be derived from

one or more base classNameor interface

  • Inheritance inherits member variables and members from multiple base classes or interfaces.

Syntax

acess-specifier classNamebase_class

{

...

}classNamederived_className: base_class

{

...

}

Example

Here a base classNameShape and its derived classNameRectangle is used as an inheritance example

using System;

namespace Inheritance

{

classNameShape // Base className

{

public void setWidth(int w)

{

width = w;

}

public void setHeight(int h)

{

height = h;

}

protected int width;

protected int height;

}

classNameRectangle: Shape // Derived className

{

public int getArea()

{

return (width * height);

}

}

classNameRectangleTest

{

static void Main(string[] args)

{

Rectangle R = new Rectangle();

R.setWidth(5);

R.setHeight(7);

Console.WriteLine("Total area: {0}", Rect.getArea());

Console.ReadKey();

}

}

}

Output: Total area: 35

Types of Inheritance in C#

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Note: Multiple Inheritances is not supported in c# but it can be achieved through an Interface

Example

The following example demonstrates the usage of interface in implementing the multiple inheritance in c#

using System;

namespace Inheritance

{

classNameShape

{

public void setW (int w)

{

width = w;

}

public void setH (int h)

{

height = h;

}

protected int width;

protected int height;

}

public interface Paint // Base classNamePaintCost

{

int getCost(int area);

}

classNameRectangle : Shape, Paint // Derived className

{

public int getArea()

{

return (width * height);

}

public int getCost(int area)

{

return area * 70;

}

}

classNameRectangleTester

{

static void Main(string[] args)

{

Rectangle Rect = new Rectangle();

int area;

Rect.setW(5);

Rect.setH (10);

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();

}

}

}

Output: Total area: 50

Total paint cost: $3500

.