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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:32:37+00:00 2026-05-27T22:32:37+00:00

I have a class ParentClass() , some child-classes ChildClass1() , ChildClass2() , etc. and

  • 0

I have a class ParentClass(), some child-classes ChildClass1(), ChildClass2(), etc. and an interface iChild(). The ChildClass() inherits from the ParentClass() and implements iChild():

class ChildClass1 
    extends ParentClass 
    implements iChild {}

class ChildClass2 
    extends ParentClass 
    implements iChild {}

In the iChild()-Interface, I would like to require, that some constants need to be filled (which are mainly identifiers and some functionality definitions). I have not found any meaning to do so, as constants may only be defined in the interface, but which are not redefinable in the implementing classes. The only workaround I found is to define some getter-functions in iChild(), which in turn return the necessary values, but that does not seem to be the right way to do. As I already extend from the ParentClass(), I also cannot use an abstract class to e.g. check the definition of the required constants in all ChildClasses.

Any ideas on how to tackle this problem?


Update: To answer a comment – with leaner and more readable I mean to following:

class ChildClass1 {
    const MY_NAME = "Foo Bar Name 1";
}

-- vs --

class ChildClass1 {
    public function GetMyName() {
        return "Foo Bar Name 1";
    }
}

I find the first example a lot more readable and understandable than the second. Unfortunatly, I currently do not have a way to enforce MY_NAME to be set by the ChildClasses.

  • 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-27T22:32:38+00:00Added an answer on May 27, 2026 at 10:32 pm

    I’m not sure that the proper solution here is for a constant. The very definition of a constant is that it does not change. If each implementation of iChild needs to define the constant it isn’t really a constant, it is a variable. I suggest adding in the appropriate getter methods, communicating to the user that they need to supply this information in their implementation.


    Ok, I did a little testing and it simply is not possible for a class to override a constant set by an interface it is implementing or a parent class it is extending. Class constants appear to be checked at compile time and a fatal error is thrown if you attempt to override it. For what you are trying to do the appropriate answer is providing getter methods in your interface.

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

Sidebar

Related Questions

Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
I have a MustInherit Parent class with two Child classes which Inherit from the
Let's have the following class hierarchy: public class ParentClass implements SomeInterface { } public
I have a parent class and child class (inherited from parent). In the child
In Django, when you have a parent class and multiple child classes that inherit
I have a class called Entity, with two child classes: Creature and Item. (I'm
I have a collection of classes that inherit from an abstract class I created.
I have an parent class, and several child classes. What I want is when
I have two classes with a parent-child relationship (the Parent class has-a Child class),
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS

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.