Inheritance part 4 Abstract classes methods COMP 401 Spring 2013 Lecture 12 2 19 2013 Midterm Curve Average 67 55 Std Dev 14 45 Raw Score Range Normalized Curve Formula 83 103 1 06 2 45 N 1 1 5 0 4 3 85 68 82 0 03 0 99 N 0 85 3 0 54 67 0 94 0 04 N 3 0 46 53 1 49 1 01 N 1 0 5 0 3 2 0 32 45 2 46 1 55 N 1 5 0 7 1 7 The next few weeks Th 2 21 Assignment 4 will be released due M 3 4 syllabus says 3 1 ComposiXon and DelegaXon Tu 2 26 Th 2 28 kmp in Olso Two lectures on excepXons by Prof Dewan Tu 3 5 kmp goes to Washington Pre recorded lecture on Factory Pa ern A5 Released due 3 18 Th 3 7 Cancelled Tu 3 12 Th 3 14 Spring Break Inheritance Recap Subinterfacing Adding methods to create a new contract Subclassing Inherits elds and methods from parent class Visibility controlled by access modi er Adds subclass speci c elds and methods Constructor relaXonship Overloading parent methods Subclass methods with di erent signatures Overriding parent methods Subclass methods with same signature Always virtual super keyword provides a mechanism to call parent class version of a method Using inheritance Example starts with several related classes that are implemented independently BlackBear PolarBear Python Robin lec12 v1 Common behavior Refactor common behaviors into a common parent interface Animal interface lec12 v2 Common object state Refactor common object elds into a common parent class AnimalImpl Pull common elds id and loca on to here Need to make protected in order to allow subclass access lec12 v3 Common implementaXon Refactor common method implementaXons into parent class Use overriding if subclasses need to do something special or di erent AnimalImpl Constructor in parent class used by subclass constructor to iniXalize common elds at this level getID and getLocaXon moved here Common porXon of move put here Subclass speci c override of move Calls common porXon through super keyword lec12 v4 Common behavior with uncommon implementaXon NoXce speak is a common behavior So we want it to be part of Animal interface But no common implementaXon Each subclass of animal will have a di erent way of speaking No good default or commonaliXes in way of speaking that can be speci ed at parent class Abstract Classes and Methods Parent class has no meaningful implementaXon of a method But part of interface of parent class Expect subclass to provide it In these situaXons we never expect or want the parent class to be instanXated directly We always make new objects using a subclass Syntax Use abstract modi er when declaring parent class Declare any methods that must be provided by subclass in parent Add abstract modi er to method signature Follow signature with semicolon instead of method de niXon Example Revisited AnimalImpl Declare implementaXon of Animal here where it belongs since AnimalImpl is matching implementaXon Declare AnimalImpl as abstract Prevents direct instanXaXon Declare speak method as abstract No common implementaXon but needs to be declared here as part of common interface Declaring as abstract forces subclass to override lec12 v5 Repeat Exercise with Bears lec12 v6 Draws out common bear behavior into Bear interface and common bear implementaXon in BearImpl Bear extends Animal Adds getColor BearImpl extends AnimalImpl Provides lumber trek move and speak Declared abstract because subclass must provide getColor
View Full Document
Unlocking...