ap computer science a – healdsburg high school 1 interfaces, abstract classes and the dancestudio...

8
1 AP Computer Science A – Healdsburg High School Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes and interfaces - Dance Studio Project

Upload: eleanore-mcbride

Post on 03-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes

1 AP Computer Science A – Healdsburg High School

Interfaces, Abstract Classes and the DanceStudio

- Similarities and Differences between Abstact Classes and

interfaces- Dance Studio Project

Page 2: AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes

2 AP Computer Science A – Healdsburg High School

Class Object

• In Java every class by default extends a library class Object (from java.lang)

• Object is a concrete class

public class Object{ public String toString {...} public boolean equals (Object other) {... } public int hashCode() { ... }

// a few other methods ...}

Methods redefined (overridden) as necessary

Page 3: AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes

3 AP Computer Science A – Healdsburg High School

Classes Interfaces

• A superclass provides a secondary data type to objects of its subclasses.

• An abstract class cannot be instantiated.

• An interface provides a secondary data type to objects of classes that implement that interface.

• An interface cannot be instantiated.

Similarities

Page 4: AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes

4 AP Computer Science A – Healdsburg High School

Classes Interfaces

• A concrete subclass of an abstract class must define all the inherited abstract methods.

• A class can extend another class. A subclass can add methods and override some of its superclass’s methods.

• A concrete class that implements an interface must define all the methods specified by the interface.

• An interface can extend another interface (called its superinterface) by adding declarations of abstract methods.

Similarities

Page 5: AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes

5 AP Computer Science A – Healdsburg High School

Classes Interfaces

• A class can extend only one class.

• A class can have fields.

• A class defines its own constructors (or gets a default constructor).

• A class can implement any number of interfaces.

• An interface cannot have fields (except, possibly, some public static final constants).

• An interface has no constructors.

Differences

Page 6: AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes

6 AP Computer Science A – Healdsburg High School

Classes Interfaces

• A concrete class has all its methods defined. An abstract class usually has one or more abstract methods.

• Every class is a part of a hierarchy of classes with Object at the top.

• All methods declared in an interface are abstract.

• Interfaces are generally standalone structures.

Differences

Page 7: AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes

7 AP Computer Science A – Healdsburg High School

Dance Studio (already done)

AbstractDance

Waltz

AbstractDance Class

Subclass of AbstractDance

Dance Dance Interface

Page 8: AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes

8 AP Computer Science A – Healdsburg High School

Dance Studio (your job)

ReversedDance ReversedDance Class(needs to be implemented)

Dance Dance Interface

public interface Dance{ DanceStep getStep(int i); int getTempo(); int getBeat(int i);}