ObjectMapper() - The object provided by Jackson to serialize using .writeValueAsString(object) and .readValue(json, object.class)
Note: For this to work, you need public getters for the attributes you want to serialize and a default construct, i.e. a constructor that doesn't take any parameters, to deserialize.
Serialization Practice (15 minutes)
You just got hired at an aquarium! Your task it to define a class for the Fish that will be serialized into a database.
The Fish object will store it's String species, String diet, int size, boolean saltwater. Please define the class so that it can be serialize and deserialized.
Also code a toString() function.
Aquarium Starter Code
import com.fasterxml.jackson.databind.ObjectMapper;
publicclassFish{
// Your code here.
}
ObjectMapper map = new ObjectMapper();
Fish nemo = new Fish("Clownfish", "Fecal Matter", 6, true);
Fish dory = new Fish("Regal Blue Tang", "Plankton", 12, true);
String json = map.writeValueAsString(new Fish(){nemo, dory});
Fish[] aquariumFish = map.readValue(json, Fish[].class);
for (int i = 0; i < aquariumFish.length; i++) {
System.out.println(aquariumFish[i]);
}
"Interfaces and Abstract, What's the Differences?"
Good question me!
In short, interfaces and abstract classes/methods allow you to specify certain functionalities that should exist, but provide no details on how to implement them, e.g. Shape class and .getSides(). It depends on the shape, but all shapes should have that ability.
publicabstractclassShape{ // Objects can *extend* abstract classes.publicabstractintgetSides();
}
interfaceShape{ // Objects can *implement* interfaces.publicintgetSides();
}
"There are differences though."
Both can have functions without bodies, not be declared alone, i.e. cannot declare an abstract class or interface.
interface can be implemented with multiple other interfaces, be implemented by classes, define static and final variables, only define public methods.
abstact classes can be extended by classes, have instance variables, have non-abstract methods with bodies, define both public and private methods.
Practice with Interfaces (15 minutes)
Implement these interfaces to the Mansion class.
Pool interface: swim() should print "Splash!", suntan() should print "Getting sun-kissed skin!".
Butler interface: callButler() should print "What may I help you with?".
Vineyard interface: getGrapeType() should return the grape types in the vineyard.
Remember that interfaces are by default public, but implementing them in a class requires you to explicitly state that they're public.
Abstract Classes and Methods Practice (10 minutes)
Make your own social media platform by extending the Social Media abstract class.
Here we have a vaguePost() method that returns a String for a vague post, definition. Another is postHumbleBrag() which will take a humble brag, definition, post and print it.
The last one, welcomeMessage(), you don't have to implement, just set a platform name to what your social media platform will be called.
Social Media Starter Code
publicabstractclassSocialMedia{
publicint followers;
publicint following;
public String platform;
publicabstract String vaguePost();
publicabstractvoidpostHumbleBrag(String post);
public String welcomeMessage(){
return"Welcome to " + this.platform + "!";
}
}
That's All Folks!
We covered a lot. Hopefully you understand the difference more between interfaces and abstract classes and methods. Feel free to ask about that here.
Have a good weekend. Enjoy the good weather, if you have it, and be sure to stay safe and have fun.