Write a java program to demonstrate the package concept using a simple example.

Program Algorithm:
STEP 1: Create a package of user-defined.
STEP 2: Create a class of specific user-defined and save them in the package folder created.
STEP 3: All the classes are saved in a separate file and used in a package folder just by importing them.
STEP 4: The members and methods and class defined as protected specifier can be accessed throughout the package.

Program Code:
File name: Check.java
package rear;
public class Check
{
int n=1;
private int np=2;
protected int npro=3;
public int npub=4;
public Check()
{
System.out.println("Base constructor:");
System.out.println("default n:"+n);
System.out.println("private n(np):"+np);
System.out.println("protected n(npro):"+npro);
System.out.println("public n(npub):"+npub);
System.out.println(" ");
}
}
File name: DerSame.java
package rear;
class DerSame extends Check
{
DerSame()
{
System.out.println("Derived Constructor");
System.out.println("n:"+n);
System.out.println("npro:"+npro);
System.out.println("npub:"+npub);
System.out.println(" ");
}
}
File name: Create.java
package rear;
class Create
{
public static void main(String args[])
{
DerSame d=new DerSame()
}
}
File name: DerAno.java
package Front;
import rear.Check;
class DerAno
{
public static void main(String args[])
{
System.out.println("Importing class Constructor");
Check c=new Check();
System.out.println("npub:"+c.npub);
System.out.println(" ");
}
}

Sample Inputs and Outputs:
Running Create.class
Base constructor:
default n:1
private n(np):2
protected n(npro):3
public n(npub):4
Derived Constructor
n:1
npro:3
npub:4
Running DerAno.class
Importing class Constructor
Base constructor:
default n:1
private n(np):2
protected n(npro):3
public n(npub):4
npub:4
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: