18
Chapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY CS 2302 SUMMER 2015 slides created by Rashaad Jones

Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

  • Upload
    vandat

  • View
    223

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

Chapter 13

Abstract Classes &

InterfacesDR. JONES

KENNESAW STATE UNIVERSITY

CS 2302

SUMMER 2015

slides created by Rashaad Jones

Page 2: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

LEARNING OBJECTIVES DISCUSSED

SO FAR…

Understand the basic principles of object-oriented programming, including inheritance and polymorphism (Chap 10-12)

Understand both the "program driven" and "event-driven" approaches to user interaction, including the relationship between event-driven programs and Graphical User Interfaces

Understand the fundamentals of exception handling (Chap 10-12)

Be able to design and implement algorithms utilizing the principles of object-oriented programming to solve elementary problems (Chap 10-12)

Be able to follow specified style guidelines in writing programs, and understand how the guidelines enhance readability and promote correctness in programs (Chap 10-12)

Be able to edit, compile, debug and run moderate sized programs in a specific programming language (Chap 10-12)

Page 3: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

LEARNING OBJECTIVES DISCUSSED

SO FAR

Understand the basic principles of object-oriented programming, including inheritance and polymorphism

• Discussed in Chap 10-12 and will be discussed later…

Understand both the "program driven" and "event-driven" approaches to user interaction, including the relationship between event-driven programs and Graphical User Interfaces

• Coming soon…

Understand the fundamentals of exception handling

• Discussed in Chap 10-12

Page 4: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

LEARNING OBJECTIVES DISCUSSED

SO FAR

Be able to design and implement algorithms utilizing the principles of object-oriented programming to solve elementary problems

• Discussed in Chap 10-12 and will be discussed later…

Be able to follow specified style guidelines in writing programs, and understand how the guidelines enhance readability and promote correctness in program

• Discussed in Chap 10-12 and will be discussed later…

Be able to edit, compile, debug and run moderate sized programs in a specific programming language

• Discussed in Chap 10-12 and will be discussed later…

Page 5: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

QUESTION TO CONSIDER:

HOW TO PROPERLY MODEL AN ANIMAL

CLASS Animal Class

Eat(Food food)

//convert food to energy (or health)

Sleep(int mins)

//rest for specified time

makeNoise

