Results 1 to 6 of 6

Thread: Big confusion with virtual/overrides..I dont get it..

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    Big confusion with virtual/overrides..I dont get it..

    why should i set a function virtual and then use override to replace it if i can simply make:

    Code:
    //base class
    public void LOL() {
    }
    
    //derived class
    public new void LOL() {
    }
    \m/\m/

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Good point.
    You wouldn't need to as long as the method signatures were the same.
    Example:
    Code:
    // C# Document
    using System;
    
    class One 
    { 
    	public void A() 
    	{ 
    		Console.WriteLine("One.A() being called"); 
    	} 
    } 
    
    class Test : One
    {
    	public static void Main()
    	{
    		Test a = new Test();
    		
    		a.A();
    	}
    	
    	public new void A()
    	{
    		Console.WriteLine("Test.A() being called");
    	}
    }
    Displays:
    Test.A() being called

    But if you comment out the A() in class test, then it displays.
    One.A() being called

    My understanding with the virtual/overrides issue is the functionality is there to help deal with compatibility issues between different versions of classes.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This is called Shadowing in VB.NET (hiding in C#) . If you want to obscure a base class method and provide new implementation with the same name but different access level or different return type .
    It's different from virtual and overriding in sense of overriden methods must have the same signature and same return types as the base class method .

  4. #4

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    If I want to hide a method in my derived class that is public in the original class can I do shadowing changing it to private so it doesn't appear in the derived class to the rest of the classes?

    edit: Just tried it and it doesnt work as I thought..it would be cool if it worked
    \m/\m/

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I don't think it would be much helpful . I mean this would make sense if you're working on another developer's class and you want to provide the same names for the methods but with different return type or modifiers .

  6. #6
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502
    Code:
    class One 
    { 
    	public void A(int one, int two, int three) 
    	{ 
    		Console.WriteLine("One.A() being called: {0}", one); 
    		Console.WriteLine("One.A() being called: {0}", two); 
    		Console.WriteLine("One.A() being called: {0}", three); 
    	} 
    } 
    
    class Test : One
    {
    	public static void Main()
    	{
    		Test a = new Test();
    		
    		a.A();
    	}
    	
    	public void A(int one, int two, int three, int four):base(one,two,three)
    	{
    		Console.WriteLine("Test.A() being called: {0}", four);
    	}
    }
    Can't you use other signatures too, like here ? Again, why would we use virtual. (The only explanation I can find would be for Polymorphism when making an instance from a class that is inheriting from an inheritance of an interface. And declare the instance as a reference to the interface type. for example
    Code:
    	public interface IShape
    	{
    		string Name();
    	}
    
    	public class Square : IShape 
    	{
    		public virtual string Name()
    		{
    			return "Square";
    		}
    	}
    
    	public class Box : Square 
    	{
    		public override string Name()
    		{
    			return "Box";
    		}
    	}
    
    	class Head
    	{
    		static void Main(){
    			IShape[] Table1 = new IShape[4];
    			Table1[0]= new Square(10,2);
    			Table1[1]= new Box(8 , 5, '&');
    			Table1[2]= new Square(7,4);
    			Table1[3]= new Box(9,2, '#');
    			string output = "";
    			for (int i=0;i < Table1.Length;i++)
    			{
    				output += Table1[i].Name() + "\n";
    			}
    			Console.WriteLine(output);
    		}
    	}
    Without the virtual and override here, the output would 4 times use the .Name of the Square class here. Instead like I showed it here, the .Name of the Box class will be found if necessary. (You'll get Dynamic binding, instead of static binding if you use override. In all other situations Virtual seems to be unnecessary and just something people seem to use to avoid complications.)
    Last edited by BramVandenbon; Jun 1st, 2004 at 12:20 AM.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width