Skip to content

Commit 2ce5f32

Browse files
Create Interfaces.md
1 parent 06b8f7a commit 2ce5f32

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
Interfaces
2+
----------
3+
4+
Interfaces are class templates. Although not strictly required, they are part of the organizational stucture of object-oriented programming. Interfaces define methods for classes by specifying the method name, the return type (or void) and the method arguments (by type and name). These method definitions are called signatures. Because this is a template, the method signatures contain no code. The code is entered into the **implementation** of an interface. Interfaces are used in the discipline of polymorphism.
5+
6+
Note these two important points about interfaces:
7+
8+
- If a class implements an `interface`, all of the interface's methods must appear in the class.
9+
- The `implements` keyword is used when creating a class that is modeled after an interface.
10+
11+
### An analogy
12+
13+
Think of cutting a duck decoy from a block of wood. First, a template is used to trace the pattern onto the wood. Then, when the wood is cut, it resembles the template. At this point, however, neither the template nor the wood have any of the fine details that one would expect from a finished decoy. The template was used to define basic characteristics, not precise details. After multiple duck-shaped pieces have been cut, each one can be carved and decorated uniquely. Yet, they all have the same basic size and shape.
14+
15+
An interface is generally used as a template for multiple classes. These classes all share the methods defined in the interface. However, the implementation of the methods may vary from one class to another.
16+
17+
### An example of an interface
18+
19+
An interface named Animal might have method defintions for feed, groom and pet. The feeding, grooming and petting of animals can be as different as the animals themselves.
20+
21+
interface Animal {
22+
bool feed(bool timeToEat)
23+
void groom()
24+
void pet()
25+
}
26+
27+
A dog class, for instance, may be implemented in a somewhat predictable way.
28+
29+
class Dog implements Animal {
30+
bool feed(bool timeToEat) {
31+
// pour food into bowl
32+
return true
33+
}
34+
void groom() {
35+
// brush well
36+
}
37+
void pet() {
38+
// pet cautiously
39+
}
40+
}
41+
42+
Other animal classes, may have specific needs.
43+
44+
class Giraffe implements Animal {
45+
bool feed(bool timeToEat) {
46+
// point to the trees
47+
return true
48+
}
49+
void groom() {
50+
// get a ladder
51+
// brush well
52+
}
53+
void pet() {
54+
// get a ladder
55+
// pet cautiously
56+
}
57+
}
58+
59+
class Tiger implements Animal {
60+
bool feed(bool timeToEat) {
61+
// toss raw meat into cage
62+
return true
63+
}
64+
void groom() {
65+
// tranquilize
66+
// brush well
67+
}
68+
void pet() {
69+
// DO NOT PET
70+
}
71+
}
72+
73+
Exercise
74+
--------
75+
76+
Create a gorilla class which implements the Animal interface.

0 commit comments

Comments
 (0)