2

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

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

7 Comments

+1. The general principle is that you should create subclasses for different behavior, not different attributes.
what about the ID @Carsten ??
Thanks Carsten, I believe this is the answer I was looking for.
@PiyushMittal both classes would probably have their own IDs. The unique Movie/Film and each of the dozen MovieCopy (DVDs, Blurays, Flashdrives) instances available to be hired.
@AshleyRowland I added a short example for the instantiation. Be sure to understand the difference between 'class' and 'object'/'instance'. In the given example Movie is the 'class', but starWars4 = new Movie() creates a new 'instance' of that class. You can have thousands of Movie objects, which are all instances of the one Movie class. The difference between the 'instances' are their respective attributes ('Star Wars 1', 'Star Wars 2', ...). The class only defines what kind of attributes exist ('id', 'title', ...).
|
1

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.

4 Comments

I think there's some confusion stemming from the overloading of the word movie. The OP's Movie class contains the title, date, etc., while her MovieCopy class represents a physical copy of that movie.
@KevinKrumwiede I think you can just use one whole Movie class for that purpose. Movie class contains the title, date, etc and a Movie object represents a physical copy of that movie
I think the goal is to reduce the duplication of data.
Kevin, that is correct. @Sweeper I had separate classes to try and reduce the amount of data I'm manually inputting. eg, I wanted a Movie object to be made for each movie (eg, Star wars 4), but I wanted each time I made a copy of that movie, it'd automatically inherit details such as movie ID and length from the movie object. That way, each time a copy of the movie is made, instead of having to specify all the details, only it's unique ID, date of purchase ect. need to be added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.