.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Illustrates abstract classes and methods

Posted By:SanthoshNathan       Posted Date: November 02, 2010    Points: 15    Category: Visual Studio    URL: http://www.dotnetspark.com  

Illustrates abstract classes and methods
 

using System;


// declare the abstract MotorVehicle class
abstract class MotorVehicle
{

  // declare the fields
  public string make;
  public string model;

  // define a constructor
  public MotorVehicle(string make, string model)
  {
    this.make = make;
    this.model = model;
  }

  // declare the abstract Accelerate() method (no code)
  abstract public void Accelerate();

}


// declare the Car class (derived from MotorVehicle)
class Car : MotorVehicle
{

  // define a constructor
  public Car(string make, string model:
  base(make, model)
  {
    // do nothing
  }

  // override the Accelerate() method (contains code)
  public override void Accelerate()
  {
    Console.WriteLine("In Car Accelerate() method");
    Console.WriteLine(model + " accelerating");
  }

}


public class Example7_9
{

  public static void Main()
  {

    // create a Car object
    Console.WriteLine("Creating a Car object");
    Car myCar = new Car("Toyota""MR2");

    // call the Car object's Accelerate() method
    Console.WriteLine("Calling myCar.Accelerate()");
    myCar.Accelerate();

  }

}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend