Declare the package at the beginning of a file using the formpackagepackagename;2. Define the class that is to be put in the package and declare it public.Create a subdirectory under the directory where the main source files are stored.Store the listing as classname.java in the subdirectory created.Compile the file. This creates .class file in the subdirectoryJava supports a keyword called “package” for creatinguser-defined packages. The package statement mustbe the first statement in a Java source file (exceptcomments and white spaces) followed by one or moreclasses.Package name is “myPackage” and classes areconsidred as part of this package; The code is saved ina file called “ClassA.java” and located in a directorycalled “myPackage”.
Here, access is either public or not used.When no access specifier is included, then default access results, and the interface is only available to other members of the package in which it is declared.name is the name of the interface, and can be any valid identifier.The methods which are declared have no bodies.Each class that includes an interface must implement all of the methodsVariables can be declared inside of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class. They must also be initialized with a constant value.
Here, access is either public or not usedIf a class implements more than one interface, the interfaces are separated with a comma.The methods that implement an interface must be declared public.
It cannot extend classes
Package - is a collection of related classes and
interfaces that provides access protection and
Declare the package at the beginning of a file using the
Public class FirstClass
//Body of the class
Two types of packages:
1. Java API packages
2. User defined packages
general form of importing package is:
import myPackage.secondPackage .ClassName;
Public : available/visible everywhere
Private : only visible within that class
Protected : visible every where except the nonsubclasses in other package
public class c1 extends c2
public void show_c1()
public static void main(String ar)
c1 c=new c1();
It defines a standard and public way of
specifying the behavior of classes.
It supports the concept of multiple
All methods of an interface are abstract
To define an interface, we write:
interface Interface Name [extends other interfaces]
constant declarations ;
abstract method declarations ;
int a =10;
public void eat( );
public void travel( );
Once an interface has been defined, one or more
classes can implement that interface.
This can be done as:
Class classname implements interfacename
body of classname
public class Mammal implements Animal
public void eat()
System.out.println ("Mammal eats");
public void travel()
System.out.println ("Mammal travels");
public static void main(String args[ ])
Mammal m = new Mammal( );
An interface can extend another interface, similarly to
the way that a class can extend another class.
The extends keyword is used to extend an interface.
interface name2 extends name1
body of name2