In object-oriented programming we know a term called "polymorphisms", which is a programming technique in a more direct us to program in general than specific. As an example is like we have 3 different classes, namely: "Motorcycle", "Car" and "Aircraft". Where is the third class are derivatives of class "Vehicle". In a class vehicle there is a method that is "Move ()", but whether the method "Move ()" will give the same results on three class. Where if method "Move ()" called on class "motor" will result in the displacement 30km, if the class "Car" will result in the displacement 70Km and if called on class "aircraft" will result in displacement 300km. This is what we refer to as polymorphisms, which use the same method, which will produce a different result depending on the class that uses those methods. Here is a sample program that uses polymorphism:
See The Code :
See The Code ://Class Vehicle.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Polymorphism
{
class Vehicle
{
public Vehicle(int posisi)
{
this.posisi = posisi;
}
private int posisi;
public int Posisi
{
get { return posisi; }
set { posisi = value; }
}
public virtual void Move()
{
return;
}
}
}
//Class Motorcycle.csSee The Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Polymorphism
{
class Motorcycle : Vehicle
{
public Motorcycle(int posisi)
: base(posisi)
{ }
public override void Move()
{
base.Posisi = base.Posisi + 30;
}
}
}
//Class Car.csSee The Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Polymorphism
{
class Car : Vehicle
{
public Car(int posisi)
: base(posisi)
{ }
public override void Move()
{
base.Posisi = base.Posisi + 70;
}
}
}
//Class pesawat.csHopefully Useful
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Polymorphism
{
class Aircraft : Vehicle
{
public Aircraft(int posisi)
: base(posisi)
{ }
public override void Move()
{
base.Posisi = base.Posisi + 300;
}
}
}
reference :
http://yohandamandala.blogspot.com
1 comments:
I appreciate your writing skill.You have given a detailed explanation of polymorphism in a simple step with understandable example.
Post a Comment and Don't Spam!
Dont Spam please