With the posts from the first step, where
Message is an abstract class, which line does not compile?Post m = new Message("ben", "My cat learns Java", 7);Messageis abstract, sonew Message(...)is a compile error. Its constructor only runs throughsuper(...)in a subclass.Post v = new VideoPost("ben", "My cat learns Java", 7, 42);VideoPostis not abstract and gives its ownpreview, so it can be constructed.Message t = new TextPost("cse11", "Exam moved to Friday", 12, true);A variable can have an abstract class as its type. The object in it is aTextPost.
Still with the posts from the first step, and
Post v = new VideoPost("ben", "My cat learns Java", 7, 42);:v.snippet() runs the one in v.preview() runs the one in v.snippet() returns What happens with this class?
class Post {
final int likes;
Post(int likes) { this.likes = likes; }
void like() { this.likes = likes + 1; }
}- It does not compile, because of the line in
likeAfinalfield can only be assigned in the constructor. Java reportscannot assign a value to final variable.
likes - It does not compile, because of the line in the constructorAssigning a
finalfield inside the constructor is exactly what’s allowed; that’s how it gets its value. - It compiles, and
likeleaveslikesthe sameJava checksfinalwhen it compiles, so the program never gets to run.
With the Post, RepostCopy, and RepostRef classes from the sharing step:
Post p = new Post("joe", "CSE-PACE next Tuesday!", 22);
p.like();
RepostCopy copy = p.repostCopy("ana");
RepostRef ref = p.repostRef("ben");
p.like();
p.like();copy.likes is ref.original.likes is