Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HelloWorld{
- public static void main(String []args){
- CD theCD = new CD("Katy Perry - Dab", 5);
- theCD.printTitle();
- theCD.printPages();
- Library lib = new Library();
- lib.setItem(theCD);
- lib.printTitle();
- CD cd2 = (CD) lib.getItem();
- cd2.printPages();
- }
- }
- class LibraryItem {
- private String title;
- public LibraryItem(String title){
- this.title = title;
- }
- public void printTitle(){
- System.out.println(this.title);
- }
- }
- class CD extends LibraryItem {
- private int pages;
- public CD (String title, int pages){
- super(title);
- this.pages = pages;
- }
- public void printPages(){
- System.out.println(this.pages);
- }
- }
- class Library {
- LibraryItem item = null;
- public void setItem(LibraryItem item){
- this.item = item;
- }
- public void printTitle(){
- this.item.printTitle();
- }
- public LibraryItem getItem(){
- return this.item;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement