COMP 401 ADVANCED INHERITANCE Instructor Prasun Dewan MORE INHERITANCE Top Down vs Bottom Up Inheritance Abstract Classes Variable Initialization Constructor Invocation in Same Class Constructor Invocation in Super Class Init Methods Abstract Methods Dynamic Dispatch 2 COURSE DISPLAYER User input course title Program displays course number if offered 3 COURSE DISPLAYER USER INTERFACE 4 COURSE DISPLAYER CLASSES Course List Matches title Main class Creates course list and adds course instances Prints courses Combines dept and number into one string Course class es 5 ALTERNATIVE 1 ONE COURSE CLASS Course Properties Title Dept Number Variables Title Dept Number Not appropriate for freshman seminar Number is not a variable 6 ALTERNATIVE 2 TWO COURSE CLASSES Regular But someCourse variables properties are common Properties Title Dept Variables Title Dept Number Title Dept Number Freshman Seminar Properties Variables Title Dept Number Title Dept Constant Number 7 ALTERNATIVE 3 THREE COURSE CLASSES Additional class for common variables Title Dept Two course classes inherit from it Regular course adds number variable and property Freshman seminar adds number constant and property 8 BOTTOM UP VS TOP DOWN INHERITANCE HIERARCHY Not instantiated AStringHistory AStringDatabase ACourse AFreshmanSeminar ARegularCourse AStringSet 9 ACOURSE Should not be instantiated Cannot have course without a number Always some subclass is instantiated Declared as abstract class 10 ABSTRACT CLASSES Java ensures they are not instantiated Abstract vs regular class Car vs Honda Accord Mammal vs Homo Sapiens 11 ACOURSE package courses public abstract class ACourse String title dept public ACourse String theTitle String theDept title theTitle dept theDept public String getTitle return title public String getDepartment return dept 12 AREGULARCOURSE package courses public class ARegularCourse extends ACourse implements Course int courseNum public ARegularCourse String theTitle String theDept int theCourseNum super theTitle theDept courseNum theCourseNum public int getNumber return courseNum 13 AFRESHMANSEMINAR package courses public class AFreshmanSeminar extends ACourse implements FreshmanSeminar public AFreshmanSeminar String theTitle String theDept super theTitle theDept public int getNumber return SEMINAR NUMBER 14 COURSE INTERFACE package courses public interface Course public String getTitle public String getDepartment public int getNumber 15 FRESHMAN SEMINAR INTERFACE package courses public interface FreshmanSeminar extends Course public final int SEMINAR NUMBER 6 16 INTERPRETED HOW public interface RectangleWithPoint public int getX public int getY public void getWidth public void getHeight public Point getPoint Object interpreted as either an atomic rectangle or an atomic point depending on whether Point or Rectangle in the name gets precedence Bad programming as code for defining Rectangle cannot be used in situations where rectangle without point is needed 17 REUSABLE DESIGN public interface Rectangle public int getX public int getY public void getWidth public void getHeight public interface RectangleWithPoint public Rectangle getRectangle public Point getPoint 18 OBJECTEDITOR AND IS A public class ABoundedObject extends ACartesianPoint implements BoundedObject Point upperLeftCorner lowerRightCorner public ABoundedPoint int initX int initY Point initUpperLeftCorner Point initLowerRightCorner super initX initY upperLeftCorner initUpperLeftCorner lowerRightCorner initLowerRightCorner public Point getUpperLeftCorner public void setUpperLeftCorner Point newVal Neither class or interface has term Point Supertypes ACartesianPoint and Point do Given class C OE looks at names of all T C IS A T 19 POSSIBLE OE BUG If a shape label has a location property that is a Point Point extends some other type The location property is displayed as a Point The bug has been removed in latest OE 20 COURSE DISPLAYER USER INTERFACE REVIEW 21 ACOURSE REVIEW package courses public abstract class ACourse String title dept public ACourse String theTitle String theDept title theTitle dept theDept public String getTitle return title public String getDepartment return dept 22 AREGULARCOURSE REVIEW package courses public class ARegularCourse extends ACourse implements Course int courseNum public ARegularCourse String theTitle String theDept int theCourseNum super theTitle theDept courseNum theCourseNum public int getNumber return courseNum 23 AFRESHMANSEMINAR REVIEW package courses public class AFreshmanSeminar extends ACourse implements FreshmanSeminar public AFreshmanSeminar String theTitle String theDept super theTitle theDept public int getNumber return SEMINAR NUMBER 24 COURSE INTERFACE REVIEW package courses public interface Course public String getTitle public String getDepartment public int getNumber 25 FRESHMAN SEMINAR INTERFACE REVIEW package courses public interface FreshmanSeminar extends Course public final int SEMINAR NUMBER 6 26 COLLECTION VS COURSE CLASS HIERARCHY Collection Course No abstract class Linear inheritance No instance variables in subclasses No explicit constructors to initialize instance variables Abstract class Multiple subclasses of ACourse Instance variables in subclass and superclass Explicit constructors to initialize instance variables 27 AREGULARCOURSE package courses public class ARegularCourse extends ACourse implements Course int courseNum public ARegularCourse String theTitle String theDept int theCourseNum super theTitle theDept courseNum theCourseNum public int getNumber return courseNum 28 ALTERNATIVE AREGULARCOURSE package courses public class ARegularCourse extends ACourse implements Course int courseNum public ARegularCourse String theTitle String theDept int theCourseNum courseNum theCourseNum super theTitle theDept public int getNumber return courseNum Super call must be first statement in constructor but not other methods Subclass may want to override initialization in super class Superclass vars initialized before subclass vars Subclass vars not visible in superclass 29 ALTERNATIVE AREGULARCOURSE package courses public class ARegularCourse extends ACourse implements Course int courseNum public ARegularCourse String theTitle String theDept int theCourseNum courseNum theCourseNum public int getNumber return courseNum No super call 30 ACOURSE package courses public abstract class ACourse extends Object String title dept public ACourse String theTitle String theDept title theTitle dept theDept
View Full Document
Unlocking...