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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:33:53+00:00 2026-06-09T11:33:53+00:00

After reading a number of questions regarding abstract classes and interfaces, I am still

  • 0

After reading a number of questions regarding abstract classes and interfaces, I am still unsure about how I should go about designing my code, so at the risk of having missed a possible duplicate, here’s my situation.

I am working on a number of utility classes that are meant to handle a nasty hierarchy of a particular type of object. While the precise details of the object is irrelevant, it should suffice to say that it’s not a general solution whatsoever. So I started by writing a CustomNode and a CustomHierarchy class.

A bit later on I realized that it’s much smarter to produce a more general solution, which can be reused in different context and easily tested without having to create lots of mock instances of the aforementioned complicated class. I could easily provide all the common code in interfaces and abstract classes, and just provide the specific bits where they are needed! (this I believe is the whole point with OOP?)

So far I have:

  • an interface Relatable which simply holds an enum of possible relations that nodes might have to one another, and a method signature of getRelation(Relatable other)

  • an abstract class Node<E> which implements the Relatable and contains all utility methods (e.g. addChild() or getNextSibling(). The only method this class is missing is the getRelation(Relatable other).

  • a concrete class Hierarchy<E> which will define how to build up the hierarchy of the nodes, starting from a Collection<E> which holds the data.

The idea is to extend the Node<E> and Hierarchy<E> classes with appropriate subclasses based on the problem at hand. The first two are done without any major hustle but I am having problems with the Hierarchy<E> class since it has a private method addNodeToHierarchy(E data) which tries to instantiate a Node<E> and place it in the hierarchy. This obviously fails as the node class is abstract and can’t be instantiated.

I understand why this is not working, but I am not sure how I should go about circumventing this problem. Is there a flaw in my design? Should I add a static createNewNode(E data) method in the Node<E> and use that instead? Another option I can think of is to detach the Node from getting the relation and go with a Relator interface instead. This can’t be a unique problem by any chance, so what’s “good practice” in this situation?

Thanks in advance,

  • 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-09T11:33:55+00:00Added an answer on June 9, 2026 at 11:33 am

    Decide who should know how a Node<E> must be constructed from from an instance of E. It could be the instance of E, it could be the Node class, it could be the Hierarchy, or it could be the caller of addNodeToHierarchy().

    If it’s the caller of addNodeToHierarchy(), then either you make it take an instance of Node<E> instead of an instance of E, or you make it take an instance of some NodeFactory<E>, to which the method will delegate to transform the data into a node. This factory could also be passed as an argument to the hierarchy constructor if all the nodes are constructed by the same factory.

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

Sidebar

Related Questions

After reading alot about VTables, I still have one unanswered question. Given the next
After much reading it seems that, really, the only way to read a number
After reading MSDN-XAML Namespaces and MSDN-Understanding XAML Namespaces , I still do not understand
after reading the cookbook and various Q&A here I am still a bit confused
Even after reading the standard documentation, I still can't understand how Ruby's Array#pack and
After reading about at least the first 3 or 4 chapters of about 4
After a number of hours reading stackoverflow and watching railscasts, I've decided to post.
After reading another question about the use of macros, I wondered: What are they
After reading monkeytalk faq from http://www.gorillalogic.com/testing-tools/monkeytalk/documentation/monkeytalk-faq : How does it all work? MonkeyTalk is
After reading answer to this question: Make "make" default to "make -j 8" 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.