Part 21 - C# Tutorial - Inheritance in C#


Part 21 - C# Tutorial - Inheritance in C#


10 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
  5. Hi Venkat thanks for tutorials may I Know where we can write Console.Realine(); in this program so that we can hold the screen to look Pragim Tech thanks

    ReplyDelete
  6. using System;


    namespace Inheritance
    {
    public class Employee
    {
    public string Firstnm;
    public String Lastnm;

    public void fullname()
    {
    Console.WriteLine(Firstnm + " " + Lastnm);

    }
    }
    public class Fulltime:Employee
    {

    public int Yearlysal;


    }
    public class Parttime:Employee
    {
    public float Hourlysal;

    }

    class Program
    {
    static void Main()
    {
    Fulltime ft = new Fulltime();
    ft.Firstnm = "Manas";
    ft.Lastnm = "Sahu";
    ft.fullname();
    ft.Yearlysal = 800000;



    Parttime pt = new Parttime();
    pt.Firstnm = "Pinku";
    pt.Lastnm = "Sahu";
    pt.fullname();
    Console.ReadLine();



    }
    }
    }



    why not yearly salary printed?

    ReplyDelete
    Replies
    1. Because you have not wrote Console.WriteLine(ft.Yearlysal); to print the salary to console.

      Delete
  7. sir can you upload Encapsulation,Abstration in C# with example

    ReplyDelete

It would be great if you can help share these free resources