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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:41:12+00:00 2026-05-25T12:41:12+00:00

I have an abstract superclass with a property that must be set by the

  • 0

I have an abstract superclass with a property that must be set by the subclass for proper operation of instances…

abstract SuperClass {
  // this list must be initialized by the subclass
  List<? extends SavedSearch> savedSearches;
}

This abstract superclass has methods that operate on the savedSearches but it’s up to the subclass to ensure that list is properly initialized.

What’s the best way to do this?

It’s easy with Java create superclass methods that must be implemented by subclasses, but not properties. I thought about requiring an abstract initSavedSearch method like this in the superclass…

abstract initSavedSearchList();

… but that seemed strange, the subclass implementer is under no obligation to do anything in their method implementation, they could just do nothing.

I know I can code up some runtime checks to make sure the savedSearch list is init’d before running code in the superclass, but I’m hoping for compile-time checks (akin to to abstract methods) not runtime checks.

I saw a C# answer to a similar question which suggests hiding the default (zero-arg) constructor and creating a constructor in the abstract class that requires the list as an arg. That sounds decent for Java, too. But my particular class is a JSF 1.x managed bean. I’m a JSF newbie, but AFAIK there’s no easy way to get JSF to call a non-standard constructor for Managed Beans.

Is there a way to do this where I get some compile-time help to ensure that implementing subclasses are properly initializing a superclass property?

Thanks!

  • Gary
  • 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-25T12:41:12+00:00Added an answer on May 25, 2026 at 12:41 pm

    One of the ways is to provide a constructor which takes that as argument.

    public abstract class SuperClass {
    
        private List<SavedSearch> savedSearches;
    
        protected SuperClass(List<SavedSearch> savedSearches) {
            this.savedSearches = savedSearches;
            // You can do additional checks here on null, size(), etc.
        }
    
    }
    

    The subclasser is required to invoke that constructor.

    public class SubClass extends SuperClass {
    
        public SubClass() {
            super(initAndGetSavedSearchesSomehow());
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract superclass and various subclasses. Each subclass contains a value that
So say I have a subclass that extends a superclass. In what scenarios do
Within my code a have the following abstract superclass public abstract class AbstractClass<Type extends
I have a class with a subclass. The superclass has a Position property. The
I have an abstract superclass that has JPA annotations on it mapping some of
Hi is it possible to cast a List? i have an abstract class that
I have some Entities that look like this: @Entity public abstract class AbstractEntity{ @Id
Let say I have abstract class called: Tenant and Customer. The tenant in this
I have an abstract superclass. abstract class SuperClass{ public static function loadInstanceFromText(){ $instance =
I've defined an abstract superclass in one file and a subclass in another. I

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.