Create user defined package - Java Program

Q. Write a program to create a user defined package in Java.

Answer:

A package is a mechanism to group the similar type of classes, interfaces and sub-packages and provide access control.  It organizes classes into single unit. 

Vehicle.java

package vehicles;
interface Vehicle
{
        public void run();
        public void speed();
}


Car.java

package vehicles;
public class Car implements Vehicle
{
        public void run()
        {
                System.out.println("Car is running.");
        }
        public void speed()
        {
                System.out.println("Speed of Car: 50 Km/h");
        }
        public static void main(String args[])
        {
                Car Car = new Car();
                Car.run();
                Car.speed();
                System.out.println("Hello World!");
        }
}


Output:

package