0

The following code is a small part of a class called GreedyGraph, which extends the class Graph.

public class GreedyGraph extends Graph {
    private final boolean DEBUG=false;
    private GreedyPriorityQueue q;
    private int components;

// constructor
    public GreedyGraph(String name) throws java.io.IOException {
        process_header(name);
        add_vertices();
        add_edges();
    }

My main class, ShortestPath, contains the following code:

public class ShortestPath extends GreedyGraph {

    public ShortestPath(String name) throws IOException  {
        super(name);
    }

Of course, there is a lot more to this project than just these lines of code. When I try to run it, the following error code occurs:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Implicit super constructor GreedyGraph() is undefined. Must explicitly invoke another constructor

Shouldn't this not be happening?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
YoyoDevo
  • 1
  • 1
  • What constructors **ARE** available to you? What library holds the Graph class? Or is it your own class? You need to check out somehow all the constructors that the Graph class has, and then call the appropriate `super(...)` within your own class's constructor. – Hovercraft Full Of Eels Nov 30 '15 at 22:24
  • In the class GreedyGraph, public GreedyGraph(String name) is the only constructor – YoyoDevo Nov 30 '15 at 22:26
  • No, you misunderstand, and that is not important. What is important is what constructors are available in the **super** class, the Graph class, a class not available to us. – Hovercraft Full Of Eels Nov 30 '15 at 22:26
  • In the Graph class, the constructors are public Graph(String name) throws IOException { process_header(name); add_vertices(); add_edges(); } public Graph(String name, int order, int size, boolean directed, boolean weighted) { this.name=name; this.order=order; this.size=size; this.directed=directed; this.weighted=weighted; add_vertices(); } – YoyoDevo Nov 30 '15 at 22:29
  • sorry I don't know how to format comments correctly here – YoyoDevo Nov 30 '15 at 22:29
  • So then what you must do -- call the `super(name)` constructor in your own constructor. Please read the link to the similar question that I used to close this duplicate. And it's usually a good idea to search for duplicates *before* asking since this is a common question, one that we don't need to have here yet again when viable answers are available. – Hovercraft Full Of Eels Nov 30 '15 at 22:31
  • 1
    That's a little odd. `Implicit super constructor GreedyGraph()` would mean that a subclass of `GreedyGraph` calls `super()`. When I reproduce this scenario I get `Implicit super constructor Graph() is undefined.` (which actually seems to be the case here) – zapl Nov 30 '15 at 22:34
  • @zapl: good eyes. I'm betting it's a typo. – Hovercraft Full Of Eels Nov 30 '15 at 22:45
  • hmm I'll check every line of code now. No errors come up in Eclipse until after I run it but there might still be a typo. – YoyoDevo Nov 30 '15 at 22:52
  • nothing in GreedyGraph calls super() – YoyoDevo Nov 30 '15 at 22:53
  • I figured it out. It was some issue with Eclipse. I had to "clean project" and it fixed whatever was wrong. I don't know why it did that. I'd appreciate if it you actually read my code before linking a previous answer. You think I didn't search for my question before posting? My code was correct and I was confused why it wasn't working. – YoyoDevo Nov 30 '15 at 23:02
  • `"nothing in GreedyGraph calls super()"` -- You're wrong. A constructor always calls the super constructor either explicitly or implicitly, and here it's implicitly, and that's why you're not seeing it. Please read the tutorials on constructors again if need be. – Hovercraft Full Of Eels Nov 30 '15 at 23:02
  • my code works perfectly. I can send you a pastebin of my entire project if you don't believe me. – YoyoDevo Nov 30 '15 at 23:05
  • "You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor." – Hovercraft Full Of Eels Nov 30 '15 at 23:05
  • per the [Java Constructor Tutorial](http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html). – Hovercraft Full Of Eels Nov 30 '15 at 23:05
  • My class ShortestPath, which extends GreedyGraph, has a constructor but not a no argument constructor. The class GreedyGraph, which extends Graph, has an argument constructor, but no no-argument constructor. The class Graph has a no argument constructor as well as 2 argument constructors. What is the problem? Is it that GreedyGraph and ShortestPath don't have no-argument constructors? – YoyoDevo Nov 30 '15 at 23:08

0 Answers0