Override method with "new" keyword and "virtual" keyword...what's the difference??

Created at : Feb/26/2007  
829 Views   0 Comments

Ketika sebuah subclass yang inherits from the base class nya menggunakan method dengan nama method yang sama dengan method yang ada di base class maka subclass tsb sudah melakukan overriding atau shadowing...tapi implementasi keyword "new" dan "virtual" pada sebuah method akan memberikan hasil yang berbeda...Let us consider the following code :

Misal sebuah class yang dijadikan sebagai base class dideklarasikan spt ini :

   1:  class TheBaseClass
   2:  {
   3:      public void TheMethod()
   4:      {
   5:          System.Windows.Forms.MessageBox.Show(
   6:              "From the Base Class Method");
   7:      }
   8:  }

kemudian subclassnya :

   1:  class TheDerivedClass : TheBaseClass
   2:  {
   3:      public new void TheMethod()
   4:      {
   5:          System.Windows.Forms.MessageBox.Show(
   6:              "From the Derived Class Method");
   7:      }
   8:  }

assume the eventhandler click from the button on the form like this :

   1:  private void button1_Click(object sender, EventArgs e)
   2:  {
   3:      TheDerivedClass clsDerived = new TheDerivedClass();
   4:      clsDerived.TheMethod();
   5:   
   6:      TheBaseClass clsBaseClass = clsDerived;
   7:      clsBaseClass.TheMethod();
   8:  }

hasil dari pemanggilan TheMethod dari object clsDerived akan menghasilkan :

"From the Derived Class Method"

hasil dari pemanggilan TheMethod dari object clsBaseClass akan menghasilkan :

"From the Base Class Method"

then modify TheBaseClass :

   1:  class TheBaseClass
   2:  {
   3:      public virtual void TheMethod()
   4:      {
   5:          System.Windows.Forms.MessageBox.Show(
   6:              "From the Base Class Method");
   7:      }
   8:  }

modify the TheDerivedClass :

   1:  class TheDerivedClass : TheBaseClass
   2:  {
   3:      public override void TheMethod()
   4:      {
   5:          System.Windows.Forms.MessageBox.Show(
   6:              "From the Derived Class Method");
   7:      }
   8:  }

run again the program, the result is:

hasil dari pemanggilan TheMethod dari object clsDerived akan menghasilkan :

"From the Derived Class Method"

hasil dari pemanggilan TheMethod dari object clsBaseClass akan menghasilkan :

"From the Derived Class Method"

Jadi kalau kita menggunakan "new" keyword sebagai implementasi overriding (sebenarnya klo begini bukan overriding ya?? overriding kan merubah implementasi functionality yang terdapat di dalam base class. Mmh...ini mirip keyword "MyClass" dan "Me" di VB.NET...) maka method yang akan dipanggil yaitu method yang terdapat di dalam base class nya, jd tidak tergantung dari Downcasting type nya, akan tetapi kalau implementasi overriding menggunakan "virtual" maka pemanggilan method tersebut bergantung pada Downcasting type. Pada contoh di atas terlihat bahwa object TheBaseClass di Downcasting ke TheDerivedClass...


[Comments]

[Write your comment]
Name (required)
URL (optional)
Example : http://www.yulianmf.com  
Comment

irtu
Input code above below (Case Sensitif) :
About Me 
Rully Yulian MF
My Name is Rully Yulian Muhammad Firmansyah. I am an IT Trainer, IT Consultant and Application Developer spesializing in Microsoft .NET technology and SQL Server database. I live in Bandung, Indonesia. My hobby is to play Guitar. [Read More...]
Top Download 
Bagaimana caranya menginstal database ketika deploying sebuah aplikasi? : Downloaded 2231 times  
Change Group,Sort Order, Filtering By Date in Crystal Reports : Downloaded 2192 times  
Simple Voice Engine Application With Sound Player Class... : Downloaded 1522 times  
DataGridView Grouping : Downloaded 1243 times  
WinForms DataGrid Paging With SqlDataAdapter : Downloaded 1232 times  
Article Category 
Links 
Award 
Certifications 
MOS 2007
MCAS
MCT
MCPD
MCTS
MCAD.NET
ASP.NET Brainbench
Native Enterprise - IT Training 
Native Enterprise Facebook Group 
My Facebook 
My LinkedIn 
Syndication (RSS 2.0) 
Powered By 
Native Enterprise News