Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press
Buy directly from Paradoxal Press at $33.99 (Save 43%)
Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA
|
Listing 12-16 extracted from chapter
Inheritance, polymorphism and abstraction
Listing 12-15< > Listing 13-1
This listing can be compiled with the command line: csc.exe /target:exe Example_12_16.cs Errors: 0 Warnings: 0
Example_12_16.cs
using System;
interface IA { void f( int i ); }
interface IB { void g( int i ); }
abstract class FooBase { public abstract void h( int i ); }
class FooDerived : FooBase, IA {
public void f( int i ) { /*...*/ }
public override void h( int i ){ /*...*/ }
}
class Program {
static void Main() {
IA refA = new FooDerived();
IB refB = null;
FooBase refAbst = null;
FooDerived refDeriv = null;
refAbst = refA as FooBase;
// Here, refAbst is null at runtime.
if ( refAbst != null ) {
// use refAbst...
}
refDeriv = refA as FooDerived;
// Here, refDeriv is null at runtime.
if ( refDeriv != null ) {
// use refC...
}
refB = refA as IB;
// Here, refB is null at runtime.
if ( refB != null ) {
// use refB...
}
}
}
Copyright Patrick Smacchia 2006 2007
|