Virtual Keyword
Virtual keyword is used for generating a virtual path for its derived classes on implementing method overriding. Virtual keyword is used within a set with override keyword. It is used as:
// Base Class
class A
{
public virtual void show()
{
Console.WriteLine("Hello: Base Class!");
Console.ReadLine();
}
}
Override Keyword
Override keyword is used in the derived class of the base class in order to override the base class method. Override keyword is used with virtual keyword, as:
// Base Class
class A
{
public virtual void show()
{
Console.WriteLine("Hello: Base Class!");
Console.ReadLine();
}
}
// Derived Class
class B : A
{
public override void show()
{
Console.WriteLine("Hello: Derived Class!");
Console.ReadLine();
}
}
New Keyword
New keyword is also used in polymorphism concept, but in the case of method overloading So what does overloading means, in simple words we can say procedure of hiding your base class through your derived class.
It is implemented as:
class A
{
public void show()
{
Console.WriteLine("Hello: Base Class!");
Console.ReadLine();
}
}
class B : A
{
public new void show()
{
Console.WriteLine("Hello: Derived Class!");
Console.ReadLine();
}
}
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Generics
{
class A
{
public void show()
{
Console.WriteLine("Hello: Base Class!");
Console.ReadLine();
}
}
class B : A
{
public void show()
{
Console.WriteLine("Hello: Derived Class!");
Console.ReadLine();
}
}
class Polymorphism
{
public static void Main()
{
A a1 = new A();
a1.show();
B b1 = new B();
b1.show();
A a2 = new B();
a2.show();
}
}
}
Output :
Hello: Base Class!
Hello: Derived Class!
Hello: Base Class!
Virtual keyword is used for generating a virtual path for its derived classes on implementing method overriding. Virtual keyword is used within a set with override keyword. It is used as:
// Base Class
class A
{
public virtual void show()
{
Console.WriteLine("Hello: Base Class!");
Console.ReadLine();
}
}
Override Keyword
Override keyword is used in the derived class of the base class in order to override the base class method. Override keyword is used with virtual keyword, as:
// Base Class
class A
{
public virtual void show()
{
Console.WriteLine("Hello: Base Class!");
Console.ReadLine();
}
}
// Derived Class
class B : A
{
public override void show()
{
Console.WriteLine("Hello: Derived Class!");
Console.ReadLine();
}
}
New Keyword
New keyword is also used in polymorphism concept, but in the case of method overloading So what does overloading means, in simple words we can say procedure of hiding your base class through your derived class.
It is implemented as:
class A
{
public void show()
{
Console.WriteLine("Hello: Base Class!");
Console.ReadLine();
}
}
class B : A
{
public new void show()
{
Console.WriteLine("Hello: Derived Class!");
Console.ReadLine();
}
}
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Generics
{
class A
{
public void show()
{
Console.WriteLine("Hello: Base Class!");
Console.ReadLine();
}
}
class B : A
{
public void show()
{
Console.WriteLine("Hello: Derived Class!");
Console.ReadLine();
}
}
class Polymorphism
{
public static void Main()
{
A a1 = new A();
a1.show();
B b1 = new B();
b1.show();
A a2 = new B();
a2.show();
}
}
}
Output :
Hello: Base Class!
Hello: Derived Class!
Hello: Base Class!
No comments:
Post a Comment