Lecture 13

Fall 2018

Narges Norouzi

Recap

  • Static
    • Static field and static method
  • Final
    • Final class, method, and variable

 

Abstract behavior

football game

Abstract behaviors

abstract behaviors (1)

  • Does all football objects kick the ball?
    • No. So “to kick” is not a behavior of football objects.
  • All football objects needs to be drawn
    • “to draw” is a behavior of all objects.
    • But what is the shape of a football object?
    • It depends to the specific type of instance.

 

abstract behaviors (2)

  • “draw” is an abstract behavior of a football object
    • But we cannot specify how to draw football objects
    • It depends on the specific class of football objects
  • “draw” is a concrete behavior of Ball
  • “kick” is not a behavior of a football object but it is a concrete behavior of a Player object

 

Remember shapes?

Abstract shape

  • Shape is an abstract class
    • Some methods aren't implemented in Shape
    • Those methods should be implemented in subclasses
    • Example: getArea() - getPerimeter()
  • These methods are abstract methods

 

abstract methods (1)

  • Shape class
    • getArea() and getPerimeter()
  • FootballObject
    • draw()
  • getCoordinates() is not an abstract method
  • How should we implement an abstract method?

 

abstract methods(2)

  • Abstract method: no implementation
  • A class containing abstract methods:
    • an abstract class
  • You cannot instantiate any object from an abstract classes. Why?
  • If a subclass doesn't implement all abstract methods of its superclass, it will be abstract too

example

Interface

Interface

  • Sometimes we have an abstract class, with no concrete method

  • Interface:

    • Pure abstract class

    • No concrete method

Interface

Methods in interfaces

  • All the methods are implicitly abstract
    • No need to specify them as abstract
  • All the methods are implicitly public
    • No need to write public access specifier

 

Interface implementatioN

  • A class may inherit from an abstract class

 

  • A class may implement interfaces

  • Is-a relation exists if a class implements an interface

  • What if it does not implement all the methods?

    • The class becomes an abstract class

Zybooks chapters we covered

  • Chapter 11: abstract class and interfaces

Copy of CMPS12A - Lecture 13

By Narges Norouzi

Copy of CMPS12A - Lecture 13

  • 293