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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:14:44+00:00 2026-06-02T15:14:44+00:00

I created an abstract base class. It contains an object which should be extended

  • 0

I created an abstract base class. It contains an object which should be extended by any subclasses:

public abstract class AbstractParent {
    protected AbstractObject subMePlz;
    // ... some fields that all subclasses need
    public AbstractParent() {
        this.subMePlz = createThisInYourExtendedClass();

    }

    public abstract AbstractObject createThisInYourExtendedClass();
} 

the abstractObject:

public abstract class AbstractObject {
   // ... some fields/methods that all subclasses need 
}

What I want is to be able to use the extended field in the extended class without casting:

public class ExtendParent extends AbstractParent {
     // .. some unique fields
     public ExtendParent(){
       super();
     }


   public ConcreteObject createThisInYourExtendedClass(){
        return new ConcreteObject();
   }

   // what I want to do - no cast
   public void doSomethingWithSubMePlzWithoutCastingIt() {
        System.out.println(this.subMePlz);  
   }

   // what I end up doing - gotta cast
   public void doSomethingWithSubMePlzWithoutCastingIt() {
        System.out.println((ConcreteObject)this.subMePlz);  
   }
}

Would needing a comparator change how I should implement this? – I’m thinking a generic comparator for a list of the AbstractObjects that could be used by its subclasses.

  • 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-02T15:14:46+00:00Added an answer on June 2, 2026 at 3:14 pm

    It sounds like you need to make it generic:

    public abstract class AbstractParent<T extends AbstractObject> {
        protected T subMePlz;
        // ... some fields that all subclasses need
        public AbstractParent() {
            this.subMePlz = createThisInYourExtendedClass();
    
        }
    
        public abstract T createThisInYourExtendedClass();
    } 
    
    public class ExtendParent extends AbstractParent<ConcreteObject> {
        ...
    }
    

    Note that calling non-private methods within a constructor is usually a bad idea – the subclass won’t have been fully initialized yet, which can make it difficult to reason about how much you can really rely on.

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

Sidebar

Related Questions

One of my base repository classes contains a method: public abstract class RepositoryBase<T, TDb>
I am trying to create a class in VB.NET which inherits a base abstract
I have a stateless, abstract base class from which various concrete classes inherit. Some
In my application I have a mapping like this @MappedSuperclass public abstract class Base
I have an abstract class — let's name it Base . This class contains
I would like to use a base message class like: [Serializable] public abstract class
I've created my own Razor view class, thus: public abstract class MyWebViewPage : System.Web.Mvc.WebViewPage
In a C# program, I have an abstract base class with a static Create
I have a collection of classes that inherit from an abstract class I created.
Created BroadcastReceiver which displays application name and version number on install/ uninstall of any

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.