❮ Previous Next ❯

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);
    }
    }
    ❮ Previous Next ❯



  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved