Docsity

Object-Oriented Programming (OOP) Chapter 11: Inheritance, Polymorphism, and Composition, Slides of Applications of Computer Sciences

Partial preview of the text

Download Object-Oriented Programming (OOP) Chapter 11: Inheritance, Polymorphism, and Composition and more Slides Applications of Computer Sciences in PDF only on Docsity!

OOP

Chapter 11 Overview

Today’s Lesson Plan

  • Discuss labs, quizzes, and TDA topics (10 min)
    • One more week left so please keep up!
  • Class Activities – covers Chapter 11 (120 min)
    • Discuss a PICA (Polymorphic, Inheritance, and Composition Architecture)
    • Discuss and visually show the structure, syntax and execution of an Inheritance application.
    • Discuss and visually show the structure, syntax and execution of a Polymorphic Application.
    • Discuss and visually show the structure, syntax and execution of a Composition application.
    • NOTE: All of these applications could actually be one application with a PICA.
  • Coding Exercises & break (90 min)
    • Code Ex 1 – p 594 – #11.3.8a & 8b
    • Code Ex 2 – p 601 – #11.4.
    • Code Ex 3 – p 601 – #11.4.

Each object has its own copy of the Class’

instance variables

  • Each object maintains its own set of instance variables
  • Permits each object to have its own clearly defined state
  • When an object is created, a distinct area of memory is set aside for its instance variables

The this Reference

  • Member methods
    • Unlike data storage
      • Only one copy of each member method is maintained in memory per class
    • Requires providing a means of identifying which specific object a member method should be operating on
    • Use the this reference to specify the current instance

Memberwise Assignment (continued)

  • Cascading method call:
    • Example:
      • a.setEqual(b.setEqual(c))
    • Method must return same data type as parameter
    • Can use the this reference in a return statement

Class Inheritance & Polymorphism

  • Inheritance
    • Constructing one class from another
  • E.g., constructing a Sphere from a Circle class:
  • Polymorphism
    • Provides the ability to redefine how methods of related classes operate based on the object being referenced

Derived Class Properties

  • Derived class:
    • Derived from base
    • Also called:
      • Child or Subclass
    • Completely new class
      • Incorporates all the variables and methods of the base class
    • Adds new data and method members
    • Can override any base class method

Kinds of Inheritance

  • Simple inheritance
    • Each derived type has only one immediate base type
  • Multiple inheritance
    • Derived type has two or more base types
      • Not supported in Java
  • Class hierarchies
    • Illustrate the order in which one class is derived from another

Inheritance Syntax & Idioms

  • Header line syntax for derived class:
    • public class derivedClassName extends baseClassName
  • protected visibility:
    • Behaves identically to private visibility within a class
      • But it permits this restriction to be inherited by any derived class
  • super reserved word
    • Refers to a method or variable in the parent class

Abstract methods cannot be:

  • Abstract methods cannot be declared as:
    • static or final
  • final not allowed because it doesn’t allow over-riding by definition
  • static because you can only “hide” a static method:
    • Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class.Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can't override a static method with a nonstatic method. In other words, you can't change a static method into an instance method in a subclass.

Interfaces

  • An Interface consists of constants and abstract methods only - Syntax: interface interfaceName { constant declarations; abstract method declarations; }

Implementing an interface

  • Implementing an interface syntax: create a new , derived class, as per the following:

class className implements interfaceName { override defns of all abstract methods; }

Polymorphism

  • Permits the same method name to invoke:
    • One response in objects of a base class
    • Another response in objects of a derived class
    • The determination of which overloaded method is actually called is made at run time
    • Known as run-time binding
    • Object being operated on ultimately determines the appropriate method to be called