Inheritance in C#

Posted on

Inheritance in C#

A stand-out amongst the most essential ideas in article arranged writing computer programs is legacy. Legacy permits us to characterize a class as far as another class, which makes it less demanding to make and keep up an application. This likewise gives a chance to reuse the code usefulness and speeds up execution time. While making a class, rather than composing totally new information individuals and part works, the software engineer can assign that the new class ought to acquire the individuals from a current class. This current class is known as the base class, and the new class is alluded to as the determined class. The thought of legacy actualizes the IS-A relationship. For instance, warm blooded animal IS A creature, pooch IS-A vertebrate consequently canine IS-A creature too, et cetera. The following mainstay of OOP, legacy, comes down to the dialect’s capacity to permit you to fabricate new class definitions taking into account existing class definitions. Basically, legacy permits you to broaden the conduct of a base (or parent) class by acquiring center usefulness into the determined subclass (additionally called a child class).

There is another type of code reuse in the realm of OOP: the regulation/assignment demonstrate too known as the “has-a” relationship or conglomeration. This type of reuse is not used to build up guardian/kid connections. Maybe, the “has-a” relationship permits one class to characterize a part variable of another class and uncover its usefulness (if required) to the article client in a roundabout way.

Particularly talking, code reuse comes in two flavors: legacy (the “is-a” relationship) and the control/designation demonstrate (the “has-a” relationship). We should start this part by looking at the traditional “is-a” legacy model. When you set up “is-a” connections between classes, you are building a reliance between two or more class sorts. The fundamental thought behind established legacy is that new classes can be made utilizing existing classes as a beginning stage.

Base and Derived Classes

A class can be gotten from more than one class or interface, which implies that it can acquire information and capacities from different base classes or interfaces.

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

}

}

}

Initializing Base Class

The determined class acquires the base class part variables and part systems. In this manner the super class item ought to be made before the subclass is made. You can give directions for superclass instatement in the part introduction list.

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

}

}

}

Multiple Inheritance in C#

C# does not bolster different legacy. Be that as it may, you can utilize interfaces to actualize different legacy.

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

}

}

}

Regarding Multiple Base Classes

Talking about base classes, it is vital to remember that C# requests that a given class have precisely one direct base class. It is impractical to make a class sort that straightforwardly gets from two or more base classes (this method [which is bolstered in unmanaged C++] is known as various legacy, or basically MI).

On the off chance that you endeavored to make a class that determines two direct parent classes as appeared in the taking after code, you will get compiler mistakes.

/Illegal! The .NET stage does not permit /numerous legacy for classes!

class WontWork : BaseClassOne, BaseClassTwo

{}

The .NET stage allows a given class, or structure, to execute any number of discrete interfaces. Along these lines, a C# sort can show various practices while keeping away from the complexities connected with MI. On a related note, while a class can have one and only direct base class, it is allowable for an interface to specifically get from different interfaces.

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s