Advertisement
Guest User

asdfasdf

a guest
Jan 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. public class HelloWorld{
  2.  
  3.      public static void main(String []args){
  4.         CD theCD = new CD("Katy Perry - Dab", 5);
  5.         theCD.printTitle();
  6.         theCD.printPages();
  7.        
  8.         Library lib = new Library();
  9.         lib.setItem(theCD);
  10.         lib.printTitle();
  11.        
  12.         CD cd2 = (CD) lib.getItem();
  13.         cd2.printPages();
  14.      }
  15. }
  16.  
  17. class LibraryItem {
  18.    
  19.     private String title;
  20.    
  21.     public LibraryItem(String title){
  22.         this.title = title;
  23.     }
  24.    
  25.     public void printTitle(){
  26.         System.out.println(this.title);
  27.     }
  28. }
  29.  
  30. class CD extends LibraryItem {
  31.    
  32.     private int pages;
  33.    
  34.     public CD (String title, int pages){
  35.         super(title);
  36.         this.pages = pages;
  37.     }
  38.    
  39.     public void printPages(){
  40.         System.out.println(this.pages);
  41.     }
  42.    
  43. }
  44.  
  45. class Library {
  46.    
  47.     LibraryItem item = null;
  48.    
  49.     public void setItem(LibraryItem item){
  50.         this.item = item;
  51.     }
  52.    
  53.     public void printTitle(){
  54.         this.item.printTitle();
  55.     }
  56.    
  57.     public LibraryItem getItem(){
  58.         return this.item;
  59.     }
  60.    
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement