2

I am trying to write some Deterministic Finite Automaton related algorithms. So the obvious start is to define the entities: DFA, State and Edges. However, I though since the DFA is a kind of graph I should have basic classes Graph, Node and Edge each implementing IGraph, INode and IEdge. And then have DFA and State extend Graph and Node.

So the stripped down code of Graph is:

class Graph { //... public void addNode(INode node) { //... } //... } 

Now the main difference between State and Node is that State adds properties isFinalState and isStartingState to Node. So I want State objects to be added to the DFA not the Node objects through Graph's addNode() method.

I thought I can override and make this method private in DFA and have defined a new method addState() which will accept State objects. But reducing the visibility of parent class members in derived class is not allowed in Java.

So what is the standard solution / approach followed in such scenarios. Or am I thinking this all wrong?

4
  • "I thought I can override and make this method private in DFA". In Java, the access level cannot be more restrictive than the overridden method's access level. What this means is that you can't override a public method and change its access specifier to private. Commented Mar 7, 2015 at 12:48
  • yep I know that, the point is how should I go with the situation. I hope my requirement is genuine and am not messing up with the OO basics. Commented Mar 7, 2015 at 12:54
  • It's a bit difficult to comment on anything at the moment since your question is not clear. Put down the skeleton for each class that you plan to create and some basic methods you plan to add to them into the quesiton to make it more clear. Commented Mar 7, 2015 at 13:03
  • I don't think you're thinking about it wrong, but I would probably not model State unless its existence was fundamental to the problem being solved. At some point, we split frog hairs with unnecessary specialization and accomplish very little. I would put isStartingState and isFinalState on Node and call it a fine day. Commented Mar 7, 2015 at 13:04

2 Answers 2

1

If I get your example correctly, you can achieve that by Generics:

class Graph<T extends Node> { public void addNode(T node) { ... } } class DFA extends Graph<State> { @Override public void addNode(State node) { ... } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Or perhaps class Graph<T extends Node>
0

If your DFA class extends Graph, then it must maintain Graph's contract. So, if you need to reduce the visibility of the method (not possible in Java as you say), you need to break the Graph's contract for DFAand thus, DFA should not be a child of Graph.

Another way to say it is that as DFA cannot have Nodes, then DFA is not a Graph.

Your solution could be that DFA doesn't extend Graph anymore (maybe you need a IDFA inteface?). In this case, your DFA class can keep a Graph as a field.

Also, thinking about what a Deterministic Finite Automaton is, it is not a graph, it's a state machine that can be represented using a graph.

3 Comments

Now thats interesting: DFA is not a graph, it has a graph
Edited: DFA is not a graph, it's a state machine that can be represented using a graph. I'm not a computer scientist, but I think it's correct
Ok thats correct sure 100%. DFA is a concept that can be represented by graph. And can also be represented by transitions tables. But I dont know if represented by Graph can also be termed as contain Graph

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.