Java Programming/Inheritance
Syntax
[edit | edit source]In Java, the syntax for deriving a child class from a parent class is:
class subclass extends superclass
{
// new characteristics of the subclass go here
}
Example
[edit | edit source]Here is a program that uses a class Video to represent movies available at a video rental store. The Video class has basic information in it, and would be OK for documentaries and instructional tapes. But movies need more information. An additional class, Movie, is created that is similar to Video, but has the name of the director and a rating.
class Video
{
protected String myTitle; // name of the item
protected int myLength; // number of minutes
protected boolean myIsAvail; // is the tape in the store?
public Video(String title, int len)
{
myTitle = title; myLength = len ; myIsAvail = true;
}
public String toString()
{
return myTitle + ", " + myLength +
" min. available: " + myIsAvail;
}
}
class Movie extends Video
{
protected String myDirector; // name of the director
protected String myRating; // G, PG, R, or X
// constructor
public Movie(String title, int len, String dir, String rating)
{
// use the super class' constructor
super(title, len);
// initialize what's new to Movie
myDirector = dir; myRating = rating;
}
}
class VideoStore
{
public static void main (String args[])
{
Video item1 = new Video("Juiced on Java", 90 );
Movie item2 = new Movie("Just Java", 120, "Gosling", "PG" );
System.out.println(item1.toString());
System.out.println(item2.toString());
}
The class Movie is a derived class (subclass) of Video. An object of type Movie has the following members in it:
Members | Where is it from? |
---|---|
MyTitle | inherited from Video |
myLength | inherited from Video |
myIsAvail | inherited from Video |
toString() | inherited from Video |
myDirector | defined in Movie |
myRating | defined in Movie |
Both classes are defined: the Video class can be used to construct objects of that type, and now the Movie class can be used to construct objects of the Movie type.
The class definition for Video has a constructor that initializes the member data of Video objects. The class Movie has a constructor that initializes the data of Movie objects. The constructor for class Movie looks like this:
// constructor
public Movie(String title, int len, String dir, String rating)
{
// use the super class' constructor
super(title, len);
// initialize what's new to Movie
myDirector = dir; myRating = rating;
}
The statement super(title, len) invokes the base class' constructor to initialize some of the data. Then the next two statements (on one line) initialize the members that only Movie has. When super is used in this way, it must be the first statement in the subclass' constructor.
4. It is not necessary to use super; the following would also work as a constructor for Movie:
// constructor
public Movie(String title, int len, String dir, String rating)
{
// initialize the inherited members
myTitle = title; myLength = len ; myIsAvail = true;
// initialize members unique to Movie
myDirector = dir; myRating = rating;
}
In this constructor, each variable of the newly created Movie object is set to an initial value.
Project: Introduction to Programming in Java |
Previous: Introduction to Programming in Java/Java Classes — Java Programming/Inheritance — Next: Introduction to Programming in Java/Arrays |