TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Override Isortable Interface
Harishsady D
Jul 09
2015
Code
1.8
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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();
}
}
}
}
Advanced Concept
Override Interface