Skip to main content

Create a program in C# by using Vehicle Car and Truck as a classes and implement polymorphism in it.


using System;

namespace POLYMORPHISM1
{
    public class Program
    {
        static void Main(string[] args)
        {
            Vehicle vehicle = new Vehicle();
            Vehicle vehicle1 = new Car();
            Vehicle vehicle2 = new Truk();

            vehicle.start();
            vehicle.horn();
            vehicle.stop();

            vehicle1.start();
            vehicle1.horn();
            vehicle1.stop();

            vehicle2.start();
            vehicle2.horn();
            vehicle2.stop();

            Console.ReadLine();
        }
    }


    public class Vehicle
    {
        public virtual void start() { Console.WriteLine("Vehicle Started"); }
        public virtual void horn() { Console.WriteLine("Vehicle Horned"); }
        public virtual void stop() { Console.WriteLine("Vehicle Stopped"); }
    }

    public class Car : Vehicle
    {
        public override void start() { Console.WriteLine("Car Started"); }
        public override void horn() { Console.WriteLine("Car Horned"); }
        public override void stop() { Console.WriteLine("Car Stopped"); }
    }
    public class Truk : Vehicle
    {
        public override void start() { Console.WriteLine("Truck Started"); }
        public override void horn() { Console.WriteLine("Truck Horned"); }
        public override void stop() { Console.WriteLine("Truck Stopped"); }
    }
}


output:
=====

Comments

  1. I really like your post. I found it very helpful. Please keep posting such kind of valuable post.
    C++ Course in Delhi

    ReplyDelete
  2. I read your post and got it quite informative. I couldn't find any knowledge on this matter prior to. I would like to thanks for sharing this article here. Best C++ With Data Structures Course In Delhi

    ReplyDelete

Post a Comment