-1

In my Graph class i extends a abstract class List(That extends comparable). I want to make object of list but don't want to implement (abstract)method in graph class.Whoever extends Graph must implement that method. How i can do this?

graph

public abstract class Graph<T> extends List<T>{

private  List<List<T>> adjacencyList;
private  int vertexNumber;
private boolean directed;


public Graph(int vertex,boolean directed)
{
    vertexNumber=vertex;
    this.directed=directed;
    adjacencyList= new List<List<T>>()// The problem is here compiler wants the implementation of the abstract method.
    createVertex(vertexNumber);

}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Shuvo
  • 47
  • 1
  • 11

4 Answers4

0

you are defining an abstract class extending a List so basically you have no more than 2 options;

  1. either implement the methods of the list
  2. or defined abstract to and the class that inherits the Graph will have to handle it...
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You can not create an instance of an abstract class, you must implement all methods.

However in your situation you just need to provide one of the existing implementations. Most likely an ArrayList

adjacencyList = new ArrayList<List<T>>();

If you look at the List there are several implementing classes listed.

Most common are: - ArrayList - LinkedList - Stack

Design Question: Why does your Graph extend List and then have a List of Lists? Typically you only do one of the following

  • Extend List, in this case your Graph is a list
  • List field, in this case your Graph has a list

It is confusing to have both relationships on one object.

cyroxis
  • 3,661
  • 22
  • 37
0

You could do this to force subclasses to provide a means to create the list:

public abstract class Graph<T> {
   private List<List<T>> x;

   public Graph(int vertex,boolean directed) {
      adjacencyList = createListOfList();
   }

   protected abstract List<List<T>> createListOfList();
}
FredK
  • 4,094
  • 1
  • 9
  • 11
0

since you have provide PRIVATE access modifier in :

private  List<List<T>> adjacencyList;
private  int vertexNumber;
private boolean directed;

so, even if you another class (suppose) A extends Graph, it would be able to access those variables. Try with public access specifier, it may help.

THANK YOU

Amritansh
  • 1
  • 1