Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism and c#

Here one more basic question asked in MS interview recently

class A {
    public virtual void Method1(){}

    public void Method2() {
        Method1();
    }
}

class B:A {
    public override void Method1() { }
}

class main {
    A obk = new B();
    obk.Method2(); 
}

So which function gets called? Sorry for the typos.

like image 231
TalentTuner Avatar asked Mar 16 '10 15:03

TalentTuner


People also ask

Does C allow polymorphism?

In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. According to that definition, no, C doesn't natively support polymorphism.

Is the C language an OOP language?

C is a Procedural Oriented language. It does not support object-oriented programming (OOP) features such as polymorphism, encapsulation, and inheritance programming.

What is polymorphism and example?

The word “polymorphism” means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. A real-life example of polymorphism is a person who at the same time can have different characteristics.

How many types of polymorphism are there in C language?

There are two major types of polymorphisms in Object Oriented Programming (OOPS) languages. They are Static Binding (Compile time Polymorphism) and Dynamic Binding (Runtime Polymorphism). Method overriding would be the example of Dynamic Polymorphism and Method Overloading would be the example of Static Polymorphism.


1 Answers

B.Method1();

gets called because it properly overrides the virtual method A.Method1();

like image 99
Justin Niessner Avatar answered Oct 31 '22 13:10

Justin Niessner