DOC PREVIEW
UMD CMSC 131 - Lecture 33: Inheritance

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11/15/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 33:InheritanceLast time:1. Iterators2. StringBufferToday1. InheritanceCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Interitance 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: CircleCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Shape, Circle Example Shape classpublic class Shape {private int color; // Color of shapepublic int getColor () {return color;}public void setColor (int newColor) {color = newColor;}} Circle classpublic class Circle {private int color; // Color of circleprivate double radius; // Radius of circleprivate Point center; // Center of circlepublic int getColor () {…}public void setColor (int newColor) {…}public double getRadius () {…}…}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3Can 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 capabilityCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Shape, Circle Revisited Shapepublic class Shape {private int color; // Color of shapepublic int getColor () {return color;}public void setColor (int newColor) {color = newColor;}} Circlepublic class Circle extends Shape {private double radius; // Radius of circleprivate Point center; // Center of circlepublic double getRadius () {return radius;}public Point getCenter () {return center;}…}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5extends Keyword in Java to signify one class is derived from (inherits from) anotherpublic 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 membersCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6Inheritance More Generally Classes / objects have a natural “is-a” hierarchy Object-oriented programming provides mechanisms for exploiting this for Code re-useCommon operations implemented in super classes PolymorphismObjects in subclasses can be used wherever superclass objects are neededShapeCircle RectangleTriangleRight-Triangle Equilateral-TriangleAnimalInsect ReptileMammalCat DogPrimateHuman Ape HomerCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7Example: People at University Base class: person Derived classes: student, professor, administratorPersonStudent Faculty AdministratorUndergrad GradStudent Instructor Professor……PersonStudent FacultyCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8Base Class: Person (Part 1)package university;public class Person {private String name; // person's nameprivate String idNum; // ID numberpublic Person( ) {name = "No Name";idNum = "000-00-0000";}public Person( String n, String id ) {name = n;idNum = id;}public Person( Person p ) {name = p.name;idNum = p.idNum;}// …other methods in part 2}Instance variablesDefault constructorStandard constructorCopy constructorCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9Subclasses: Student, Faculty Each class inherits data, methods from Person Each adds special new data, methods appropriate for subclass Person:nameID-NumberStudent:admission yearGPAFaculty:year hiredPerson is thebase class (or super class)Student and Facultyare the derivedclasses (subclasses)CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Derived Class Structure Person: (base class)Instance Data: Name and ID-number.String nameString idNumMethods: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 admitYeardouble gpaMethods: (same structure as Person) Faculty: (derived from Person)Instance Data: Year hired.int hireYearMethods: (same structure as Person)CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11Derived class: Student (Part 1)package university;public class Student extends Person {private int admitYear;private double gpa;public Student( ) {super( );admitYear = -1;gpa = 0.0;}public Student( String n, String id, int yr, double g ) {super( n, id );admitYear = yr;gpa = g;}public Student( Student s ) {super( s );admitYear = s.admitYear;gpa = s.gpa;}// …other methods in part 2}Additional instance variablesDefault constructorStandard constructorCopy constructorThis calls the default constructorfor base class (superclass),Person, to set name and idNum.Calls Person constructor.Calls Person copy constructor.Tells Java that Student is derived from PersonCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland12Understanding 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 2006Rance Cleaveland©2006 University of Maryland13HeapMemory 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 ExamplePerson ted = new Person("Ted Goodman", "111-22-3333" );Student carole = new Student(“Carole Goode", "123-45-6789", 2004, 4.0 );tedcaroleTed Goodman111-22-3333Carole Good123-45-6789super( n, id )builds thePerson part20044.0Student constructor finishes it offCMSC 131 Fall 2006Rance Cleaveland©2006 University


View Full Document

UMD CMSC 131 - Lecture 33: Inheritance

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 33: Inheritance
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 33: Inheritance and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 33: Inheritance 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?