Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

Based on the classes below, what's the difference between
C#
ClassBase ObjectBase = new ClassDerived();
ObjectBase.Show();  // Output --- This is Derived Class.

and
C#
ClassDerived ObjectDerived = new ClassDerived();
ObjectDerived.Show();  // Output --- This is Derived Class.


C#
public class ClassBase
   {
     public virtual void Show()
        {
            MessageBox.Show("This is Base Class");
        }
    }

public class ClassDerived : ClassBase
   {
     public override sealed void Show()
        {
           MessageBox.Show("This is Derived Class");
        }
   }
Posted
Updated 18-Jun-13 9:22am
v6

Nothing from the standpoint of functionality since both your classes have the same methods. The difference would be if the derived class had methods that the base class did not, you would not be able to access them in the first case without casting it.

C#
((DerivedClass)ObjectBase).SomeDerivedMethod()
 
Share this answer
 
v2
Comments
FTWO 18-Jun-13 9:30am    
I have the output in comments that show that I was able to access the derived method
in first example

ClassBase ObjectBase = new Derived();
ObjectBase.Show(); // Output --- This is Derived Class.
Sergey Alexandrovich Kryukov 18-Jun-13 9:57am    
My 5, but OP is not getting anything. Also, this does not bring to understanding of the ideas of OOP and should not be used as a practical programming advice. For us, it should be obvious, but OP would need a note.
—SA
FTWO 18-Jun-13 10:42am    
Thanks Sergey - Are you saying that first case may work, but should use second case ?
Sergey Alexandrovich Kryukov 18-Jun-13 11:59am    
Which case? It's not about working, your sample makes no practical sense anyway. You see, you should not consider cases, you should understand the principles. Do you know how virtual method table works? Why do you need late binding, what's the use?
—SA
FTWO 18-Jun-13 12:17pm    
It's a beginners question I suppose. "The use" is to help me understand it.
It's not code that is being used anywhere, it's an example I'm giving so I can understand the principles.
I saw the code here http://www.codeproject.com/Articles/602141/Polymorphism-in-NET
and was surprised that the class was being instantiated as
ClassBase ObjectBase = new Derived() ---I'm calling this case 1
and not
ClassDerived ObjectDerived = new Derived() -- I'm calling this case 2

So I'm asking the question why, so that I can learn from the answer - maybe that's the use.


// If you want to know the difference run following snippet

C#
using System;

namespace ConsoleApplication1
{
    public class ClassBase
    {
        public virtual void Show()
        {
            Console.WriteLine("This is Base Class");
        }
    }

    public class ClassDerived : ClassBase
    {
        public override sealed void Show()
        {
            Console.WriteLine("This is Derived Class");
        }
    }
    class Program
    {
        // If you want to know the difference run following snippet
        static void Main()
        {
            ClassBase baseClass = new ClassBase();
            ClassBase derviedClass = new ClassDerived();//Note that i am using base class reference in derived class as well.

            Console.WriteLine("Case1: Child class is overriding the base class method.");
            //Case 1: child class is overriding virtual method from base class. 
            Console.WriteLine("Accessing base class object:");
            baseClass.Show();//"This is Base Class"
            Console.WriteLine("Accessing derived class object:");
            derviedClass.Show(); //"This is Derived Class"


            //Case2: Child class is not overriding base class method. Remove 'override' and 'sealed' qualifiers from 'Show' method of ClassDerived, then run this part.
            Console.WriteLine("Case2: Child class is not overriding the base class method.");
            Console.WriteLine("Accessing base class object:");
            baseClass.Show();//"This is Base Class"
            Console.WriteLine("Accessing derived class object:");
            derviedClass.Show(); //"This is Base Class"

            //In case2 if you want call show method from child class when you are using a reference of base class to child class object then you have to
            //do something like shown below.         
            Console.WriteLine("Accessing child class object (child class has not overridden base class method.)");
            ((ClassDerived)derviedClass).Show(); //"This is Derived Class"            

            Console.WriteLine("Accessing base and child class objects with 'new' qualifier used in child class method to hide base class method.:");
            //If you want to check real difference then one can use new operator.
            //Use 'new' instead of 'override' in above example and remove 'sealed' keyword.
            // Now if you run same two lines output is as follows
            Console.WriteLine("Accessing base class object:");
            baseClass.Show();//"This is Base Class"
            Console.WriteLine("Accessing child class object class object:");
            derviedClass.Show(); //"This is Base Class"

            Console.WriteLine("Accessing the child class object using typecasting to base and child class (when child class has not overridden the base class method)");
            //Also if you do something like this (assuming that child class is not overriding the method)
            ((ClassBase)new ClassDerived()).Show(); //"This is Base Class"
            //But if you have overriden method in child class then output would be as follows.
            ((ClassDerived)new ClassDerived()).Show(); //"This is Derived Class" 

            //To summarise we have following.
            //1. When a virtual method which is (not overriden by child class) but just implemented using child class with or without new keyword following scenarios happen
            //     i. If you are referring child class object using base class reference then base class' method will be invoked. eg. output - This is Base Class
            //     ii.If you are referring child class object using child class reference then child class method will be invoked.  eg. output - This is Derived Class
            //     iii. You are accessing child class object using child class reference but typecasting it to base class reference then it will invoke base class' method. eg. output - This is Base Class
            //2. When a virtual method is overriden in child class then in all the cases output is 
            //'This is Derived Class'. So when you override a method in child class then no matter how you try to access it in child class object(using base class reference or child class reference) only child class' method will be invoked and not from the base class.It is bit long explaination but i hope this helps in understanding runtime polymorphism through inhertance.
        }




        //I hope this clears the confusion...
    }
}
 
Share this answer
 
v3
Comments
[no name] 18-Jun-13 16:33pm    
For your case 2 (the second one... You have 2 oddly so it is actually case 3), casting it is irrelevant. The object is in fact a "ClassDerived" and will then run the overridden Show class (as can be seen in "derivedClass.Show();

As Solution 1 points out there is no difference in how it was coded.
Sagar Sumant 18-Jun-13 23:20pm    
You are correct, it was typo,
it should have been
((ClassDerived)derviedClass).Show(); //"This is Derived Class"
assuming that derivedClass is base class reference.

[no name] 19-Jun-13 8:25am    
Well done. You added some good info as well. I have now given you a 5 :-)
FTWO 18-Jun-13 17:32pm    
Ah Yes - thank you Sagar, I think that clears it up


1. Without overwrite in DerivedClass show() method

if instantiated like this (using base class reference in derived class
BaseClass objBaseClass= new DervivedClass()

The base class method will be run for command objBaseClass.Show()

if instantiated like this
DerivedClass objBaseClass= new DervivedClass()

The derived class method will be run for command objBaseClass.Show()

2. With overwrite in DerivedClass show() method
The derived class method will run regardless

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900