Write a program to implement packages and interface in java.
Program code:
(i)
package mypack;
public class Test
{
public static void main(String args[])
{
System.out.println("Welcome to First package");
}
}
Save Test.java
Compile: Syntax : javac -d directory javafilename
Eg. javac –d.Test.java
Run: java mypack.Test
Output:
Welcome to First package
(ii)
package my;
public class A
{
void show()
{
System.out.print(“Hello”);
}
}
Compile using javac –d A.java
How to use package in another class
import my.*;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
Output :
javac B.java
java B
Hello
(iii).
interface printable
{
void print();
}
class A implements printable
{
public void print()
{
System.out.println("Hello");
}
public static void main(String args[])
{
A obj = new A(); obj.print();
}
}
Output: Hello
Post A Comment:
0 comments: