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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:35:50+00:00 2026-06-18T09:35:50+00:00

I have some class X with the field A in it. It is a

  • 0

I have some class X with the field A in it. It is a final field, initialized in constructor. Now I have a derived class Y where this field must always be an instance of B, a class that is derived from A. The problem, the class Y needs to call a number specific methods that are only available on the class B but not on its ancestor, A.

I see several solutions:

  1. Reuse the field of the type A, inherited from X, and inside the class Y, cast A to B. This makes my code full of nasty type casts.
  2. Add another field of the type B. Now there are no typecasts but we have two fields of slightly different type that must always hold the same value – also does not feel good.
  3. Add all methods that B provides also to A, throw NotImplementedException. This adds some strange methods that knowingly make no sense in that class.

Which is the most right way to deal with this field? Or maybe some other, better exists? I do not think this is very language specific but must be doable in Java I am using.

  • 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-18T09:35:52+00:00Added an answer on June 18, 2026 at 9:35 am

    The X type should probably be a generic type:

    public class X<T extends A> {
    
        protected T a;
    
        public X(T a) {
            this.a = a;
        }
    }
    
    public clas Y extends X<B> {
    
        public Y(B b) {
            super(b);
        }
    
        public void foo() {
            // here, this.a is of type B, without any type cast.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python, say I have some class, Circle, that inherits from Shape. Shape needs
I have some doubts about a constructor. There is a class Foo that can
I have some AsyncTask class that executing from service: public class DownloadTask extends AsyncTask<Void,
I have some class that I'm passing as a result of a service method,
the basic idea is that you have some class that has a reference type
I have some test class TestKlass = (function() { function TestKlass(value) { this.value =
I have some C# class libraries, that were designed without taking into account things
I am working on a class that has some fields with final access modifier
I have something like this: class A { final int f = 1; int
I have a set of documents that represent some workitems: public class WorkItem {

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.