Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to.

Similar presentations


Presentation on theme: "C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to."— Presentation transcript:

1 C# - Inheritance Ashima Wadhwa

2 Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and speeds up implementation time.

3 When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on. Inheritance

4 Base and Derived Classes A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces. The syntax used in C# for creating derived classes is as follows: class {... } class : {...

5 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; }

6 // 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(); }

7 class Shape { public double Width; public double Height; public void ShowDim() { Console.WriteLine("Width and height are " + Width + " and " + Height); } class Triangle : Shape { public string Style; public double Area() { return Width * Height / 2; } public void ShowStyle() { Console.WriteLine("Triangle is " + Style); }

8 class Driver { static void Main() { Triangle t1 new Triangle(); Triangle t2 new Triangle(); t1.Width =4.0; t1.Height =4.0; t1.Style ="isosceles"; t2.Width =8.0; t2.Height =12.0; t2.Style ="right"; Console.WriteLine("Info for t1: "); t1.ShowStyle(); t1.ShowDim(); Console.WriteLine("Area is " + t1.Area()); Console.WriteLine(); Console.WriteLine("Info for t2: "); t2.ShowStyle(); t2.ShowDim(); Console.WriteLine("Area is " + t2.Area()); }

9 The output from this program is shown here: Info for t1: Triangle is isosceles Width and height are 4 and 4 Area is 8 Info for t2: Triangle is right Width and height are 8 and 12 Area is 48

10 Queries?


Download ppt "C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to."

Similar presentations


Ads by Google