Ashley Rowland
Ashley Rowland

Reputation: 33

Java object extensions

So i'm in the process of building a movie hiring system and have come to conclusion that I want to have a class of movies (which have specific movie data stored) and then another class which will have objects which extend the specific movies (eg, copies of Star wars: the new Hope) each with their own unique ID.

How do I setup my classes so that the information for each unique movie is inherited by the copy objects? (will extending my movieCopy class by my movies class achieve what I'm trying to do? Because I was thinking that would just extend the variables of the movie class, rather than the specific attributes of each object of the movie class.

Sorry in advance for any communication errors. Please feel free to ask if you need me to clarify something.

Structure I'm trying to achieve:
Movie (class)
MovieCopy(class)
MovieCopy <- attributes of the specific movie are inherited in each copy of the movie

Upvotes: 2

Views: 151

Answers (2)

Carsten
Carsten

Reputation: 2147

Your MovieCopy class (DVD, Bluray, ...) could just contain a member variable storing the associated Movie instance (actual film with title, description, ...). That way you have access to all the meta data without any awkward inheritance.

class Movie {
    private long id;
    private String title;
    private LocalDate release;
    private String contentDescription;

    Movie(long id, String title) {
        this.id = id;
        this.title = title;
    }
    ...
}

class MovieCopy {
    private long copyId;
    private Movie movie;
    private LocalDate lastHired;
    private LocalDate latestReturn;

    MovieCopy(long id, Movie movie) {
        this.copyId = id;
        this.move = movie;
    }
    ...
}

EDIT - You would populate your collection of movies like this:

Movie starWars4 = new Movie(1, "Star Wars 4");
MovieCopy starWarsDvd1 = new MovieCopy(1, starWars4);
MovieCopy starWarsDvd2 = new MovieCopy(2, starWars4);
MovieCopy starWarsDvd3 = new MovieCopy(3, starWars4);

As a result you have three copies of the same Movie.

Upvotes: 5

Sweeper
Sweeper

Reputation: 271735

In your case, inheritance is not very suitable. What you are trying to do is create objects. You don't even need a MovieCopy class. You store the specific details of each movie in the movie objects.

Let's assume that your movie class has a name and a durationInMinutes fields and they both have getters and setters. If you want to create a new movie copy, you can do this:

Movie movie = new Movie ();
movie.setName("Star Wars");
movie.setDurationInMinutes (150);

And then you can refer to Star Wars using the variable name --- movie.

You might have other fields in your Movie class but you get the idea, right?

Let me show you when to use inheritance: if you have a kind of movie that has some attributes that ordinary movies don't have, which I can't give you an example because there is only one kind of movie.

Upvotes: 1

Related Questions