Object Oriented Programming (Introduction)

Inheritance

On OOP(Object Oriented Programming), all class, even the class that build java API and .net Library, were subclasses from it's superclass object. Contoh hirarki dari kelas dapat dilihat pada diagram dibawah ini.
Class on the top level, or class that inherit was called "superclass", even though the class that receive inheritance was called "subclass".

OOP Hierarchy
Inheritance has big advantage on Object Oriented Programming because  the behavior and method on the superclass can automatically inherited to it's subclasses, so this method can be used on the subclass that receive inheritance from it's superclass. 


Nb : Method = Procedure Or Function




1.2 Define SuperClass and SubClass

To get the class, we use the keyword "extends"  for java and ":" for C#. With illustration like this following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace Console_Test
{
class Vehicle
{
protected string Engine = "One Cilinder" ,VehicleName ="My Private Car",Type=VehicleType.SUV.GetHashCode().ToString();
protected bool FourWheelDrive = true;



public enum VehicleType
{


CityCar=0,SUV=1,Truck=2,SportCar=4
}


public Vehicle()
{
// System.Console.Out.Write("This was Vehicle Constructor");

}

public string GetName()
{
return this.VehicleName;
}


public void SetName(string UrVehicleName)
{
this.VehicleName = UrVehicleName;
}


public void SetEngineType(string UrEngineType)
{
this.Engine = UrEngineType;
}


public string GetEngineType()
{
return Engine;
}


public string GetType()
{
return Type;
}


public void SetType(string Type_)
{
Type = Type_;
}


public bool GetForWheelDrive()
{
return this.FourWheelDrive;
}


public void SetForWheelDrive(bool WD)
{
this.FourWheelDrive = WD;
}


}
}



Then we define the subclass of  Vehicle class with name "AudiTT"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Console_Test
{
class AudiTTCar : Vehicle
{

public AudiTTCar()
{
this.SetName("AudiTT");
this.SetForWheelDrive(true);
this.SetEngineType("4 Cilinder");
this.SetType(Vehicle.VehicleType.SUV.GetHashCode().ToString());
}

}
}
On the "Main method"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Console_Test
{
class Program
{
static void Main(string[] args)
{
System.Console.Out.WriteLine("Nama Kendaraan Ini Adalah :{0}",new Vehicle().GetName());

System.Console.Out.WriteLine("Jenis Mesin :{0}", new Vehicle().GetForWheelDrive() ? "4WD" : "Bukan 4WD");

System.Console.Out.WriteLine("Silinder Mesin :{0}", new Vehicle().GetEngineType());

System.Console.Out.WriteLine("Jenis Mobil :{0}", Enum.GetName(typeof(Vehicle.VehicleType),int.Parse(new Vehicle().GetType())));

System.Console.Out.WriteLine("================================");

System.Console.Out.WriteLine("Nama Kendaraan Ini Adalah :{0}", new AudiTTCar().GetName());

System.Console.Out.WriteLine("Jenis Mesin :{0}", new AudiTTCar().GetForWheelDrive() ? "4WD" : "Bukan 4WD");

System.Console.Out.WriteLine("Silinder Mesin :{0}", new AudiTTCar().GetEngineType());

System.Console.Out.WriteLine("Jenis Mobil :{0}", Enum.GetName(typeof(AudiTTCar.VehicleType), int.Parse(new AudiTTCar().GetType())));

System.Console.ReadKey();
}
}
}

Bookmark and Share

0 comments:

Post a Comment