Social Network app · Inheritance · Polymorphism · Overriding
In this step, you will create version 3 of the Network system. This version will have a menu that will control the NewsFeed class.
In IntelliJ, create a new project called Network-V3.
Copy the following classes into this new project (note they have the System.out.print statements removed from them and instead have returned the Strings).
Create a new class, called Driver. This class will contain the main() method. In this class:
write a private method, called run(). This method should call the mainMenu() method and use a switch statment to process the user's input. Hint: write a private method for each case i.e. one that creates a MessagePost, one that creates a PhotoPost and then one that adds to the posts collection.
The run() method should be called from the main method.
Test the program by doing the following:
Are the posts printed in the order we entered them?
Are all the details that we entered, displayed when we "List all posts"? Why do you think details are missing? Hint: Investigate the code in Post, PhotoPost and MessagePost that displays the post. Note: we will fix this is the next step.
The solution to network-v3 is here.
In this step, you will build on v3 that you wrote in the previous step.
Create a new project called Network-V4 and copy the src files from either your solution to network-v3 or our solution of Network-V3 into it.
In v3, details that we entered via the menu were NOT displayed back to us when we listed all posts.
The data is missing because the display method was written in the superclass, Post. This superclass has no knowledge of its subclasses. Therefore, it has no knowledge of the message field in MessagePost, or the filename and caption fields in the PhotoPost class.
To fix this issue, you need to also write a display method in the subclasses, MessagePost and PhotoPost. This display method should:
Test your code again. Are all details printed to the console?
The solution is available here
These exercises are based on the exercises in Chapter 8 of Objects First with Java - A Practical Introduction Using Bluej, by David Barnes & Michael Kolling).
Order the following items into an inheritance hierarchy :
Apple
IceCream
Bread
Fruit
FoodItem
Cereal
Dessert
ChocolateMousse
Baguette
Assume that we have four classes :
Person
Teacher
Student
PhDStudent
Teacher and Student are both subclasses of Person. PhDStudent is a subclass of Student.
Person p1 = new Student();
Person p2 = new PhDStudent ();
PhDStudent phd1 = new Student();
Teacher t1 = new Person();
Student s1 = new PhDStudent ();
Given the Person hierarchy from Exercise 2, suppose that we have the following legal declarations and assignments:
Person p1 = new Person();
Person p2 = new Person ();
PhDStudent phd1 = new PhDStudent();
Teacher t1 = new Teacher();
Student s1 = new Student ();
s1 = p1;
s1 = p2;
p1 = s1;
t1 = s1;
s1 = phd1;
phd1 = s1;
The question was...order the following items into an inheritance hierarchy :
apple
ice-cream
bread
fruit
food-item
cereal
dessert
chocolate mousse
baguette
The answer is:
In the Person hierarchy, Teacher and Student are both subclasses of Person. PhDStudent is a subclass of Student.
Person p1 = new Student(); // This is legal because a Student "is a" Person.
// We are following the direction of the arrows in the
// inheritance diagram.
Person p2 = new PhDStudent (); // This is legal because a PhDStudent "is a" Person.
// We are following the direction of the arrows in the
// inheritance diagram.
PhDStudent phd1 = new Student(); // This is NOT legal because a Student "may not be"
// a PhDStudent. We are going against the direction
// of the arrows in the inheritance diagram.
Teacher t1 = new Person(); // This is NOT legal because a Person "may not be"
// a Teacher. For example, the Person could be a Student.
// We are going against the arrows in the
// inheritance diagram.
Student s1 = new PhDStudent(); //This is legal because a PhDStudent "is a" Student.
// We are following the arrows in the inheritance diagram.
Given the Person hierarchy from Exercise 2:
...suppose that we have the following legal declarations and assignments:
Person p1 = new Person();
Person p2 = new Person ();
PhDStudent phd1 = new PhDStudent();
Teacher t1 = new Teacher();
Student s1 = new Student ();
Based on these legal variable declarations, which of the following assignments are legal and why or why not?
s1 = p1; // This is NOT legal...a Person "may not be" a Student.
// We are going against the arrows in the inheritance diagram.
s1 = p2; // Again, this is NOT legal...a Person "may not be" a Student.
// We are going against the arrows in the inheritance diagram.
p1 = s1; // This is legal...a Student "is a" Person.
// We are following the direction of the arrows in the inheritance diagram.
t1 = s1; // This is NOT legal...there is no relationship between a Teacher and a
// Student in our inheritance diagram.
s1 = phd1; // This is legal...a PhDStudent "is a" Student.
// We are following the direction of the arrows in the inheritance diagram.
phd1 = s1; // However, this is NOT legal...a Student "may not be" a PhDStudent.
// We are going against the direction of the arrows in the
// inheritance diagram.