Skip to main content

Create object of generic type

No replies
vvsh
Offline
Joined: 2012-01-30
Points: 0

Hi.

There is an abstract class GraphRepresent:

public abstract class GraphRepresent {}



and several classes

public class AdjacencyMatrix extends GraphRepresent {}

public class IncidenceMatrix extends GraphRepresent {}



Graph is a class of type G, which is derived fromGraphRepresent:

public class Graph <G extends GraphRepresent> extends GraphData <G> {

G represent;



public Graph (GraphRepresent represent) throws Exception {

this.represent = represent;

}

}



to create an account to write this:

Graph <?> Graph = new Graph <AdjacencyMatrix> (new AdjacencyMatrix ());



it looks very cumbersome

Is there a way to create an object of type G directly in the class constructor Graph?



p.s. it must have in order to prevent change the object class that inherits from GraphRepresent.



sorry for my bad English.