CS 112 Introduction to Programming - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

CS 112 Introduction to Programming

Description:

... child class inherits the methods and data defined for the parent class ... Overloading lets you define a similar operation in different ways for different data ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 16
Provided by: Richar9
Category:

less

Transcript and Presenter's Notes

Title: CS 112 Introduction to Programming


1
CS 112 Introduction to Programming
  • Lecture 24
  • Inheritance
  • Http//zoo.cs.yale.edu/classes/cs112/

2
Outline
  • Admin.
  • Inheritance

3
Admin.
  • Assignment 5
  • Linked online
  • Midterm one
  • Highest 59
  • Lowest 33
  • Median/average 50/49

4
Recap Inheritance
  • Inheritance allows a software developer to derive
    a new class from an existing one
  • The existing class is called the parent class, or
    superclass, or base class
  • The derived class is called the child class, or
    subclass, or derived class.
  • As the name implies, the child inherits
    characteristics of the parent
  • That is, the child class inherits the methods and
    data defined for the parent class

5
Inheritance
  • Inheritance relationships are often shown
    graphically in a class diagram, with the arrow
    pointing to the parent class

Inheritance should create an is-a relationship,
meaning the child is a more specific version of
the parent
public - private
6
Declaring a Derived Class
  • Define a new class DerivedClass which extends
    BaseClass
  • class BaseClass
  • // class contents
  • class DerivedClass BaseClass
  • // class contents
  • Base class the parent class if omitted the
    parent is the Object class

See Book.cs, Dictionary.cs, BookInheritance.cs
7
Outline
  • Admin.
  • inheritance
  • why inheritance?
  • how to implement inheritance?
  • controlling inheritance

8
Controlling Inheritance
  • A child class inherits the methods and data
    defined for the parent class however, whether a
    data or method member of a parent class is
    accessible in the child class depends on the
    visibility modifier of a member
  • Variables and methods declared with private
    visibility are not accessible in the child class
  • a private data member defined in the parent class
    is still part of the state of a derived class
  • Variables and methods declared with public
    visibility are accessible but public variables
    violate our goal of encapsulation
  • There is a third visibility modifier that helps
    in inheritance situations protected

9
The protected Modifier
  • Variables and methods declared with protected
    visibility in a parent class are only accessible
    by a child class or any class derived from that
    class
  • The details of each modifier are linked on the
    schedule page
  • Example
  • Book.cs
  • Dictionary.cs
  • TestBookDictionary.cs
  • public
  • private
  • protected

10
Outline
  • Admin.
  • inheritance
  • why inheritance?
  • how to implement inheritance?
  • controlling inheritance
  • constructors

11
Calling Parents Constructor in a Childs
Constructor the base Reference
  • Constructors are not inherited, even though they
    have public visibility
  • The first thing a derived class does is to call
    its base class constructor, either explicitly or
    implicitly
  • implicitly it is the default constructor
  • yet we often want to use a specific constructor
    of the parent to set up the "parent's part" of
    the object
  • Syntax use basepublic DerivedClass
    BaseClass public DerivedClass() base()
    //
  • Example2
  • See FoodItem.cs
  • See Pizza.cs
  • See FoodAnalysis.cs
  • Example1
  • See Book.cs
  • See Dictionary.cs
  • See BookConstructor.cs

12
Outline
  • Admin.
  • Inheritance
  • why inheritance?
  • how to implement inheritance?
  • controlling inheritance
  • calling parents constructor
  • overriding methods

13
Defining Methods in the Child Class Overriding
Methods
  • A child class can override the definition of an
    inherited method in favor of its own
  • That is, a child can redefine a method that it
    inherits from its parent
  • The new method must have the same signature as
    the parent's method, but can have different code
    in the body
  • The type of the object executing the method
    determines which version of the method is invoked

14
Overriding Methods Syntax
  • override keyword is needed if a derived-class
    method overrides a base-class method
  • If a base class method is going to be overridden
    it should be declared virtual
  • Example
  • Thought.cs
  • Advice.cs
  • ThoughtAndAdvice.cs

15
Overloading vs. Overriding
  • Overloading deals with multiple methods in the
    same class with the same name but different
    signatures
  • Overloading lets you define a similar operation
    in different ways for different data
  • Overriding deals with two methods, one in a
    parent class and one in a child class, that have
    the same signature
  • Overriding lets you define a similar operation in
    different ways for different object types
Write a Comment
User Comments (0)
About PowerShow.com