java - subclasses common methods implemented in abstract superclass with different constructors -
i know has been discussed here have add question. need make abstract class called abstractgraph, , has extended 2 types of graph implementations: 1 uses matrix, other 1 uses lists.
so far have this:
abstract class abstractgraph implements graph { public void removealledges(){ //implementation here } }
and subclasses:
public graphmatrix(){ graphmatrix() { //implementation matrix type } } public graphlist(){ graphlist() { //different implementation lists type } }
both implementations have same removealledges()
method written same, guess has placed in abstract class. question is, how use references this
inside abstract class ? have fill implementations getters , setters ? if so, benefit of using single implementation of method 2 (or more) subclasses.
you can use same code removealledges long not depend on underlying structure (e.g., matrix or list) or functionality implemented encapsulated methods use underlying graph structure (e.g., getters, setters, , other common methods may need). in perspective, latter methods abstract methods of class, since need implemented according underlying graph structure.
one of benefits code reuse. write once, , use in every graph implementation. in case have 2 graph classes, may end having multiple representations graphs depending on application. writing general methods nice way of saving time spent in coding, , helps maintain code in more efficient , organized way. debugging easier!!
Comments
Post a Comment