For testing, I have created a class named Vehicle that I made abstract since I don’t want to allow anyone to create instances of this class. Its only purpose is to exist so I can use my other classes, Car and Bicycle, to inherit from it.
Do I really need a constructor and getters and setters in my Vehicle class?
I have attached my code below to show what I have come up with so far. I know that I still need to implement proper validation for various things, and so on.
Vehicle.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ObjectsClasses
{
//Create a super class named vehicle and set it to abstract because we do not allow anyone to create instances of this class
abstract class Vehicle
{
//Properties
private string name;
private string color;
private int year;
private int wheels;
//Constructor
public Vehicle(string name, string color, int year, int wheels)
{
this.name = name;
this.color = color;
this.year = year;
this.wheels = wheels;
}
// GETTERS & SETTERS
public string Name
{
get { return name; }
set { name = value; }
}
public string Color
{
get { return color; }
set { color = value; }
}
public int Year
{
get { return year; }
set {
//Check if the value is over 50 years or in the future. If so, throw an error
int thisYear = DateTime.Now.Year;
int timeDifference = thisYear - value;
if (timeDifference > 50 || value > thisYear)
{
throw new ArgumentException("Error! The car can't have been made for over 50 years ago or in the future!");
}
else
{
year = value;
}
}
}
public int Wheels
{
get { return wheels; }
//If wheels is set to under 2 or over 18, default it to 4 wheels using a ternary operator
set { wheels = value < 2 || value > 18 ? 4 : value; }
}
//Methods
public abstract void Honk(); //Every class that inherits from Vehicle must create it's own version of this method
}
}
Car.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ObjectsClasses
{
class Car : Vehicle
{
//Properties
private string fuelType;
public Car(string name, string fuelType, string color, int year, int wheels) : base(name, color, year, wheels)
{
this.fuelType = fuelType;
}
//Getters & Setters
public string FuelType
{
get { return fuelType; }
set { fuelType = value; }
}
//Methods
//The method Honk inherits from it's parent class Vehicle but makes it's own version by using override
public override void Honk() => Console.WriteLine("TUUUT, TUUUT, TUUUT!");
}
}
Bicycle.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ObjectsClasses
{
class Bicycle : Vehicle
{
//Properties
private string material;
//Constructor
public Bicycle(string name, string color, string material, int wheels) : base(name, color, 0, wheels)
{
this.material = material;
}
//GETTER & SETTER
public string Material
{
get { return material; }
set { material = value; }
}
//Methods
//The method Honk inherits from it's parent class Vehicle but makes it's own version by using override
public override void Honk() => Console.WriteLine("PLING, PLING, PLING!");
}
}
Marcus Lehm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1