Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Sub-Package

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


  1. The class given below have saved in the folder "Silan". Will the program execute?
  2. Example:
    package Silan;
    class Technology
    {
    	public void show( )
    	{
    	}
    	public void get( )
    	{
    	}
    	public static void main(String args[ ])
    	{
    		System.out.println("package is created");
    	}
    }
  3. The code uses the class defined above. Class SilanSoftware is not defined in technology folder. Will the code execute without giving any errors ?
  4. 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");
    	}
    }
  5. 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 ?
  6. Example:
    package technology ;
    import technology.Vivtech ;
    class Silan
    {
    	public static void main(String args[ ])
    	{
    		Vivtech v=new Vivtech( ) ;
    		v.show( ) ;
    	}
    }
  7. Importing a complete package with all its classes has been demonstrated in the program. Will the class compile ?
  8. w3-example

    import technology ;
    class Silan
    {
    	public static void main(String args[ ])
    	{
    		technology.Vivtech v=new technology.Vivtech( );
    		System.out.println("programming in java");
    	}
    }
  9. What will be the output ?
  10. 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);
    	}
    }

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext