I’m not sure what would be good OO design in the following case.
First of all I know you cannot use:
this = anObject;
But my sitiuation is as follows. I have a superclass (Film) with over 20 data fields. A subclass (ComedyFilm) extends the superclass and has just 3 additional data fields.
This is the code I would like to place in my subclass:
public ComedyFilm(GenericFilm parent, FilmRating rating) {
this = parent;
}
So that when creating a new instance of ComedyFilm, it would be something like:
Film myFilm = new Film(.... long constructor);
ComedyFilm myComedyFilm = new ComedyFilm(myFilm, FilmRating.EIGHTEEN);
Is there an easy way to do this without getting/setting every single variable in the constructor? Without doing
this.filmName = parent.filmname;
this.directors = parent.directors;
//etc...
Thanks!
The derived class should defer to the base class constructor for copying base class fields. What this means is that the very first line of your ComedyFilm constructor will be
super(someArgumentsForTheBaseClass)followed by whatever you need to do to initialize your derived class.