The general form to create a sub-package is

example

package package1.package2;

Where package2 is inside package1.
To use the classes and interfaces of package2 we can write the import statement as
import package1.package2;
Let us see a program that we are creating a package technology inside the package silan by writing the statement

example

package silan.software;

And in this sub-package we are declaring a class simple with a method display ( ).

example

package silan;
package silan.software;
public class simple
{
public void display ( )
{
System.out.println("welcome 2 JavaRace");
}
}
import silan. software.simple;
class Demo
{
public static void main (string args [])
{
Simple s =new Simple ( );
s.display ();
}
}

Output

Examples for practice

 

example"

package Silan;
class Technology
{
public void show( )
{
}
public void get( )
{
}
public static void main(String args[ ])
{
System.out.println("package is created");
}
}

example

import technology.Vivtech ;
class SilanSoftware
{
public static void main( String args[ ] )
{
technology.Vivtech v=new technology.Vivtech( ) ;
System.out.println("art of java programming");
}
}

example

package technology ;
import technology.Vivtech ;
class Silan
{
public static void main(String args[ ])
{
Vivtech v=new Vivtech( ) ;
v.show( ) ;
}
}

w3-example

import technology ;
class Silan
{
public static void main(String args[ ])
{
technology.Vivtech v=new technology.Vivtech( );
System.out.println("programming in java");
}
}

example

package main;
class Base
{
public void Print()
{
System.out.println("Base");
}
}
class Derived extends Base
{
public void Print()
{
System.out.println("Derived");
}
}
class Main
{
public static void DoPrint( Base o )
{
o.Print();
}
public static void main(String[] args)
{
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}

  1. The class given below have saved in the folder "Silan". Will the program execute?
  2. The code uses the class defined above. Class SilanSoftware is not defined in technology folder. Will the code execute without giving any errors ?
  3. The method show( ) in Vivtech has been set as private. Class Silan is in the same package as Vivtech. Will the class able to use the method ?
  4. Importing a complete package with all its classes has been demonstrated in the program. Will the class compile ?
  5. What will be the output ?
Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment