Lecture 33 Inheritance Last time 1 Iterators 2 StringBuffer Today 1 Inheritance 11 15 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Interitance A crucial feature of object oriented programming languages One class derived class subclass is constructed by importing inheriting information from another base class superclass parent class and adding new information redefining existing Example Base class Shape Derived class Circle CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Shape Circle Example Shape class public class Shape private int color Color of shape public int getColor return color public void setColor int newColor color newColor Circle class public class Circle private int color private double radius private Point center Color of circle Radius of circle Center of circle public int getColor public void setColor int newColor public double getRadius CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 Can We Avoid Code Copying Circle is a Shape Operations on Shape e g setColor should be inherited by Circle Circle should only have to add information specific to circle Radius Center Inheritance provides just this capability CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Shape Circle Revisited Shape public class Shape private int color Color of shape public int getColor return color public void setColor int newColor color newColor Circle public class Circle extends Shape private double radius Radius of circle private Point center Center of circle public double getRadius return radius public Point getCenter return center CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 extends Keyword in Java to signify one class is derived from inherits from another public class Circle extends Shape Methods data members from base class superclass parent class are implicitly present in derived class Derived class subclass may Add new methods data members Redefine existing methods data members CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Inheritance More Generally Classes objects have a natural is a hierarchy Object oriented programming provides mechanisms for exploiting this for Code re use Common operations implemented in super classes Polymorphism Objects in subclasses can be used wherever superclass objects are needed Shape Circle Triangle Right Triangle Animal Rectangle Equilateral Triangle Insect Mammal Cat Primate Dog Human Ape Homer CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland Reptile 6 Example People at University Base class person Derived classes student professor administrator Person Student Undergrad GradStudent Faculty Instructor Professor CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland Administrator 7 Base Class Person Part 1 package university public class Person private String name private String idNum person s name ID number Instance variables public Person name No Name idNum 000 00 0000 Default constructor public Person String n String id name n idNum id Standard constructor public Person Person p name p name idNum p idNum other methods in part 2 Copy constructor CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 Subclasses Student Faculty Each class inherits data methods from Person Each adds special new data methods appropriate for subclass Person name ID Number Student admission year GPA Person is the base class or super class Faculty year hired CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland Student and Faculty are the derived classes subclasses 9 Derived Class Structure Person base class Instance Data Name and ID number String name String idNum Methods Constructors default standard copy constructors Accessors Setters getname setName getIdNum setIdNum Standard methods toString equals Student derived from Person Instance Data Admission year and GPA int admitYear double gpa Methods same structure as Person Faculty derived from Person Instance Data Year hired int hireYear Methods same structure as Person CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 Derived class Student Part 1 package university public class Student extends Person private int admitYear Additional private double gpa public Student super admitYear 1 gpa 0 0 Tells Java that Student is derived from Person instance variables Default constructor This calls the default constructor for base class superclass Person to set name and idNum public Student String n String id int yr double g Standard constructor super n id admitYear yr Calls Person constructor gpa g public Student Student s Copy constructor super s admitYear s admitYear Calls Person copy constructor gpa s gpa other methods in part 2 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Understanding Student extends specifies that Student is subclass of Person public class Student extends Person super When creating a new Student object we need to initialize its base class instance variables from Person This is done by calling super E g super name id invokes constructor Person name id super must be the first statement of your constructor If you do not call super Java will automatically invoke the base class s default constructor If the base class s default constructor is undefined Error You must use super not Person CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 Memory Layout and Initialization Order When you create a new derived class object Java allocates space for base class instance variables and derived class variables Java initializes base class variables first and then the derived class variables Example Person ted new Person Ted Goodman 111 22 3333 Student carole new Student Carole Goode 123 45 6789 2004 4 0 Heap Ted Goodman ted 111 22 3333 carole Carole Good 2004 4 0 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 123 45 6789 super n id builds the Person part Student constructor finishes it off 13 Derived class Student Part 2 public class Student extends Person private int admitYear Instance private double gpa variables part 1 constructors in part 1 public int getAdmitYear return admitYear public double getGpa return gpa Standard accessors and setters public void setAdmitYear int yr admitYear yr public void setGpa double g gpa g We do not need accessors for the base class we inherit them public String toString return super toString admitYear gpa public boolean equals Student s return super equals s admitYear
View Full Document