How do I get interfaces implemented by a class?

The getClass().getInterfaces() method return an array of Class that represents the interfaces implemented by an object.

package org.kodejava.lang;

import java.util.Date;
import java.util.Calendar;

public class ClassInterfaces {
    public static void main(String[] args) {
        // Get an instance of Date class
        Date date = Calendar.getInstance().getTime();

        // Get all interfaces implemented by the java.util.Date class and
        // print their names.
        Class<?>[] interfaces = date.getClass().getInterfaces();
        for (Class<?> i : interfaces) {
            System.out.printf("Interface of %s = %s%n", date.getClass().getName(), i.getName());
        }

        // For the primitive type the interface will be an empty array.
        interfaces = char.class.getInterfaces();
        for (Class<?> i : interfaces) {
            System.out.printf("Interface of %s = %s%n", char.class.getName(), i.getName());
        }
    }
}

The java.util.Date class implements the following interfaces:

Interface of java.util.Date = java.io.Serializable
Interface of java.util.Date = java.lang.Cloneable
Interface of java.util.Date = java.lang.Comparable

How do I get package name of a class?

To get the package name of a class we can use the getClass().getPackage() method of the inspected object.

package org.kodejava.lang;

import java.util.Date;

public class GetPackageName {
    public static void main(String[] args) {
        // Create an instance of Date class, and then obtain the class package
        // name.
        Date date = new Date();
        Package pack = date.getClass().getPackage();
        String packageName = pack.getName();
        System.out.println("Package Name = " + packageName);

        // Create an instance of our class and again get its package name
        GetPackageName object = new GetPackageName();
        packageName = object.getClass().getPackage().getName();
        System.out.println("Package Name = " + packageName);
    }
}

Our code printed the following output:

Package Name = java.util
Package Name = org.kodejava.lang

How do I get super class of an object?

To get the super class of an object we can call the object’s getClass() method. After getting the class type of the object we call the getSuperclass() method to get the superclass. Let’s see the code snippet below.

package org.kodejava.lang;

public class ObtainingSuperClass {
    public static void main(String[] args) {
        // Create an instance of String class
        Object object1 = new String("Hello");

        // Get String class super class
        Class<?> clazz1 = object1.getClass().getSuperclass();
        System.out.println("Super Class = " + clazz1);

        // Create an instance of StringIndexOutOfBoundsException class
        Object object2 = new StringIndexOutOfBoundsException("Error message");

        // Get StringIndexOutOfBoundsException class super class
        Class<?> clazz2 = object2.getClass().getSuperclass();
        System.out.println("Super Class = " + clazz2);
    }
}

The program above prints the following string:

Super Class = class java.lang.Object
Super Class = class java.lang.IndexOutOfBoundsException

How do I sort items in a Set?

The trick to sort a java.util.Set is to use the implementation of a java.util.SortedSet such as the java.util.TreeSet class. The example below shows you the result of using the java.util.TreeSet class, in which the items in it will be sorted based on the element’s natural order.

package org.kodejava.util;

import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        // The TreeSet class is an implementation of a SortedSet, this means
        // that when you are using the TreeSet to store you data collections
        // you'll get the items ordered base on its elements natural order.
        Set<String> set = new TreeSet<>();

        // In the example below we add some letters to the TreeSet, this mean
        // that the alphabets will be ordered based on the alphabet order
        // which is from A to Z.
        set.add("Z");
        set.add("A");
        set.add("F");
        set.add("B");
        set.add("H");
        set.add("X");
        set.add("N");

        for (String item : set) {
            System.out.print(item + " ");
        }
    }
}

This demo prints:

A B F H N X Z 

Java Programming Keywords Summary

Here are the summary of the available keywords in the Java programming language. Keywords are reserved words that already taken and internally used by Java, so we cannot create variables and name it using this keyword.

Keyword Meaning
abstract an abstract class or method
assert used to locate internal program errors
boolean the Boolean type
break breaks out of a switch or loop
byte the 8-bit integer type
case a case of a switch
catch the clause of a try block catching an exception
char the Unicode character type
class defines a class type
const not used
continue continues at the end of a loop
default the default clause of a switch
do the top of a do/while loop
double the double-precision floating-number type
Keyword Meaning
else the else clause of an if statement
enum define an enum type
extends defines the parent class of a class
final a constant, or a class or method that cannot be overridden
finally the part of a try block that is always executed
float the single-precision floating-point type
for a loop type
goto not used
if a conditional statement
implements defines the interface(s) that a class implements
import imports a package
instanceof tests if an object is an instance of a class
int the 32-bit integer type
interface an abstract type with methods that a class can implement
long the 64-bit long integer type
Keyword Meaning
native a method implemented by the host system
new allocates a new object or array
null a null reference
package a package of classes
private a feature that is accessible only by methods of this class
protected a feature that is accessible only by methods of this class, its children, and other classes in the same package
public a feature that is accessible by methods of all classes
return returns from a method
short the 16-bit integer type
static a feature that is unique to its class, not to objects of its class
strictfp Use strict rules for floating-point computations
super the superclass object or constructor
switch a selection statement
synchronized a method or code block that is atomic to a thread
this the implicit argument of a method, or a constructor of this class
Keyword Meaning
throw throws an exception
throws the exceptions that a method can throw
transient marks data that should not be persistent
try a block of code that traps exceptions
void denotes a method that returns no value
volatile ensures that a field is coherently accessed by multiple threads
while a while loop type