Abstract Classes and Interfaces (?) June 21, 2017 Reading Quiz - - PowerPoint PPT Presentation

abstract classes and interfaces
SMART_READER_LITE
LIVE PREVIEW

Abstract Classes and Interfaces (?) June 21, 2017 Reading Quiz - - PowerPoint PPT Presentation

Abstract Classes and Interfaces (?) June 21, 2017 Reading Quiz Abstract Classes A. Abstract classes inherit from multiple parents, standard classes only inherit from one parent. B. Abstract classes can only have one instance at a time,


slide-1
SLIDE 1

Abstract Classes and Interfaces (?)

June 21, 2017

slide-2
SLIDE 2

Reading Quiz

slide-3
SLIDE 3

Abstract Classes

  • A. Abstract classes inherit from multiple parents,

standard classes only inherit from one parent.

  • B. Abstract classes can only have one instance at a time,

standard classes can have any number.

  • C. Abstract classes cannot be directly instantiated,

standard classes can be directly instantiated

  • D. Abstract classes can only contain primitive properties,

standard classes can have primitive and reference properties.

slide-4
SLIDE 4

Abstract Methods

  • A. Abstract classes consist of only abstract methods
  • B. Abstract methods do not specify their

implementation

  • C. Abstract methods are the same thing as a "static

final" method

  • D. Abstract methods perform faster than standard

methods

slide-5
SLIDE 5

Interfaces vs. Abstract Classes

  • A. Interfaces always inherit from Abstract Classes
  • B. Abstract Classes always implement Interfaces
  • C. Interfaces cannot inherit from Abstract Classes
  • D. Abstract Classes cannot implement Interfaces
slide-6
SLIDE 6

Whats the Result?

  • A. "Child💕Person"
  • B. "Person💕Child"
  • C. "null💕null"
  • D. Won't compile
  • E. Runtime error

abstract class Person { public String name() { return "Person";
 }
 } class Child extends Person { public String name() { return "Child";
 }
 } Child aKindChild = new Child(); Person aMeanPerson = new Person(); System.out.println(
 aMeanPerson.name() + "💕 " + aKindChild.name()
 );

slide-7
SLIDE 7

Whats the Result?

  • A. "🐲"
  • B. "🐷"
  • C. "" (ie empty string)
  • D. Won't compile
  • E. Runtime error

interface Emojier { public String asEmoji(); } class Dog implements Emojier { public String asEmoji() { return "🐷 ";
 }
 } class Cat implements Emojier { public String asEmoji() { return "🐲 ";
 }
 } Cat toonces = new Cat(); String anEmoji = toonces.asEmoji(); System.out.println(anEmoji);

slide-8
SLIDE 8

Done!

slide-9
SLIDE 9

Housekeeping

  • Piazza Recap / Java environments
  • Homework 2 questions
  • Homework 3 incoming
slide-10
SLIDE 10

Abstract Classes

slide-11
SLIDE 11

Problem

  • Types express a Taxonomy and data / functionality
  • Some types are meant to represent data in a

program

  • Some types are "only" categorization
  • Code should enforce this distinction
slide-12
SLIDE 12

Animals Mammals Reptiles Dogs Whales Snakes Lizards isWarmBlooded() -> true isWarmBlooded() -> false

slide-13
SLIDE 13

Animals Mammals Reptiles Dogs Whales Snakes Lizards isWarmBlooded() -> true isWarmBlooded() -> false

Categorization <– –> Program Logic

slide-14
SLIDE 14

Animals Mammals Reptiles Dogs Whales Snakes Lizards isWarmBlooded() -> true isWarmBlooded() -> false

Categorization <– –> Program Logic

Abstract

slide-15
SLIDE 15

Java Solution:
 Abstract Classes

  • Can not be instantiated, but can be inherited
  • Standard inheritance model
  • Arbitrarily deep
slide-16
SLIDE 16

Animals Mammals Whales Dogs Sad Dogs Kujo Old Yeller

slide-17
SLIDE 17

Animal Mammals Whale Dog SadDog DeadDog JerkDog

Which are Valid?

  • A. new Mammal()
  • B. new SadDog()
  • C. new Dog()
  • D. new JerkDog()
slide-18
SLIDE 18

Abstract Use Cases?

Abstract Concrete Subtype(s) 1 SimpsonCharacter Bart, Lisa 2 AdminUser StandardUser, AnonymousUser 3 MembersOfTheRamones JoeyRamone, TommyRamone 4 CreditCardChargeError RareCreditCardChargeError

slide-19
SLIDE 19

Animal.java –>

slide-20
SLIDE 20

Abstract Methods

  • Taxonomy can impose "helpful" restrictions too
  • Things below me must implement these methods
  • Keep types clean
slide-21
SLIDE 21

Fur.java –>

slide-22
SLIDE 22

Interfaces

slide-23
SLIDE 23

Problem

  • Classes categorize data in a single, global way
  • Multiple categorizations possible
  • Redundancy, or insane hierarchies
slide-24
SLIDE 24

TrickyTypes.md –>

slide-25
SLIDE 25

Entertainment Live Recorded Opera Performance Jazz Performance Books Photographs Whirly Ball Records

slide-26
SLIDE 26

Entertainment Live Recorded Opera Performance Jazz Performance Books Photographs Whirly Ball Records

slide-27
SLIDE 27

Entertainment Live Recorded Opera Performance Jazz Performance Books Photographs Whirly Ball Vinyl void record(? item);

slide-28
SLIDE 28

Java Solution

  • Interfaces
  • Hierarchical, but separate
  • Same modifiers, same inheritance rules
slide-29
SLIDE 29

Interfaces vs. Classes

  • Classes for data
  • Animals
  • Music Categories
  • Movies
  • Interfaces for Functionality
  • Pet-able
  • Danceable
  • Recordable
slide-30
SLIDE 30

Interfaces.java –>

slide-31
SLIDE 31

Interfaces Specifics

  • Classes can implement multiple interfaces
  • ArrayList implements Serializable, Iterable,

Collection, List

  • Small, light
  • Similar to Abstract Classes
  • Inheritance, default methods
slide-32
SLIDE 32
slide-33
SLIDE 33

Taxonomizing File Types

  • /etc/mime.types
  • Mime is a very simple taxonomy system
  • Lets implement in code!