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

Posted at : Feb/26/2007
4857 Views

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...


ABOUT ME

Rully Yulian MF
Rully Yulian Muhammad Firmansyah | Co-Founder & IT Trainer at Native Enterprise | Microsoft Azure Data Scientist | IBM RAG & Agentic AI | IBM Data Science & Data Analyst | Python Certified (PCEP, PCAP) | MOS, MTA, Xamarin Certified, ex MCT | ex MVP

CERTIFICATIONS

Microsoft Certified Associate
IBM RAG and Agentic AI Professional
IBM Data Science Professional IBM Data Analyst Professional
PCAP Associate Python Programmer Certified PCEP Entry Level Python Programmer Certified
Xamarin Certified
MOS 2007
MCPD MCTS
MCAD.NET

NATIVE ENTERPRISE

Native Enterprise - IT Training

FOLLOW ME

Youtube  X Twitter Facebook  Instagram  LinkedIn

RSS


NATIVE ENTERPRISE NEWS

© Copyright 2006 - 2026   Rully Yulian MF   All rights reserved.