Polymorphism:what is the real type of the called method?

mshwf

Can anyone explain the behaviour of this code , as I'm playing with polymorphism I came up with this code by coincidence, and I'm really confused with its behaviour:

public class Book
{
    public virtual void Method() 
    {
        Console.WriteLine("Book/ I am here in "+ GetType().Name);
    } 
} 
class Chapter : Book
{
    public new void Method() 
    {
        Console.WriteLine("Chapter/ I am here in "+ GetType().Name);
    }

    static void Main() 
    {
        Book myBook = new Chapter() ;
        myBook.Method();
    } 
} 

The output from this program is

Book/ I am here in Chapter

This means that the instance called the method within the book class but gets the type chapter. Does GetType() method depend on the instance it called with, not the class it lies in?

RvdK

Yes, GetType() gets the actual type from the object (instance) at runtime.

If you changed public new void Method() to public override void Method() it would print out the other line.

See MSDN:

Return Value

Type: System.Type

The exact runtime type of the current instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to choice what method should be called when use polymorphism

From Dev

Method return type polymorphism

From Dev

PowerMockito - Spying - Real method called

From Dev

What ASM Visitor Method gets called for type annotation on catch

From Dev

Real method getting called when Mockito doNothing method is called

From Dev

What is this variable type called?

From Dev

Polymorphism and reference class member - wrong virtual method called

From Dev

Defined service method is not called, instead real service's is called

From Dev

What is this programming method called? And is it bad?

From Dev

What this JavaScript function method called?

From Dev

What are these method parameters called in Swift?

From Dev

Determine what method is called by a function

From Dev

What does "type domain" and "real type" mean?

From Dev

can we have different return type of method in c# polymorphism

From Dev

Get real type of generic method from StackFrame

From Dev

What is the REAL return type of DATEADD() in SQL?

From Dev

What is real use of return type of signal function

From Java

Mockito: verify that a method was not called with specific parameter type

From Dev

What happens after a method is called in Java

From Dev

Check what thread a method is being called from

From Dev

What is the relation called if ClassB::Method(); is in ClassA

From Dev

What happens when recursion is called twice in a method?

From Dev

What method is called right before the application exits?

From Dev

What is it called when a method has optional parameters?

From Dev

What method is called when UINavigationController is presented?

From Dev

What happens when recursion is called twice in a method?

From Dev

What's this collision-detection-method called?

From Dev

what the result if unused method called several times?

From Dev

Is there real static polymorphism in C++?

Related Related

  1. 1

    How to choice what method should be called when use polymorphism

  2. 2

    Method return type polymorphism

  3. 3

    PowerMockito - Spying - Real method called

  4. 4

    What ASM Visitor Method gets called for type annotation on catch

  5. 5

    Real method getting called when Mockito doNothing method is called

  6. 6

    What is this variable type called?

  7. 7

    Polymorphism and reference class member - wrong virtual method called

  8. 8

    Defined service method is not called, instead real service's is called

  9. 9

    What is this programming method called? And is it bad?

  10. 10

    What this JavaScript function method called?

  11. 11

    What are these method parameters called in Swift?

  12. 12

    Determine what method is called by a function

  13. 13

    What does "type domain" and "real type" mean?

  14. 14

    can we have different return type of method in c# polymorphism

  15. 15

    Get real type of generic method from StackFrame

  16. 16

    What is the REAL return type of DATEADD() in SQL?

  17. 17

    What is real use of return type of signal function

  18. 18

    Mockito: verify that a method was not called with specific parameter type

  19. 19

    What happens after a method is called in Java

  20. 20

    Check what thread a method is being called from

  21. 21

    What is the relation called if ClassB::Method(); is in ClassA

  22. 22

    What happens when recursion is called twice in a method?

  23. 23

    What method is called right before the application exits?

  24. 24

    What is it called when a method has optional parameters?

  25. 25

    What method is called when UINavigationController is presented?

  26. 26

    What happens when recursion is called twice in a method?

  27. 27

    What's this collision-detection-method called?

  28. 28

    what the result if unused method called several times?

  29. 29

    Is there real static polymorphism in C++?

HotTag

Archive