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
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: