Use virtual methods and polymorphism. : virtual « Language Basics « C# / CSharp Tutorial

C# / CSharp Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statement
5. String
6. struct
7. Class
8. Operator Overload
9. delegate
10. Attribute
11. Data Structure
12. Assembly
13. Date Time
14. Development
15. File Directory Stream
16. Preprocessing Directives
17. Regular Expression
18. Generic
19. Reflection
20. Thread
21. I18N Internationalization
22. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / CSharp Tutorial » Language Basics » virtual 
1. 25. 4. Use virtual methods and polymorphism.
 
 
using System; 
 
class Shape {  
  double pri_width;  // private 
  double pri_height; // private 
  string pri_name;   // private 
  
  public Shape() {  
    width = height = 0.0;  
    name = "null";  
  }  
  
  public Shape(double w, double h, string n) {  
    width = w;  
    height = h;  
    name = n;  
  }  
  
  public Shape(double x, string n) {  
    width = height = x;  
    name = n;  
  }  
  
  public Shape(Shape ob) {  
    width = ob.width;  
    height = ob.height;  
    name = ob.name;  
  }  
  
  // Properties for width, height, and name 
  public double width 
    get return pri_width; 
    set pri_width = value; 
  
 
  public double height 
    get return pri_height; 
    set pri_height = value; 
  
 
  public string name 
    get return pri_name; 
    set pri_name = value; 
  
  
  public void showDim() {  
    Console.WriteLine("Width and height are " +  
                       width + " and " + height);  
  }  
  
  public virtual double area() {   
    Console.WriteLine("area() must be overridden");  
    return 0.0;  
  }   
}  
  
class Triangle : Shape {  
  string style; // private 
    
  public Triangle() {  
    style = "null";  
  }  
  
  public Triangle(string s, double w, double h
    base(w, h, "triangle") {  
      style = s;   
  }  
  
  public Triangle(double x: base(x, "triangle") {  
    style = "isosceles";   
  }  
  
  public Triangle(Triangle ob: base(ob) {  
    style = ob.style;  
  }  
  
  // Override area() for Triangle. 
  public override double area() {  
    return width * height / 2;  
  }  
  
  // Display a triangle's style. 
  public void showStyle() {  
    Console.WriteLine("Triangle is " + style);  
  }  
}  
  
class Rectangle : Shape {   
  public Rectangle(double w, double h: base(w, h, "rectangle"){ }  
  
  public Rectangle(double x: base(x, "rectangle") { }  
  
  // Construct an object from an object.  
  public Rectangle(Rectangle ob: base(ob) { }  
  
  // Return true if the rectangle is square. 
  public bool isSquare() {   
    if(width == heightreturn true;   
    return false;   
  }   

  // Override area() for Rectangle. 
  public override double area() {   
    return width * height;   
  }   
}  
  
class DynShapes {  
  public static void Main() {  
    Shape[] shapes = new Shape[5];  
  
    shapes[0new Triangle("right"8.012.0);  
    shapes[1new Rectangle(10);  
    shapes[2new Rectangle(104);  
    shapes[3new Triangle(7.0);  
    shapes[4new Shape(1020"generic")
  
    for(int i=0; i < shapes.Length; i++) {  
      Console.WriteLine("object is " + shapes[i].name);  
      Console.WriteLine("Area is " + shapes[i].area());  
  
      Console.WriteLine();    
    }  
  }  
}

        
object is triangle
Area is 48

object is rectangle
Area is 100

object is rectangle
Area is 40

object is triangle
Area is 24.5

object is generic
area() must be overridden
Area is 0

  
1. 25. virtual
1. 25. 1. Demonstrate a virtual method.
1. 25. 2. When a virtual method is not overridden, the base class method is used
1. 25. 3. Virtual method in a multilevel hierarchy
1. 25. 4. Use virtual methods and polymorphism.
w_w__w__.__j_ava2s_.co_m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.