Java Program to Create a Package to Access the Member of External Class and Same Package Last Updated : 18 Aug, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report As the name suggests a package in java that contains all the same kinds of classes, Abstract classes, interfaces, etc that have the same functionality. A Single package can contain many classes, Abstract classes e.g. java.util package etc. There are two types of packages: Built-in-package: These are the packages that are predefined in the java jar file. The most commonly used package in java. util and java.io package in java.User-defined package: These packages are created by the user that used to store related classes some other utility functions, interfaces and abstract classes, etc which are defined for the specific task is called a user-defined package.Project structure: Project -- GFG | | Package 1 GFG1 | GFG1.java (class) Package 2 GFG2 | GFG2.java (class) | GFG3.java (class)Package 1: GFG1.java Java package GFG1; // Creating Interface interface GFG1Interface { String name = "This is the Interface of GF1"; void GFG1Interface(); } public class GFG1 { // Instance variable String name; // Getter Function public String getName() { return name; } // Setter Function public void setName(String name) { this.name = name; } } Â Package 2: GFG3.java Java package GFG2; // Creating Interface interface GFG3Interface { String name = "GFG"; public void interfaceGFG(); } // Creating Abstract class abstract class GFGabstract { String name = "GFGAbstract"; // Abstract Method abstract public void print(); } public class GFG3 { // Instance Variables int first; int second; // Creating Constructor GFG3(int a, int b) { this.first = a; this.second = b; } // Creating add Function public int add() { return this.first + this.second; } } Â Accessing the members of package 1 class in package 2 class: GFG2.java Java package GFG2; // Importing the members of GFG1 package import GFG1.*; public class GFG2 implements GFG3Interface { @Override public void interfaceGFG() { System.out.println( "This is the interface of the GFG3class"); } public static void main(String args[]) { // Creating object of class GFG1 GFG1 ob = new GFG1(); // Calling setName Function ob.setName("GFGsetter"); System.out.println(ob.getName()); // Creating object of class GFG2 GFG2 ob2 = new GFG2(); ob2.interfaceGFG(); } } Â Output: Â Comment More infoAdvertise with us Next Article How to Access Private Field and Method Using Reflection in Java? Z zack_aayush Follow Improve Article Tags : Java Java Programs Java-Packages Practice Tags : Java Similar Reads Java Program to Check if a Given Class is a Local Inner Class Local Inner Classes are classes declared inside a block. These classes are only visible inside the block. So, You need to instantiate the class within the block. Sometimes, this block can be for loop or if-else clause. These classes can access the fields of a class enclosing it. The local inner clas 5 min read Java Program to Check if a Given Class is an Inner Class Inner class is a member of another class which is basically a non-static nested class i.e. if a class is inside another class and is not static, then the class is called is referred to as an inner class. Types of Inner Classes: Nested Inner classMethod Local inner classesAnonymous inner classesStati 5 min read How to Access Private Field and Method Using Reflection in Java? If we want to access Private Field and method using Reflection we just need to call setAccessible(true) on the field or method object which you want to access. Class.getDeclaredField(String fieldName) or Class.getDeclaredFields() can be used to get private fields. Whereas Class.getDeclaredMethod(Str 3 min read How to Create a Package in Java? Package in Java is a mechanism to encapsulate a group of classes, sub-packages, and interfaces. All we need to do is put related classes into packages. After that, we can simply write an import class from existing packages and use it in our program. A package is a container of a group of related cla 4 min read How to Read a File From the Classpath in Java? Reading a file from the classpath concept in Java is important for Java Developers to understand. It is important because it plays a crucial role in the context of Resource Loading within applications. In Java, we can read a file from the classpath by using the Input Stream class. By loading the cla 3 min read Four Main Object Oriented Programming Concepts of Java Object-oriented programming generally referred to as OOPS is the backbone of java as java is not a purely object oriented language but it is object oriented language. Java organizes a program around the various objects and well-defined interfaces. There are four pillars been here in OOPS which are l 7 min read Like