Home » Core Java » Package and Interface in Java

Package and Interface in Java

What is a Package???

A package is a namespace that organizes a set of related classes and interfaces. The Java platform provides an enormous class library (a set of packages) suitable for use in applications. This library is known as the “Application Programming Interface” or “API.



Uses of Packages:

1. Packages help in resolving any name conflicts with names in other packages. So, two classes with the same name can exist in different packages.
2. Using packages, it is easier to provide access control and it is also easier to locate the related classes.
3. The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

package com.mathapachi;
public class HelloWorld {

	public static void main(String[] args) {
  	    System.out.println("HelloWorld!");
	}

}

What is an Interface????

An interface is a reference type in java. It is used to achieve abstraction in java. Interface has the following characteristics:

(I.) An interface can’t be instantiated.

    interface DemoExample{
            void printHelloWorld();
    }

// It is invalid as interface can’t be instantiated.
DemoExample demoExample = new DemoExample();  

(II.) An interface does not contain any constructors.

    interface DemoExample{
           DemoExample(){
                    // It is invalid to have constructor in an interface
          }
    }

(III.) All the methods in an interface are abstract by default whether abstract keyword is written or not.

     interface DemoExample{
            void printHelloWorld();
     }

printHelloWorld() is abstract whether we write abstract before method or not.

void printHelloWorld(); is same as abstract void printHelloWorld();

(IV.) An interface can’t contain instance fields. The only fields that can appear in an interface must be declared both static and final. It is not necessary to write static and final before fields in interface.

(V.) An interface is not extended by a class; it is implemented by a class.

    interface DemoExample{

    }

    class DemoExampleImpl extends DemoExample{

            // It is wrong as class can not extend an interface. A class implements an interface.

    }

    class DemoExampleImpl implements DemoExample{

           // It is correct as class can implement an interface.

    }

(VI.) An interface can extend multiple interfaces.

(VII.) An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.

(VIII.) Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.

(IX.) Methods in an interface are implicitly public.

Example of an interface containing fields and methods:

package com.mathapachi;
public interface DemoExampleInterface {

		public static final String NAME = "Gopal";
		public static final String CITY = "MUMBAI";
	
		void doSomething();
	
}

Here, method doSomething() is an abstract method.