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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:54:18+00:00 2026-05-13T13:54:18+00:00

How do you deal with having only single inheritance in java? Here is my

  • 0

How do you deal with having only single inheritance in java? Here is my specific problem:

I have three (simplified) classes:

public abstract class AbstractWord{
    String kind;    // eg noun, verb, etc

    public String getKind(){ return kind; }

}

public class Word extends AbstractWord{
    public final String word;

    ctor...

    public void setKind(){
        // based on the variable word calculate kind..
    }
}

public class WordDescriptor extends AbstractWord{

    ctor..

    public void setKind(String kind){this.kind = kind;}

}

This is what I consider my most basic implementation but I want to make other implementations.

Lets say that I want to add a new variable say wordLength but I want to add it using inheritance. Meaning I do NOT want modify that original AbstractWord class. Ie something along the lines of this:

public class Length{
    private int length;

    public int getLength(){return length};
}

public class BetterWord extends AbstractWord AND Length{

    public void setLength(){
        // based on the variable word calculate Length..
    }
}

public class BetterWordDescriptor extends AbstractWord AND length{

    public void setLength(int length){this.length = length;}
}

I know that java does not let me do this but it has made my code very ugly. Right now whenever I add a field I am just adding it to AbstractWord but then I either need to rename that AbstractWord (and Word and WordDescriptor). (I can’t just add the field to the other one because of backwards compatibility, it break equals methods and stuff like that).

This seems like a pretty common design issue but I have racked my head and I cannot come up with any beautiful solutions.

Is there a design pattern that addresses this? I have some potential solutions but I wanted to see if there was something that I was missing.

thanks,
Jake

Update: Length refers to the number of syllables in the word (sorry about the lack of clarity)

  • 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-13T13:54:18+00:00Added an answer on May 13, 2026 at 1:54 pm

    Favor composition over inheritance.

    Solution takes into consideration that there could be another type of word that may need WordLengthSupport.

    Similarly other interfaces could be created and implemented and various word types can have mix and match of those interfaces.

    .

    public class WordLength {
        private int length = 0;
        public int getLength(){return length};
        public void setLength(int length){this.length = length};
    }
    

    .

    public interface WordLengthSupport {
        public WordLength getWordLength();
    }
    

    .

    public class BetterWord extends AbstractWord 
            implements WordLengthSupport {
        WordLength wordLength;
        public WordLength getWordLength() {
            if(wordLength==null) {
                // each time word changes 
                // make sure to set wordLength to null
                calculateWordLength(); 
            }
            return wordLength;
        }
        private void calculateWordLength() {
            // This method should be 
            //    called in constructor 
            //    or each time word changes 
            int length = // based on the variable word calculate Length..
            this.wordLength = new WordLength();
            this.wordLength.setLength(length);
        }
    }
    

    .

    public class BetterWordDescriptor extends AbstractWord  
            implements WordLengthSupport {
        WordLength wordLength;
        public WordLength getWordLength(return wordLength);
        public void setWordLength(WordLength wordLength) {
            // Use this to populate WordLength of respective word
            this.wordLength = wordLength;
        }
    }
    

    .

    The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

    This solution does not use strategy pattern but can be refactored for same.

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

Sidebar

Ask A Question

Stats

  • Questions 307k
  • Answers 307k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think I got some insight into the problem. When… May 13, 2026 at 9:29 pm
  • Editorial Team
    Editorial Team added an answer As far as NHibernate is concerned, this is just a… May 13, 2026 at 9:29 pm
  • Editorial Team
    Editorial Team added an answer _subLevelMeters is an NSArray that contains a number of LevelMeter… May 13, 2026 at 9:29 pm

Related Questions

Why are circular references in Visual Studio a bad practice? First, I will describe
i've been making web app's and working with various server side language like php,
I am working with couple of friends on an ASP.NET MVC website. The project
Steve Yegge's wisdom notwithstanding, most developers are faced with requirements which were gathered from
I'm working on a project which is just about to start, and since I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.