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 6110149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:28:59+00:00 2026-05-23T14:28:59+00:00

String child = C; Parent p = null; try { Class c1 = new

  • 0
String child = "C";
Parent p = null;
try {
    Class c1 = new Class.forName(child);
    Constructor co = c1.getConstructor();
    // p=co.newInstance(null); //This gives compilatoin error cannot
    // conver object to Parent
    // p=(c1.getClass())co.newInstance(null);//also gives cast errror
    p = (Parent) co.newInstance(null);// this works but it typecasts the
    // child to Parent
} catch (Exception e) {
}

What am i trying to do.

I have multiple Child classes inherited from Parent. I am getting child class name as string input.

I want to instantiate the object of Child class and assign it to Parent. I do not want to type cast Child to Parent. As later in the code i need to compare two Child classes. If I typecast it to Parent. I cannot differentiate between Child1 and Child2.

  • 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-23T14:29:00+00:00Added an answer on May 23, 2026 at 2:29 pm

    Typecasting has absolutely no effect on the object itself. Using p = (Parent) t simply does a runtime check on t to make sure that the type of t is assignable to Parent (i.e. either t is-a Parent or it is-a subclass of Parent) . Afterward, t will still be a Child1 or whatever its actual type always has been.

    Use the explicit cast.

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

Sidebar

Related Questions

I have a Parent/Child object/mapping as follows: class Parent { int Id; string name;
This is my code: File TempFiles = new File(Tempfilepath); if (TempFiles.exists()) { String[] child
My model is a simple parent/child representation looking like this public class Parent {
How can I set parent-child relation on same table? Id int, title string, ParentId
I have a parent form first.cs like this public partial class first : Form
I launch a child process in Java as follows: final String[] cmd = {<childProcessName>};
string [] files = new string[2]; files[0] = ThinkFarAhead.Example.Settings.Configuration_Local.xml; files[1] = ThinkFarAhead.Example.Settings.Configuration_Global.xml; //Resharper complains
string[] filesOfType1 = GetFileList1(); string[] filesOfType2 = GetFileList2(); var cookieMap = new Dictionary<string, CookieContainer>();
Customer Entity (Parent Entity) @Entity public class Customer { @Id @GeneratedValue(strategy = GenerationType.AUTO) private
Im trying to access some variables from the parent in a child mc. Parent

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.