Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can i create a package?

0 votes
asked Oct 28, 2019 by Harish R (120 points)

1 Answer

–3 votes
answered Nov 2, 2019 by Mvvsdurga maligireddy (90 points)

java package is a group of similar types of classes, interfaces and sub-packages.

Package in java can be categorized in two form, built-in package and user-defined package.

To Compile: javac -d . Simple.java
To Run: java mypack.Simple

To run this program from e:\source directory, you need to set classpath of the directory where the class file resides.

 


There are three ways to access the package from outside the package.

  1. import package.*;
  2. import package.classname;
  3. fully qualified name.

**If you import a package, subpackages will not be imported.

commented Jun 20, 2021 by Jayanath karunarathna (110 points)
I believe the question is not about basic java but how to achieve this from onlinegdb. Because if we declare the package we are getting below error.

<CODE>
package com.wiley.test;
/**
 * Complete the getSum method in Parent class.
 * Then do the requred modifications to Main.java file classes
 * to access Parent class getSum method via inheritance.
 *
 * */

public class Main
{

  public static void main (String[]args)
  {
    int[] input = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    System.out.println ("Sum of given array is " +
            new Child ().getSum (input));

  }
}

class Child
{

  public int getSum (int[]input)
  {
    return 0;
  }

}

<Error after run the code>
Error: Could not find or load main class Main.

<Note>
Code runs perfectly if the package declaration is removed.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...