Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyClass {
- public static void main(String[] args) {
- User michael = new User("Michael King", "supersecret");
- User pande = new User("Pandetta Bell", "Pandetta Bell");
- michael.login();
- MessengerClient client1 = new FacebookMessenger(michael);
- MessengerClient client2 = new TwitterMessenger(pande);
- client1.publish("Good morning login want my beauxbatons");
- client2.publish("Yeah no. Wont happen");
- }
- }
- public class User {
- private String username, password;
- private boolean loggedIn = false;
- public User(String username, String password) {
- this.username = username;
- this.password = password;
- }
- public String getUsername() {
- return username;
- }
- public String getPassword() {
- return password;
- }
- public boolean isLoggedIn() {
- return loggedIn;
- }
- public void login() {
- this.loggedIn = true;
- }
- }
- public abstract class MessengerClient {
- private User user;
- public MessengerClient(User user) {
- this.user = user;
- }
- public User getUser() {
- return user;
- }
- public abstract void publish(String message);
- }
- public class TwitterMessenger extends MessengerClient {
- public TwitterMessenger(User user) {
- super(user);
- }
- @Override
- public void publish(String message) {
- if(!getUser().isLoggedIn()) return;
- System.out.println("Twitter >> " + getUser().getUsername() + " tweeted " + message);
- }
- }
- public class FacebookMessenger extends MessengerClient {
- public FacebookMessenger(User user) {
- super(user);
- }
- @Override
- public void publish(String message) {
- if(!getUser().isLoggedIn()) return;
- System.out.println("Facebook>> " + getUser().getUsername() + " posted something new to his timeline: " + message);
- }
- }
- Homework. Topic: Abstract super classes
- 1. What does this program print on execute (this is a tricky one)
- 2. What does the keyword abstract mean (google wooo)
- 3. Both client1 and client2 are stored in a Variable of the type MessengerClient (compare ln 11 and 12) They still produce different prefixes. How come so ?
- 4. What is the java keyword "super" (google some more o.O its the last time i swear)
- 5. Describe the User type as well as each of its method in the context. (What would it be used for. What does it represent? What do the methods do)
- 6. Write another MessengerClient, this time for Instagramm.
- 7. What does line 7 do in this program. What are its effects
- 8. What would happen if we delete lines 7 What would the program print
Add Comment
Please, Sign In to add comment