Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4256128
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:18:44+00:00 2026-05-21T05:18:44+00:00

I have an interface Class object provided as a method parameter, and I also

  • 0

I have an interface Class object provided as a method parameter, and I also have a Object instance of the object being casted.

Task: To cast the object as if I would do the interface = object thing but manually using Class.cast() method.

And the star of the show:

def readObject(type, path) {
        Object obj
        // Prevent playing with NPE
        if (groovyClassLoader == null)
            groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader())

        // The final quest begins
        try {
            Class groovyClass = groovyClassLoader.parseClass(new File(path))

// at this point: type ins an instance of Class which is an interface
// to which I need to assign the obj instance

// groovyClass is an instance of Class 
// out of which I need to get object and eventually cast it to type
// so something like:
//            groovyClass = groovyClass.cast(type)
//            obj = groovyClass.newInstance()
// or
//            obj = groovyClass.newInstance()
//            obj = groovyClass.cast(type)



            obj = groovyClass.newInstance()
            obj = type.cast(obj)

        } catch (ex){
            //TODO:IO and obj creation exception should be logged
            ex.printStackTrace()
        }
        return obj
    }

This is a mighty warior quest for survival with mage casting and I’m starting to feal that my mission is out of mighty land possibilities XD

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:18:45+00:00Added an answer on May 21, 2026 at 5:18 am

    You cannot create an instance of an interface. You must first define a class that implements the interface and then create an instance of that class. You can then pass this object as an argument to any method that accepts a parameter of the interface type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a interface class abc { public: virtual int foo() = 0; ... }
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
Say I have the following Objective-C class: @interface Foo { int someNumber; NSString *someString;
Let's say we have public interface ITimestampProvider { DateTime GetTimestamp(); } and a class
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have a factory that returns an interface FormatService : public class FormatServiceFactory {
I have defined an interface in C++, i.e. a class containing only pure virtual
I'm currently writing a class that implements the SeekableIterator interface and have run into
I have a class Animal and an interface it inherits from IAnimal. @MappedSuperclass public
I have a class with both a static and a non-static interface in C#.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.