interface Vehicle {
// Constant
public static final int MAX_SPEED = 200;
// Abstract method
public void start();
// Default method
public default void stop() {
System.out.println(“Vehicle stopped”);
}
// Static method
public static void honk() {
System.out.println(“Beep beep”);
}
// A nested type
enum Color {
RED, GREEN, BLUE;
}
}
// Car implements Vehicle interface
class Car implements Vehicle {
// A field
private Color color;
// A constructor
public Car(Color color) {
this.color = color;
}
// Implement the abstract method
public void start() {
System.out.println(“Car started”);
}
// Override the default method
public void stop() {
System.out.println(“Car stopped”);
}
// A method to get the color
public Color getColor() {
return color;
}
}
// A class to test the interface and the class
class Main {
public static void main(String[] args) {
// Create a car object
Car car = new Car(Vehicle.Color.RED);
// Call the methods defined by the interface
car.start();
car.stop();
Vehicle.honk();
// Access the static constant and the nested type
System.out.println(“The max speed is ” + Vehicle.MAX_SPEED);
System.out.println(“The car color is ” + car.getColor());
}
}
interface IVehicle
{
// A static constant
public static readonly int MAX_SPEED = 200;
// An abstract method
void Start();
// A default method
public virtual void Stop()
{
Console.WriteLine(“Vehicle stopped”);
}
// A static method
public static void Honk()
{
Console.WriteLine(“Beep beep”);
}
// A nested type
enum Color
{
Red, Green, Blue;
}
}
// A class that implements the interface
class Car : IVehicle
{
// A field
private Color color;
// A constructor
public Car(Color color)
{
this.color = color;
}
// Implement the abstract method
public void Start()
{
Console.WriteLine(“Car started”);
}
// Override the default method
public override void Stop()
{
Console.WriteLine(“Car stopped”);
}
// A method to get the color
public Color GetColor()
{
return color;
}
}
// A class to test the interface and the class
class Program
{
public static void Main(string[] args)
{
// Create a car object
Car car = new Car(IVehicle.Color.Red);
// Call the methods defined by the interface
car.Start();
car.Stop();
IVehicle.Honk();
// Access the static constant and the nested type
Console.WriteLine(“The max speed is ” + IVehicle.MAX_SPEED);
Console.WriteLine(“The car color is ” + car.GetColor());
}
}