Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 21 - C# Tutorial - Inheritance in C#


Part 21 - C# Tutorial - Inheritance in C#


6 comments:

  1. Can you give an example on multi level inheritance

    ReplyDelete
    Replies
    1. ClassB inherits from ClassA, and ClassC inherits from ClassB, so here ClassC will have the features inherited from ClassB and A. This is an example for multi-level class inheritance.

      Delete
  2. namespace Circle
    {
    public abstract class GeometricObject
    {
    private string colour;
    protected string name;
    public double radius;

    public string Colour //propeties
    {
    get { return colour; }
    set { colour = value; }
    }

    GeometricObject (string newColour)
    {
    colour = newColour;
    }

    public abstract double CalculateArea();
    public abstract double CalculatePerimeter();


    public new class Circle : GeometricObject
    {

    public new double CalculateArea()
    {
    return 3.14 * radius * radius;
    }
    public new double CalculatePerimeter()
    {
    return 3.14 * radius;
    }
    }
    public class Square : GeometricObject
    {

    public new double CalculateArea()
    {
    return 3.14 * radius * radius;
    }
    public new double CalculatePerimeter()
    {
    return 3.14 * radius;
    }
    }
    class MyTest
    {
    static void Main(string[] args)
    {
    Circle myCircle = new Circle();
    Square mySquare = new Square();


    {
    myCircle.CalculateArea();
    Console.Write("Area is :");
    mySquare.CalculateArea();
    Console.Write("Area is :");
    myCircle.CalculatePerimeter();
    Console.Write("Perimeter is a : ");
    mySquare.CalculatePerimeter();
    Console.Write("Perimeter is a : ");
    Console.ReadLine();
    }
    Console.ReadLine();
    }
    }
    }
    }

    sir can you fix this because i don't know how to do it

    ReplyDelete
    Replies
    1. You did not mention the error. Probably, the error is that you are defining the class Circle and Square within GeometricObject.

      The formula for perimeter is 2*PI*R.

      Delete
  3. Dear Vankat
    I have confusion about “INTERFACES”
    As we have the classes where we can declare and define methods why we use the interfaces where just declaration not the implementation and also again we rewrite the declaration in classes of that method which are declare in interface. In this case the interfaces are useless please help to remove this confusion I also watch the video of interfaces in C# I get the basic idea from there but still this question in my mind please help to remove this confusion
    I shall be very thankful to you!!!

    ReplyDelete
  4. Interface basically used to force user for defining all the methods declared in the interface. for example if you want all of the object(classes) should have a "get" and "Save" method to fetch and save data, you can create an interface with declaring both the methods and implement the interface in your classes.

    Thanks

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.