Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to Override Isortable Interface
WhatsApp
Harieswaran D
Jul 09
2015
2.1
k
0
0
Overriding interface
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
Overriding_interface
{
class
Program
{
/// <summary>
/// /////////http://msdn.microsoft.com/en-us/library/orm-9780596521066-01-13.aspx ////for theory .........very usfull
/// </summary>
/// <param name="args"></param>
static
void
Main(
string
[] args)
{
Tester t =
new
Tester();
t.Run();
Console.ReadLine();
}
}
interface
IStorable
{
void
Read();
void
Write();
}
public
class
Note: IStorable
{
public
Note(
string
s)
{
Console.WriteLine(
"Creating Note with: {0}"
, s);
}
// Note's version of Read( ) is virtual
public
virtual
void
Read()
{
Console.WriteLine(
"Note Read Method for IStorable"
);
}
// Note's version of Write( ) is NOT virtual!
public
void
Write()
{
Console.WriteLine(
"Note Write Method for IStorable"
);
}
}
public
class
Document: Note {
public
Document(
string
s):
base
(s) {
Console.WriteLine(
"Creating Document with: {0}"
, s);
}
// override the Read method
public
override
void
Read()
{
Console.WriteLine(
"Overriding the Read method for Document!"
);
}
// implement my own Write method
public
new
void
Write()
{
Console.WriteLine(
"Implementing a new Write method for Document!"
);
}
}
class
Tester
{
public
void
Run()
{
Note theNote =
new
Document(
"Test Document"
);
theNote.Read();
theNote.Write();
Console.WriteLine(
"\n"
);
IStorable isStorable = theNote
as
IStorable;
if
(isStorable !=
null
)
{
isStorable.Read();
isStorable.Write();
}
Console.WriteLine(
"\n"
);
// This time create a reference to the derived type
Document theDoc =
new
Document(
"Second Test"
);
theDoc.Read();
theDoc.Write();
Console.WriteLine(
"\n"
);
IStorable isStorable2 = theDoc
as
IStorable;
if
(isStorable !=
null
)
{
isStorable2.Read();
isStorable2.Write();
}
}
}
}
Override Interface
Advanced Concept