I have a hierarchy of classes:
class Page {
}
class ChildPage1 extends Page {
private static final String URL;
}
class ChildPage2 extends Page {
private final String url;
}
Some pages have static urls but other don't. I need some methods like load()
in parent class Page
that will use url of it's descendant. I thought about implementing it in these ways:
Is it worth to not follow naming conventions in this case?
You can solve your problem by adding an abstract method getUrl()
to Page
that has to be implemented in ChildPage1
and ChildPage2
.
class Page {
protected abstract String getUrl();
}
class ChildPage1 extends Page {
private static final String URL;
protected String getUrl() {
return URL;
}
}
class ChildPage2 extends Page {
private final String url;
protected String getUrl() {
return url;
}
}