Home > ASP.NET, C#, Interview Questions, Programming, Tips & Tricks > C# Inheritance Question

C# Inheritance Question

The following are two simple questions related to C# inheritance.
Please try to give an answer without executing the code, otherwise where is the fun part ?

    // Question #1
    class Program
    {
        static void Main(string[] args)
        {
            int i = 13;
            string Result = B.Get(i);
            Console.WriteLine(Result);
        }
    }

    public class A
    {
        public static string Get(int Value) {
            return "A";
        }
    }

    public class B : A
    {
        public static string Get(float Value)
        {
            return "B";
        }
    }
    // Question #2
    class Program
    {
        static void Main(string[] args)
        {
            int i = 13;
            string Result = B.Get(i);
            Console.WriteLine(Result);
        }
    }

    public class B
    {
        public static string Get(float Value)
        {
            return "B";
        }

        public static string Get(int Value) {
            return "A";
        }
    }

What will be the result of the above code parts ?

VN:F [1.9.22_1171]
Rating: 4.6/5 (5 votes cast)
C# Inheritance Question, 4.6 out of 5 based on 5 ratings
  1. Mina
    June 29th, 2009 at 14:41 | #1

    Both will result in “A” !?

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  2. Mina
    June 29th, 2009 at 15:03 | #2

    The first will result in a “B” and the second in an “A”

    That’s because you can pass an int into a float variable but not vice versa. :)

    VA:F [1.9.22_1171]
    Rating: 3.0/5 (2 votes cast)
    • June 29th, 2009 at 20:32 | #3

      You are correct, but you can do the same thing in the B class – you have an overloaded Get in both cases.
      This question is actually about priorities of inherited overloaded methods.

      VN:F [1.9.22_1171]
      Rating: 5.0/5 (1 vote cast)
  3. July 13th, 2009 at 14:25 | #4

    First “B” Second “A”

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (2 votes cast)
  4. Kaveh Shahbazian
    July 13th, 2009 at 18:46 | #5

    I think it is more about method overloading than inheritance.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  5. July 13th, 2009 at 19:11 | #6

    @Kaveh Shahbazian
    I have to disagree with you.

    Overloading is something very obvious (in most cases) and as you can see it works exactly as expected in Question #2.

    Overloading has it’s own priorities, for example <T> priority is higher than “object”.

    My example is about how inheritance affects the “obvious” overloading.

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  6. Bob Saggett
    July 13th, 2009 at 19:18 | #7

    What’s this got to do with inheritance? You are using static methods which are not included in inheritance relationships in C# anyway. This is about overloading.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • July 13th, 2009 at 19:38 | #8

      Bob, static methods are just like instance methods ( except you can’t make it virtual) and i used them to avoid instance creation.
      You can easily remove the “static” modifier and create an instance of “B” before calling the “Get” method, and you will get the same result.

      What’s this got to do with inheritance ? Well, this is exactly what i am trying to show here. First level methods are prioritized over any inherited method in case of parameters suitability.

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
  7. Al Tenhundfeld
    July 13th, 2009 at 23:17 | #9

    In general, you should avoid creating more abstract/”bigger” overloads in a derived class. In your derived classes, i.e., smaller/more specific classes, your method overloads should be smaller/more specific as well.

    (I might be misusing the terms bigger and smaller, which are really about covariance and contravariance, but that’s how I think about it. By bigger, I don’t just mean supertype.)

    If A has Get(string value), think carefully before you create an overload in B Get(object value). Also, if A has Get(string[] values), you should be cautious creating overload Get(object[] values) in B.

    Of course there are exceptions to this rule, but I think it helps avoid this type of confusion.

    And I get your point in this context, but there’s more difference with static methods than simply not being able to make them virtual.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  8. July 14th, 2009 at 07:28 | #10

    You right, but this question is not about Good design or Bad design – it’s about how well can you read and understand the code.

    Most of the tricky questions can’t be categorized as a Good Design.

    For example, are you familiar with the ‘@’ prefix ?

    int @int = 1;
    

    The above line will compile and for my opinion it’s a terrible thing to use in your code, but despite all of that a developer must know about it’s existence and what it suppose to do.

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  9. July 15th, 2009 at 20:01 | #11

    It’s a interesting question,
    I guess it was caused by the predefined implicit conversion of int in the inheritance.
    http://msdn.microsoft.com/en-us/library/5kzh1b5w%28VS.80%29.aspx

    So, if change the float to long,double or decimal,
    it sure be has the same problem.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  10. Dinesh
    January 14th, 2011 at 06:45 | #12

    Ans of First Question is ‘B’ Because in inheritance Always First Hit the drive Class Method then Base Class if Both Drive And Base Class Have Same Method name then it first Call The Drive Class Method and Hide The Base Class Method if Want To Call Base Class Method Then You Have Use base KeyWord With Method.

    Ans Of Second Question is A in this Type of Datatype you have Pass in method is called First

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  11. June 6th, 2011 at 08:02 | #13

    Nice Question.
    I have done it.
    Bvenkat Jeewesh

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  1. No trackbacks yet.

Subscribe without commenting

SEO Powered by Platinum SEO from Techblissonline
watch free movies