Home » Java programming language

Java ClassLoader getParent() method with example

ClassLoader Class getParent() method: Here, we are going to learn about the getParent() method of ClassLoader Class with its syntax and example.
Submitted by Preeti Jain, on November 25, 2019

ClassLoader Class getParent() method

  • getParent() method is available in java.lang package.
  • getParent() method is used to return the parent class loader for delegations.
  • getParent() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getParent() method is a final method, it does not override in child class.
  • getParent() method may throw an exception at the time of returning ClassLoader.
    SecurityException: This exception may throw when its checkPermission() method does not allow access to the parent class loader of this loader when the security manager exists.

Syntax:

    protected  final ClassLoader getParent();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is ClassLoader, it returns a parent class loader.

Example:

// Java program to demonstrate the example 
// of ClassLoader getParent() method of ClassLoader 

public class GetParentOfClassLoader {
 public static void main(String args[]) throws ClassNotFoundException {

  // It loads the class 
  Class cl = Class.forName("GetParentOfClassLoader");

  // It returns the class loader associated with 
  // the given class
  ClassLoader loader = cl.getClassLoader();

  // Display Loader Class
  System.out.println("Loader Class : ");
  System.out.println(loader.getClass());

  System.out.println();

  // Display Loader Class Parent
  System.out.println("Loader Parent Class : ");
  System.out.println(loader.getParent());
 }
}

Output

Loader Class : 
class jdk.internal.loader.ClassLoaders$AppClassLoader

Loader Parent Class : 
jdk.internal.loader.ClassLoaders$PlatformClassLoader@7960847b


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.