Assign3-oop
Assign3-oop
1. Core Abstraction
Answer:
Use an abstract class MediaItem to define common properties and enforce the play()
method for all media.
An abstract class is suitable because:
Code:
public abstract class MediaItem {
protected String id;
protected String title;
protected int duration; // seconds
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof MediaItem)) return false;
if (this.getClass() != obj.getClass()) return false;
@Override
public int hashCode() {
return id.hashCode() + title.hashCode();
}
}
Page 2 of 10
2. Optional Behaviors
Answer:
Use Downloadable and Rateables interfaces to define optional capabilities.
Interfaces avoid polluting Media Item with methods irrelevant to some subclasses.
Downloadable.java
public interface Downloadable {
void download();
}
Rateable.java
public interface Rateable {
void rate(int stars);
}
Subclasses:
Song.java
public class Song extends MediaItem implements Downloadable, Rateable {
private String artist;
@Override
public void play() {
System.out.println("Playing song by " + artist + ": " + title);
}
@Override
public void download() {
System.out.println("Downloading song: " + title);
}
@Override
public void rate(int stars) {
Page 3 of 10
System.out.println("Rated song '" + title + "' with " + stars + "
stars.");
}
}
Movie.java
public class Movie extends MediaItem implements Rateable {
private String director;
@Override
public void play() {
System.out.println("Playing movie directed by " + director + ": " +
title);
}
@Override
public void rate(int stars) {
System.out.println("Rated movie '" + title + "' with " + stars + "
stars.");
}
}
Podcast.java
public class Podcast extends MediaItem implements Downloadable {
private String host;
@Override
public void play() {
System.out.println("Streaming podcast by " + host + ": " + title);
}
@Override
public void download() {
System.out.println("Downloading podcast: " + title);
}
}
Page 4 of 10
4. Subtyping vs. Inheritance
Answer:
This allows behavior composition without forcing all MediaItem subclasses to implement
unrelated methods.
Playlist.java
import java.util.ArrayList;
import java.util.List;
Page 5 of 10
6. Equality & Downcasting
Answer:
• Use getClass() in equals() to ensure a Song ≠ Movie even with same ID/title.
Code:
if (item instanceof Song) {
Song s = (Song) item;
s.rate(5);
}
Code:
for (MediaItem item : mediaList) {
if (item instanceof Downloadable) {
((Downloadable) item).download();
}
}
Class MediaItem:
public abstract class MediaItem {
protected String id;
protected String title;
protected int duration; // in seconds
Page 6 of 10
this.title = title;
this.duration = duration;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof MediaItem)) return false;
if (this.getClass() != obj.getClass()) return false;
@Override
public int hashCode() {
return id.hashCode() + title.hashCode();
}
}
Class Rateable:
public interface Rateable {
void rate(int stars);
}
Class Song:
public class Song extends MediaItem implements Downloadable, Rateable {
private String artist;
@Override
public void play() {
System.out.println("Playing song by " + artist + ": " + title);
}
@Override
public void download() {
System.out.println("Downloading song: " + title);
Page 7 of 10
}
@Override
public void rate(int stars) {
System.out.println("Rated song '" + title + "' with " + stars + "
stars.");
}
}
Class Movie:
public class Movie extends MediaItem implements Rateable {
private String director;
@Override
public void play() {
System.out.println("Playing movie directed by " + director + ": " +
title);
}
@Override
public void rate(int stars) {
System.out.println("Rated movie '" + title + "' with " + stars + "
stars.");
}
}
Class Podcast:
public class Podcast extends MediaItem implements Downloadable {
private String host;
@Override
public void play() {
System.out.println("Streaming podcast by " + host + ": " + title);
}
@Override
public void download() {
Page 8 of 10
System.out.println("Downloading podcast: " + title);
}
}
Class Playlist:
import java.util.*;
mediaLibrary.add(song);
mediaLibrary.add(movie);
mediaLibrary.add(podcast);
System.out.println("\nDownloading items:");
for (MediaItem item : mediaLibrary) {
if (item instanceof Downloadable) {
((Downloadable) item).download();
Page 9 of 10
}
}
Output:
Page 10 of 10