System.out.println( {noise made}

how would we implement???

makeNoise(Animal atype)

selection statement for each Animal type TOO BURDENSOME ON THE CLASS AUTHOR

makeNoise(String noiseStr)

Class User determines what sound is made TOO MUCH FREEDOM FOR CLASS USERS Same animal type making different sounds

Page 6: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

SOLUTION:

USE ABSTRACT CLASSESpublic abstract Animal

{

public void eat(Food food)

{

// do something with food....

}

public void sleep(int hours)

{

try

{

// 1000 milliseconds * 60 seconds * 60 minutes * hours

Thread.sleep ( 1000 * 60 * 60 * hours);

}

catch (InterruptedException ie) { /* ignore */ }

}

public abstract void makeNoise();

}

public Dog extends Animal

{

public void makeNoise() {

System.out.println ("Bark! Bark!"); }

}

public Cow extends Animal

{

public void makeNoise() {

System.out.println ("Moo! Moo!"); }

}

Page 7: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

ABSTRACT CLASSES

Overiew

Abstract classes are classes that contain one or more abstract methods

Purpose

Provide class users to implement behaviors that will apply to all

instantiations of a particular subclass

Page 8: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

ABSTRACT CLASSES

EXAMPLEpublic abstract class GeometricObject

{

public abstract double getArea();

public abstract double getPerimeter();

}

public class Circle extends GeometricObject

{

public double getArea()

{

return radius*radius * Math.PI;

}

public double getPerimeter()

{

return 2 * radius * Math.PI;

}

}

Page 9: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

INTERFACES

Overview

An interface is a class-like construct that contains ONLY constants and

abstract methods

Purpose

Similar to an abstract class

Specify common behavior for objects

Page 10: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

Interfaces:

Example

public interface Bicycle {

// wheel revolutions per minute

void changeCadence(int newValue);

void changeGear(int newValue);

void speedUp(int increment);

void applyBrakes(int decrement);

}

class ACMEBicycle implements Bicycle {

int cadence = 0; int speed = 0; int gear = 1;

void changeCadence(int newValue) {

cadence = newValue;

}

void changeGear(int newValue) {

gear = newValue;

}

void speedUp(int increment) {

speed = speed + increment;

}

void applyBrakes(int decrement) {

speed = speed - decrement;

}

}

Page 11: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

Understanding Abstract classes &

Interfaces

Abstract classes and interfaces CANNOT be instantiated with the new keyword

Concrete classes are neither abstract or interfaces

Concrete classes inherit abstract classes when there is a STRONG relationship

Townhouse extends House

Concrete classes implement interfaces when there is a WEAK relationship

Townhouse extends House implements AirConditioning

Page 12: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

Abstract Classes vs. Interfaces

Abstract classes are useful if

the solution needs…

A common base class implementation to derived classes.

Non-public members or methods with base implementation.

(In an interface, all methods must be

public with no implementation and

all members are final).

New methods in future revisions.

(If you add new method headings to

an interface, then all of the classes

that already implement that

interface will have to be changed to

implement the new methods).

Interfaces are useful when

the solution needs…

No revisions.

Concrete classes with multiple

inheritances.

(You can implement multiple

interfaces, but you can only

extend one class).

Page 13: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

Class Design Principles

Cohesion

•A class should describe a single entity

•Do NOT describe Bike and Car in the same class

Consistency

•Follow a standard programming practice

•e.g., _memberNameType for all members

•String _colorStr;

Encapsulation

•Private for all members, public for methods that need to be provided for class users

•Do NOT provide getters and setters for all members, unless there is a NEED for these methods

Clarity

•Have intuitive names for ALL methods and members

•Color _color;

•String _colorStr;

•Do NOT provide complex functionality for a single method

•Break functionality into smaller parts that exist in supporting methods (which are private to the class)

Completeness

•A class should provide functionality for a wide variety of solution

•Robust code

Page 14: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

Tips for developing proper designs

Know the difference between instance vs. static

An instance belongs to the object

ClassName nameOfObject = new ClassName();

nameOfObject.member = valueForMember;

Static belongs to the class

ClassName.member = valueForMember;

Inheritance vs. Aggregation

is-a relationship is inheritance

a class extends functionality from a base class

Apple and Fruit

has-a relationship

a class has a member

Person has a Name

Page 15: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

Value to Abstract Classes and

Interfaces Person

Height

Weight

Name

Athlete Person

Speed

Player Athlete

BaseballPlayer Player

BattingAverage

FootballPlayer Player

Quarterback

RunningBack

Person

Height

Weight

Name

Artist Person

Artistry

PerformingArtist Artist

Singer PerformingArtist

SingerAbility

Musician PerformingArtisit

Guitarist Musician

DrummistMusician

Page 16: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

https://www.youtube.com/watch?v=f74AQUeXfBY

Page 17: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

Game Engines

Chrome Engine 6

(Originally planned for a

sequel to Dead Island)

Dunia Engine 2

REDengine

Unreal Engine 3

Fox Engine (Ground

Zeroes and The Phantom

Pain)

Ignite and Impact

Page 18: Chapter 13 Abstract Classes & Interfacesksuweb.kennesaw.edu/.../summer2015/doc/LectureSlides/Chap13Lecture.pdfChapter 13 Abstract Classes & Interfaces DR. JONES KENNESAW STATE UNIVERSITY

Summary

In Chap 13, you have learned how to create and use interfaces and abstract classes

In Chaps 14-16, you will utilize interfaces and abstract classes to build user interfaces

It will be difficult to develop efficient and reusable code (and sometimes correct code) to these problems

Designing your solution, prior to programming, is becoming more and more important

Design your solution to define

Concepts

Relationships between concepts

Data and services