public abstract ClassName
{
// methods
// setters & getters...???
}
public abstract ClassName { // methods // setters & getters…??? }
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The short answer: yes:
More extended answer (I’m not sure that it’s actual for you right now):
You should realize that these methods (if they are not declared with
finalmodifier) can be overriden in subclasses. That’s why it’s important to think about methods contracts and document them carefully. If one of the methods of your abstract class invokes another its method then this fact should also be documented.All protected and public methods (and constants, and in very very seldom cases – protected fields) of your abstract class become a part of your API. Thus, other developers can use them and it’s not very easy to change them.
About best practices:
1) Think twice before making your class
Serializable– this will make all subclasses searializable too and (if you want to produce not potentially broken program) you should care about possible serialization of any of the subclasses.2) The same is actual for implementing
Cloneable(do not better implement it in a top-level abstract class).3) You should provide correct
equalsandhashCodeimplementation taking into account that 2 subclasses can be compared with your implementation from the abstract class.4) Think about providing natural ordering (i.e. implementing
Comparableinterface). It’s often a good idea if there is a real natural ordering exists.