Home » Core Java » class » Page 7

class

Inner class instance example

Bipartite Graph

In this example we shall show you how to call an inner classe’s instance in a class. To call an inner class instance in a class, we must first create an instance of the outer class, and then create an instance of the inner class, as described in the example: We have created a class, InnerClassInstance that has two inner classes, InnerClass1 ...

Read More »

Singleton Object example

Bipartite Graph

With this example we are going to demonstrate how to create and use a Singleton class. The Singleton’s purpose is to control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just ...

Read More »

Protected Keyword

Bipartite Graph

This is an example of how to use the protected keyword in a class. In order to use the protected keyword, we have created a class with a protected method and a subclass that uses its protected method: We have created a class, BadGuy that has a String field, myname and a protected void set(String nm) method that sets its field to ...

Read More »

Overriding Fields

Bipartite Graph

In this example we shall show you how to override a super class field in a subclass. To override a super class field in a subclass we have performed the following steps: We have created a class, A that has a final int field set to 1, and an int field. It has an int getAttr() that prints a message and ...

Read More »

Java Overloading Methods

Bipartite Graph

With this example we are going to demonstrate how to use Overloading Java Methods in a class. Overloaded methods are methods with the same name signature but either a different number of parameters or different types in the parameter list. We have created a class, that has constructors and methods with the same names but with different arguments and use ...

Read More »

Generic Constructor

Bipartite Graph

This is an example of how to use a generic constructor of a class. In order to see how a generic constructor works we have created a class with a generic constructor and then created instances of the class to use its constructor. GenericClass class has a double field, val. It has a constructor using an object of T class that ...

Read More »

Static Initialization Block example

Bipartite Graph

In this example we shall show you how to use a static block for initialization of a classe’s fields. To show how a static block works in a class we have performed the following steps: We have created a class StaticInitializationBlock that has a static int array initialized with 10 fields. It uses a static block, where it puts to ...

Read More »

Polymorphism Java Example (with video)

Bipartite Graph

In this post, we feature a comprehensive Polymorphism Java Example. 1. Introduction In word Polymorphism, the word Poly stands for ‘many’ and the word morph stands for ‘forms’. Polymorphism is the ability of an object to take on many forms. Polymorphism is the capability of an action or method to do different things based on the object that it is ...

Read More »

List the interfaces a class implements

Bipartite Graph

This is an example of how to list the interfaces that a class implements in Java. We are using the TreeMap class to get the list of all the interfaces implemented by the class. We get the TreeMap class and use getInterfaces() API method of Class to get its interfaces. It returns a new array of the interfaces implemented by ...

Read More »

Get the super-class of an object

Bipartite Graph

In this example we shall show you how to get the superclass of an Object. We can try to get the superclass of any Java class, as shown in the steps below: We create an Object and a new Class object. We create a new String object. We set to the Class object the String object’s superclass, using getClass() API ...

Read More »