Unit 4 Part 2 Package and Interfaces
Unit 4 Part 2 Package and Interfaces
College of Engineering
SE Computer- Division A
Course Name :Principle of Programming Language
Course Code: 210256
Course InCharge: Kainjan M. Sanghavi
Departmentof
Department ofComputer
ComputerEngineering
Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
What is an interface?
void print();
}
Output:
class A implements printable{
Hello
public void print(){System.out.println("Hello");}
obj.print();
void draw();
//Implementation d.draw();
{System.out.println("drawing rectangle");}
}
Output:
class Circle implements Drawable{ drawing circle
public void draw(){System.out.println("drawing circle");}
interface.
Packages In Java
similar files.
● packages are used to avoid name conflicts and to control access of class,
● Using package it becomes easier to locate the related classes and it also
other files.
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
● Package statement must be first statement in the program even before the
import statement.
● All classes of the package which we wish to access outside the package
● All classes within the package must have the package statement as its first
line.
String empId;
String name;
Package Program
//save as FirstProgram.java
Creates a package
package learnjava; (directory) learnjava
in current directory
public class FirstProgram{
}
}
Compilation of Package Prog : javac -d . FirstProgram.java
The -d switch specifies the destination where to put the generated class file. You
can use any directory name like d:/abc (in case of windows) etc. If you want to
keep the package within the same directory, you can use . (dot).
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
For creating a Java package, you need to follow the below steps.
Now, a dialog box opens up, where you need to fill the source folder of package and
the package name
After clicking Finish above, the package gets created successfully as shown below,
Click Here