Java Reference
In-Depth Information
defined in class
Post
, and the other a value for a field defined in class
MessagePost
. All of
these fields must be correctly initialized, and Code 8.4 shows the code segments that are used
to achieve this in Java.
Code 8.4
Initialization of subclass
and superclass fields
public
class
Post
{
private
String username;
// username of the post's author
private
long
timestamp;
private
int
likes;
private
ArrayList<String> comments;
/**
* Constructor for objects of class Post.
*
* @param author The username of the author of this post.
*/
public
Post(String author)
{
username = author;
timestamp = System.currentTimeMillis();
likes = 0;
comments =
new
ArrayList<String>();
}
// Methods omitted.
}
public
class
MessagePost
extends
Post
{
private
String message;
// an arbitrarily long, multi-line message
/**
* Constructor for objects of class MessagePost.
*
* @param author The username of the author of this post.
* @param text The text of this post.
*/
public
MessagePost(String author, String text)
{
super
(author);
message = text;
}
// Methods omitted.
}
Search WWH ::
Custom Search