1 / 12

Inheritance, part 4 Abstract classes/methods

Inheritance, part 4 Abstract classes/methods. COMP 401, Spring 2013 Lecture 12 2/19/2013. Midterm Curve. Average: 67.55 Std Dev : 14.45. The next few weeks. Th 2/21 Assignment 4 will be released (due M 3/4, syllabus says 3/1) Composition and Delegation Tu 2/26, Th 2/28 kmp in Olso

joy
Download Presentation

Inheritance, part 4 Abstract classes/methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance, part 4Abstract classes/methods COMP 401, Spring 2013 Lecture 12 2/19/2013

  2. Midterm Curve • Average: 67.55 • StdDev: 14.45

  3. The next few weeks • Th 2/21 • Assignment 4 will be released (due M 3/4, syllabus says 3/1) • Composition and Delegation • Tu2/26, Th 2/28 • kmp in Olso • Two lectures on exceptions by Prof. Dewan • Tu 3/5 • kmp goes to Washington • Pre-recorded lecture on Factory Pattern • A5 Released (due 3/18) • Th 3/7 • Cancelled • Tu3/12, Th 3/14 • Spring Break

  4. Inheritance Recap • Subinterfacing • Adding methods to create a new “contract” • Subclassing • Inherits fields and methods from parent class • Visibility controlled by access modifier • Adds subclass specific fields and methods • Constructor relationship • Overloading parent methods • Subclass methods with different signatures • Overriding parent methods • Subclass methods with same signature • Always virtual • super keyword provides a mechanism to call parent class version of a method

  5. Using inheritance • Example starts with several related classes that are implemented independently. • BlackBear • PolarBear • Python • Robin

  6. Common behavior • Refactor common behaviors into a common parent interface. • Animal interface

  7. Common object state • Refactor common object fields into a common parent class • AnimalImpl

  8. Common implementation • Refactor common method implementations into parent class. • Use overriding if subclasses need to do something special or different. • AnimalImpl

  9. Common behavior with uncommon implementation • Notice speak() is a common behavior. • So we want it to be part of Animal interface • But no common implementation. • Each subclass of animal will have a different way of speaking. • No good default or commonalities in way of speaking that can be specified at parent class.

  10. Abstract Classes and Methods • Parent class has no meaningful implementation of a method. • But part of interface of parent class • Expect subclass to provide it. • In these situations, we never expect (or want) the parent class to be instantiated directly. • We always make new objects using a subclass. • Syntax • Use “abstract” modifier when declaring parent class • Declare any methods that must be provided by subclass in parent • Add “abstract” modifier to method signature. • Follow signature with semicolon instead of method definition

  11. Example Revisited • AnimalImpl

  12. Repeat Exercise with Bears

More Related