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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:20:18+00:00 2026-05-18T07:20:18+00:00

How can I have a generic abstract class, let’s say Animal, and have it

  • 0

How can I have a generic abstract class, let’s say Animal, and have it implement a constant (or a variable; it doesn’t mind) which must be redefined by all subclasses?

Example:

abstract class Animal {
    private static int venerableAge;
}

And force Dog to be defined something like

class Dog extends Animal {
    private static int venerableAge = 10;
}

I don’t want different subclasses to be able to read nor write each others’ value. Just the one of themselves.

I.e., each class must have its own static “instance” of the variable. And no one will access the parent’s.

Is that possible in Java?

  • 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-18T07:20:19+00:00Added an answer on May 18, 2026 at 7:20 am

    The trick is to do this with getter methods rather than directly with fields:

    abstract class Animal {
       abstract int getVenerableAge();
    }
    
    class Dog extends Animal {
       private static int venerableAge = 10;
    
       public int getVenerableAge() {
          return venerableAge;
       }
    }
    

    EDIT:
    How about letting the constructor do the contract binding:

    abstract class Animal {
        public Animal(int venerableAge){
           //carry out sense checks here. i.e.:
           if (venerableAge < 0) { /* doSomething */ }
        }
    }
    
    class Dog extends Animal {
       private static int venerableAge;
    
       public Dog(int age) {
          super(age)
          venerableAge = age;
       }
    }
    

    Now Dog and Cat are both forced to be created with venerable ages, but (as implemented above) can’t see each others value.

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

Sidebar

Related Questions

Let's say I have a Java class abstract class Base { abstract void init();
Let's say I have an abstract parent class called shape, and that there are
I have a generic abstract UserControl class, SensorControl , which I want all my
I have generic list which must be a preserved order so I can retrieve
I've read posts about why you can't have a (Edit -- generic) (which use
I have made a generic Observer interface and an Observable class, but can't compile
How can I invoke overloaded generic method in IronRuby? I have a .NET class
I have a Base class that needs a Generic type. That can be either
I have confusing situation. Base Generic Type and successor public abstract class BaseType<TEntity> :
I have a abstract generic class. I want to define a method inside there

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.