Java Programming/Inheritance
< Java Programming​(Redirected from Introduction to Programming in Java/Inheritance)
Syntax
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
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:
MembersWhere is it from?
MyTitleinherited from Video
myLengthinherited from Video
myIsAvailinherited from Video
toString()inherited from Video
myDirectordefined in Movie
myRatingdefined 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:
// constructorpublic 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:
// constructorpublic 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
Last edited on 24 September 2020, at 02:27
Wikiversity
Content is available under CC BY-SA 3.0 unless otherwise noted.
Privacy policy
Terms of Use
Desktop
 Home Random Log in  Settings  Donate  About Wikiversity  Disclaimers
WatchEdit