Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: how to load a class and type cast to that class?

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to load a class and type cast to that class?

    hi friends,
    my code is like this,
      .........
      Class c=new Class(test);
      Object obj= c.newInstance();

    here ,
    i have a unknown class which is represented by a test
    now i want to call the methods of that unknown class
    how to call?
    i don't know the name of the class, i can get ,
    but how to cast obj into that class and call the methods?
    u suppose u'r own methods of unknown class?

    please tell me if know the logic here.
    thank u
    Last edited by helloworld922; November 9th, 2009 at 10:17 AM. Reason: code tags are surrounded with [], not <>


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: how to load a class and type cast to that class?

    If you dont know the type how can you know the method name?

    // Json

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to load a class and type cast to that class?

    If you now that your class will either all inherit from another class, or implement an interface, you can use polymorphism to call methods of that super class, or from the interface.

Similar Threads

  1. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  2. .class to .exe question
    By james137 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 3rd, 2009, 09:18 PM
  3. Class directory
    By Kit in forum Java Theory & Questions
    Replies: 7
    Last Post: October 11th, 2009, 10:07 AM
  4. Video Class
    By Swankee in forum Java SE APIs
    Replies: 2
    Last Post: October 8th, 2009, 11:19 AM
  5. How to delete and add elements to an array dynamically?
    By k_w_r2007 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 21st, 2009, 11:31 AM