Virtual Methods.
Virtual methods in base class get executed when there are no override methods in child class.
First priority is given to child class methods.
Difference :
Difference :
virtual methods
can be defined in both base class and child class.
Virtual
methods may not be in child classes(not overrided)
Abstract
methods should be mandatorily defined in child classes.
Abstract methods
should be present in child class(override)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
square s = new square();// this is child class of shappe
s.ShowParams();
Console.Read();
}
}
abstract class shape
{
public abstract void display();
public virtual void ShowParams()
{
Console.WriteLine("Shape");
}
public void area(int d1, int d2)
{
Console.WriteLine(d1 * d2);
}
}
class square : shape
{
private int d1;
public int D1
{
get
{
return d1;
}
set
{
d1 = value;
}
}
public override void display()
{
Console.WriteLine(d1);
}
public void areaofsquare(int s)
{
area(s, s);
}
}
class Rectangle : shape
{
private int d1, d2;
public int D1
{
get
{
return d1;
}
set
{
d1 = value;
}
}
public override void display()
{
Console.WriteLine(d1);
}
public int D2
{
get
{
return d2;
}
set
{
d2 = value;
}
}
public override void ShowParams()
{
Console.WriteLine("Rectangle");
}
}
}
Virtual Methods.
Reviewed by Share less to Learn More
on
6:54 PM
Rating:
No comments