Reputation: 225
I'm learning oop, and I need to understand how inheritance actually works.
Let's say I have server call, with response that contain list of Movies
, And another server call that I get the movie credits by movie id.
so my classes will look like this:
Class Movies :
public class Movies {
private Movie[] movies;
public Movies( Movie[] movies) {
this.movies = movies;
}
}
Class Movie:
public class Movie extends Movies {
private String title;
private int id;
private int voteCount;
public Movie(String title, int id, int voteCount) {
super();//the problem
this.title = title;
this.id = id;
this.voteCount = voteCount;
} }
Class Credits:
public class Credits extends Movie {
private int id; //movieId
private Cast[] casts;
public Credits(int id, Cast[] casts) {
super();//the problem
this.id = id;
this.casts = casts;
}
As you can see Credits
extends Movie
that extends from Movies
.
The problem is for example Credits
inherit from Movie
, because Credits
is part of a Movie
, but when I extends Movie
inside Credits
and need to pass arguments to super()
inside my constrcutor I don't really know what to do.
How can I get access to this arguments? why do I need to pass voteCount
for example in my Credits` class to super constructor?
Upvotes: 1
Views: 78
Reputation: 206996
You're using inheritance the wrong way. You have to understand what inheritance means. In programming, inheritance means specialization. It is not some arbitrary way to be able to access other parts of the program.
Which is to say: a subclass is a specialized version of its superclass.
Especially the "is a" is important. When you write:
public class Movie extends Movies
what you're really expressing is: "a Movie object is a (special kind of) Movies object", and
public class Credits extends Movie
"a Credits object is a (special kind of) Movie object".
You can already see that this doesn't make sense - a Movie is not a collection of Movie objects, and Credits are not some kind of Movie.
Theoretical explanation: Liskov substitution principle.
Upvotes: 1