1

I hava a class called QGNode that extends GNode and a class QGraph that extends Graph. In QGNode constructor when I call super constructor with an argument ArrayList<QGNode> an error occurs. Am I misunderstand inheritance?

Here is my code:

0 public class QuestionGraph extends Graph{    
1
2   public QuestionGraph(ArrayList<QGNode> nodes) {
3      super(nodes);  
4  }
5 }

1 public class Graph {
2
3 ArrayList<GNode> nodes;
4
5  public Graph(ArrayList<GNode> nodes) {
6      this.nodes = nodes;
7 }
8 }

error occurs at line 3 and it's a syntax error and IDE suggest : create method (java.Util.Arraylist) in QuestionGraph

user3070752
  • 694
  • 4
  • 23
  • please add your code and error that it gives you! you include some other part of code here – Lrrr Jun 20 '15 at 07:25
  • 3
    Firstly, *always* specify what the error is. "an error occurs" isn't nearly as useful as "Line X causes error Y" (specifying X and Y). Secondly, I would really avoid using names like "GNode" and "QGNode". Thirdly, this is a dupe - I'll find the relevant duplicate... – Jon Skeet Jun 20 '15 at 07:26
  • thanks @jon . errors occur in calling super(nodes) and it doesn't recognize any suitable constructor for that given argument. those names are abbreviation of QuestionGraphNode and AnswerGraphNode – user3070752 Jun 20 '15 at 07:32
  • 1
    No, don't try to summarize the error - you copy paste it directly into the question. And I'd avoid abbreviations when they make it harder to distinguish between things at a glance. – Jon Skeet Jun 20 '15 at 07:40
  • @amirveyseh As Jon explains user should be able to get at a glance what actual problem is without going through the code and finding out the problem for himself. Pasting the error or providing screenshot of IDE error would help a lot. – Narendra Pathai Jun 20 '15 at 07:45
  • However my problem was solved by answers given here and the previous same question but I have put the error. – user3070752 Jun 20 '15 at 07:50

1 Answers1

4

Inheritance in Generics works a bit differently. In Java ArrayList<QGNode> is not a subtype of ArrayList<GNode>.

Generics in Java and Inheritance

Inheritance and generics from Oracle documentation

How it works from Oracle documentation

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
  • @amirveyseh First you should get the hang of how generics work by understanding the material in Oracle documentation. _Hint You will have to generify the class itself and use extends i.e. Upper bound._ Notify me back if you have further queries. – Narendra Pathai Jun 20 '15 at 07:37
  • thanks @Narendra Oracle website is banned in my country so I couldn't check it. How ever my problem was salved – user3070752 Jun 20 '15 at 07:56