Description
This program is used to
demonstrate the concept of package in Java. The steps below demonstrates
how to create package in Java. Student1.java is the package and
Student.java is the program that uses the package.
Step 1
Create a directory named Mypack under your main directroy, and inside that, save the following code as Student1.java.
// Package program
package Mypack;
public class Student1
{
String name;
int sid;
public Student1(String n,int id)
{
name=n;
sid=id;
}
public void display()
{
System.out.println("Name: "+name);
System.out.println("Sid: "+sid);
}
}
Step2
Save the following code as Student.java under your main directory.
// This program imports the class Student1 under Mypack
//program to demostrate package
import Mypack.Student1;
public class Student extends Student1
{
String name;
int rollno,mark;
Student(String n,int r,int m)
{
super(n,r);
this.mark=m;
this.name=n;
this.rollno=r;
}
public void show()
{
System.out.println("Mark: "+mark);
}
public static void main(String s[])
{
Student s1=new Student("Amal",30,50);
s1.display();
s1.show();
}
Now the program is ready to be executed, compile Student1.java, then compile and execute Student.java
Output
|
Package in Java |