Java Class Cast() Method

The cast() method of java Class class casts an object to the class or interface represented by this Class object.

Syntax

Parameter

obj - the object to be cast

Returns

the object after casting, or null if obj is null

Throws

ClassCastException

Example 1

Test it Now

Output:

class ClassasCastExample2
class Base
class Derived
class Base

Example 2

Test it Now

Output:

class ClassasCastExample2
Class Derived Display() function
class Base
class Derived
class Base

Next TopicJava Class




Latest Courses