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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:14:03+00:00 2026-05-17T18:14:03+00:00

I’m fairly new to Java, and I need some help figuring out a good

  • 0

I’m fairly new to Java, and I need some help figuring out a good class hierarchy and overall design for an assignment we’ve been given (i’m studying CS).

The assignment is graph-theory related, so we were asked to create interfaces for 2 types of graphs, simple graphs and multi-graphs (which are allowed to have parallel edges), and the corresponding implementation.

I came up with the following interface hierarchy:

* Element
    - Vertex
    - Edge
        + MultiEdge
* Graph
    - MultiGraph

And their corresponding implementations.
Now I really don’t want do discuss my actual implementation here, I’m just giving some examples which I had trouble with because of (at least I think so) the way I designed the whole think.

The whole thing worked pretty well until I needed to extend my Graph to have MultiGraph functionality. Here’s a code snippet from GraphImpl:

protected final List edges;

public Graph addEdge(Edge e) {
    List newEdges = new ArrayList<Edge>();
    newEdges.addAll(edges);
    newEdges.add(e);
    return new GraphImpl(vertices, newEdges);
}

As you can see, I store the graphs edges in a List<Edge> in my GraphImpl, and therefor, I have lots of those lists all over my implementation. Also, you can see that I return a new GraphImpl from addEdge, as GraphImpl is supposed to be immutable.

With this I ran into a lot of trouble when implementing MultiGraph, because here, I needed to exchange the List<Edge> for a List<MultiEdge>. But when I redefined the “edges” variable in MultiGraph, I think that the methods in GraphImpl were still accessing the list I defined in GraphImpl, so that edges wouldn’t get added if I called MultiGraph, until completely rewrote it for MultiGraph. But later I noticed that I would’ve had to rewrite it anyway, because addEdge in GraphImpl returns (naturally) a GraphImpl, but in MultiGraphImpl, I would’ve needed a MultiGraphImpl to be created.

What I am trying to understand is, how would you design and implement such a thing. What I have is a bunch of interfaces extending each other, and the same hierarchy of implementations, also extending each other.

The functionality of Graph is just a subset of MultiGraph, so everything that is being done in GraphImpl is mostly also valid for MultiGraphImpl. Right now, I needed to copy lots of code from GraphImpl to MultiGraphImpl, just to overcome type problems (which I, at least somehow, understand, but I wouldn’t know how to circumvent them).

I hope you’re not too confused by now, because I definitely am 😉 If I was unclear in any part, I’ll be happy to clarify, just point me towards whats missing.

  • 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-17T18:14:04+00:00Added an answer on May 17, 2026 at 6:14 pm

    Here is some very well-annotated Java source code implementing graph-theoretical objects and algorithms, hopefully relevant to your problem. (You probably want to start reading here.)

    Good luck!

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

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.