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

  • SEARCH
  • Home
  • 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 7749053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:56:40+00:00 2026-06-01T10:56:40+00:00

I have a following model: abstract Article {id, name, text} final NewspaperArticle { printDate

  • 0

I have a following model:

abstract Article {id, name, text}

final NewspaperArticle { printDate } extends Article

final ScientificArticle { quotations, publicationDate } extends Article

final ... { ... } extends Article

I wonder how I could achieve casting the final entities between each other, so that the common fields of Article superclass would be set and the different fields would stay null. Something like:

ScientificArticle scientificArticle = ...;
NewspaperArticle newspaperArticle = new NewspaperArticle();
newspaperArticle.copyOf(scientificArticle);

Is it possible to cast it or I will have to use reflection?

  • 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-06-01T10:56:41+00:00Added an answer on June 1, 2026 at 10:56 am

    To answer your question in short: No, you can’t cast an object to another class. You may only cast to supertypes of the object’s real class. (A class is also a supertype of itself.)

    You could, however, use composition instead of inheritance:

    interface Article { ArticleData getArticleData(); }
    
    final ArticleData {id, name, text}
    
    final NewspaperArticle { articleData, printDate } implements Article
    
    final ScientificArticle { articleData, quotations, publicationDate } implements Article
    
    final ... { articleData, ... } implements Article
    

    Then you could add a constructor like NewspaperArticle(Article createFrom) to each class which sets the article data field and leaves the others null:

    class NewspaperArticle {
      ArticleData articleData;
      Date printDate = null;
    
      NewspaperArticle(Article createFrom) {
        this.articleData = createFrom.getArticleData();
      }
    }
    

    Your example would then look like:

    ScientificArticle scientificArticle = ...;
    NewspaperArticle newspaperArticle = new NewspaperArticle(scientificArticle);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following abstract Django models: class Food(models.Model): name = models.CharField(max_length=100) class Meta:
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS
I have the following classes in my Model: public abstract class Entity : IEntity
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I have the following model: class User: name = models.CharField( max_length = 200 )
I have the following model (simplified): abstract class CartItem { EntityReference<Cart> Cart; } class
I have the following Model: public class Person { public string Name {get;set;} public
I have the following code: abstract class BaseLpmServiceForm extends BaseFormPropel { public function setup()
I have the following zendframework model classes class Application_Model_DbTable_Mobile extends Zend_Db_Table_Abstract { protected $_name

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.