Home > C# and dot NET > Virtual Key Word C#

Virtual Key Word C#

December 14, 2014 Leave a comment Go to comments

Virtual key word digunakan untuk memodifikasi metode, properti, pengindeks, atau event deklarasi dan memungkinkan untuk di override di kelas turunan / derived class. Secara default, method adalah nonvirtual, jadi kita tidak bisa men-override method tersebut.

Example.

Animal.cs ( Animal Class )

class Animal
{
public virtual void Greet()
{
Console.WriteLine("Hello I'm an Animal");
}
}

Cat.cs ( Cat Class )

class Cat : Animal
{
public override void Greet()
{
Console.WriteLine("Hello, I'm a Cat");
}
}


Program.cs ( Main class )

class Program
{
static void Main(string[] args)
{
Animal animal = new Animal();
animal.Greet();

Cat cat = new Cat();
cat.Greet();
}
}

Capture screen.

Virtual Key Word

Virtual Key Word

Semoga bermanfaat.šŸ™‚

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: