Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f22dc55

Browse files
blakehawkinsshekhargulati
authored andcommitted
improve grammar in section 1 (#10)
1 parent 15f5286 commit f22dc55

File tree

1 file changed

+59
-57
lines changed

1 file changed

+59
-57
lines changed

‎01-default-static-interface-methods.md‎

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
Default and Static Methods for Interfaces
22
--------
33

4-
We all understand that we should code to interfaces. Interfaces give client a
5-
contract which they should use without relying on the implementation details
6-
(i.e. classes). Hence, promoting **[loose coupling](https://en.wikipedia.org/wiki/Loose_coupling)**.
4+
We all understand that we should code to interfaces. Interfaces give the client
5+
a contract which they should use without relying on implementation details (i.e.
6+
classes). Hence, promoting **[loose coupling](https://en.wikipedia.org/wiki/Loose_coupling)**.
77
Designing clean interfaces is one of the most important aspect of API design.
88
One of the SOLID principle **[Interface segregation](https://en.wikipedia.org/wiki/Interface_segregation_principle)**
9-
talks about designing smaller client-specific interfaces instead of designing
9+
talks about designing smaller client-specific interfaces, instead of designing
1010
one general purpose interface. Interface design is the key to clean and
11-
effective API's for your libraries and applications.
11+
effective APIs for your libraries and applications.
1212

1313
> Code for this section is inside [ch01 package](https://github.com/shekhargulati/java8-the-missing-tutorial/tree/master/code/src/main/java/com/shekhargulati/java8_tutorial/ch01).
1414
1515
If you have designed any API then with time you would have felt the need to add
16-
new methods to the API. Once API is published it becomes impossible to add
16+
new methods to the API. Once an API is published, it becomes difficult to add
1717
methods to an interface without breaking existing implementations. To make this
18-
point clear, let's suppose you are building a simple `Calculator` API that
19-
supports `add`,`subtract`, `divide`, and `multiply` operations. We can write
20-
`Calculator` interface as shown below. ***To keep things simple we will use
21-
int.***
18+
point clear, suppose you are building a simple `Calculator` API that supports
19+
`add`,`subtract`, `divide`, and `multiply` operations. We can write a
20+
`Calculator` interface, as shown below. ***To keep things simple we will use
21+
`int`.***
2222

2323
```java
2424
public interface Calculator {
@@ -33,8 +33,8 @@ public interface Calculator {
3333
}
3434
```
3535

36-
To back this `Calculator` interface you created a `BasicCalculator`
37-
implementation as shown below.
36+
To back this `Calculator` interface, you created a `BasicCalculator`
37+
implementation, as shown below.
3838

3939
```java
4040
public class BasicCalculator implements Calculator {
@@ -66,9 +66,9 @@ public class BasicCalculator implements Calculator {
6666

6767
## Static Factory Methods
6868

69-
Calculator API turned out to be very useful and easy to use. Users just have to
70-
create an instance of `BasicCalculator` and then they can use the API. You start
71-
seeing code like the one shown below.
69+
Suppose the Calculator API turned out to be very useful and easy to use. Users
70+
just have to create an instance of `BasicCalculator`, and then they can use the
71+
API. You start seeing code like that shown below.
7272

7373
```java
7474
Calculator calculator = new BasicCalculator();
@@ -78,10 +78,10 @@ BasicCalculator cal = new BasicCalculator();
7878
int difference = cal.subtract(3, 2);
7979
```
8080

81-
Oh no!! Users of the API are not coding to `Calculator` interface instead they
82-
are coding to implementation. Your API didn't enforced users to code to
83-
interfaces as the `BasicCalculator` class was public. If you make
84-
`BasicCalculator` package protected then you would have to provide a static
81+
Oh no! Users of the API are not coding to `Calculator` interface -- instead,
82+
they are coding to its implementation. Your API didn't enforce users to code to
83+
interfaces, as the `BasicCalculator` class was public. If you make
84+
`BasicCalculator` package protected, then you would have to provide a static
8585
factory class that will take care of providing the `Calculator` implementation.
8686
Let's improve the code to handle this.
8787

@@ -94,7 +94,7 @@ class BasicCalculator implements Calculator {
9494
}
9595
```
9696

97-
Next, we will write a factory class that will give us the `Calculator` instance
97+
Next, we will write a factory class that will give us the `Calculator` instance,
9898
as shown below.
9999

100100
```java
@@ -106,23 +106,23 @@ public abstract class CalculatorFactory {
106106
}
107107
```
108108

109-
Now, users will be forced to code to `Calculator` interface and they will not
110-
have access to implementation details.
109+
Now, users will be forced to code to the `Calculator` interface, and they will
110+
not have access to implementation details.
111111

112-
Although we have achieved our goal but we have increased the surface area of our
113-
API by adding a new class `CalculatorFactory`. Now users of the API have to
112+
Although we have achieved our goal, we have increased the surface area of our
113+
API by adding the new class `CalculatorFactory`. Now users of the API have to
114114
learn about one more class before they can use the API effectively. This was the
115115
only solution available before Java 8.
116116

117-
**Java 8 allows you to declare static methods inside an interface**. This will
118-
allow API designers to define static utility methods like `getInstance` in the
119-
interface itself. Hence keeping API short and lean. The static methods inside an
120-
interface could be used to replace static helper classes(`CalculatorFactory`)
121-
that we normally create to define helper methods associated with a type. For
122-
example, `Collections` class is a helper class that defines various helper
123-
methods to work with Collection and associated interfaces. The methods defined
124-
in `Collections` class could easily be added to `Collection` or any of its child
125-
interface.
117+
**Java 8 allows you to declare static methods inside an interface**. This allows
118+
API designers to define static utility methods like `getInstance` in the
119+
interface itself, hence keeping the API short and lean. The static methods
120+
inside an interface could be used to replace static helper classes
121+
(`CalculatorFactory`) that we normally create to define helper methods
122+
associated with a type. For example, the `Collections` class is a helper class
123+
that defines various helper methods to work with Collection and its associated
124+
interfaces. The methods defined in the `Collections` class could easily be added
125+
to `Collection` or any of its child interfaces.
126126

127127
The above code can be improved in Java 8 by adding a static `getInstance` method
128128
in the `Calculator` interface itself.
@@ -148,10 +148,10 @@ public interface Calculator {
148148
## Evolving API with time
149149

150150
Some of the consumers decided to either extend the `Calculator` API by adding
151-
methods like `remainder` or write their own implementation of `Calculator`
151+
methods like `remainder`, or write their own implementation of the `Calculator`
152152
interface. After talking to your users you came to know that most of them would
153-
like to have a `remainder` method added to `Calculator` interface. It looked a
154-
very simple API change so you added one more method to the API.
153+
like to have a `remainder` method added to the `Calculator` interface. It looked
154+
a very simple API change, so you added one more method to the API.
155155

156156
```java
157157
public interface Calculator {
@@ -174,19 +174,20 @@ public interface Calculator {
174174

175175
Adding a method to an interface broke the source compatibility of the API. This
176176
means users who were implementing `Calculator` interface would have to add
177-
implementation for `remainder` method otherwise their code will not compile.
178-
This is a big problem for API designers as it makes API difficult to evolve.
179-
Prior to Java 8, it was not possible to have method implementations inside
180-
interfaces. This often becomes a problem when it was required to extend an API
181-
i.e. adding one or more methods to the interface definition.
177+
implementation for the `remainder` method, otherwise their code would not
178+
compile. This is a big problem for API designers, as it makes APIs difficult to
179+
evolve. Prior to Java 8, it was not possible to have method implementations
180+
inside interfaces. This often became a problem when it was required to extend an
181+
API, i.e. adding one or more methods to the interface definition.
182182

183183
To allow API's to evolve with time, Java 8 allows users to provide default
184184
implementations to methods defined in the interface. These are called
185-
**default** or **defender** methods. The class implementing the interface is not
186-
required to provide implementation of these methods. If implementing class
187-
provides the implementation then implementing class method implementation will
188-
be used else default implementation will be used. `List` interface has few
189-
default methods defined like `replaceAll`, `sort`, and `splitIterator`.
185+
**default**, or **defender** methods. The class implementing the interface is not
186+
required to provide an implementation of these methods. If an implementing class
187+
provides the implementation, then the implementing class method implementation
188+
will be used -- otherwise the default implementation will be used. The `List`
189+
interface has a few default methods defined, like `replaceAll`, `sort`, and
190+
`splitIterator`.
190191

191192
```java
192193
default void replaceAll(UnaryOperator<E> operator) {
@@ -198,9 +199,9 @@ default void replaceAll(UnaryOperator<E> operator) {
198199
}
199200
```
200201

201-
We can solve our API problem by defining a default method as shown below.
202+
We can solve our API problem by defining a default method, as shown below.
202203
Default methods are usually defined using already existing methods --
203-
`remainder` is defined using `subtract`, `multiply`, and `divide` methods.
204+
`remainder` is defined using the `subtract`, `multiply`, and `divide` methods.
204205

205206
```java
206207
default int remainder(int number, int divisor) {
@@ -210,13 +211,13 @@ default int remainder(int number, int divisor) {
210211

211212
## Multiple inheritance
212213

213-
A class can extend a single class but can implement multiple interfaces. Now
214-
that it is feasible to have method implementation in interfaces Java has
215-
multiple inheritance of behavior. Java already had multiple inheritance at type
216-
level but now it also has multiple inheritance at behavior level. There are
217-
three resolution rules that help decide which method will be picked:
214+
A class can extend a single class, but can implement multiple interfaces. Now
215+
that it is feasible to have method implementation in interfaces, Java has
216+
multiple inheritance of behavior. Java already had multiple inheritance at the
217+
type level, but now it also has multiple inheritance at the behavior level.
218+
There are three resolution rules that help decide which method will be picked:
218219

219-
**Rule 1: Methods declared in classes win over method defined in interfaces.**
220+
**Rule 1: Methods declared in classes win over methods defined in interfaces.**
220221

221222
```java
222223
interface A {
@@ -237,8 +238,8 @@ class App implements A{
237238
}
238239
```
239240

240-
This will print `inside App` as methods declared in class have precedence over
241-
methods declared in interfaces.
241+
This will print `inside App`, as methods declared in the implementing class have
242+
precedence over methods declared in interfaces.
242243

243244
**Rule 2: Otherwise, the most specific interface is selected**
244245

@@ -264,7 +265,8 @@ class App implements C, B, A {
264265

265266
This will print `inside C`.
266267

267-
**Rule 3: Otherwise, class has to call the desired implementation explicitly**
268+
**Rule 3: Otherwise, the class has to call the desired implementation
269+
unambiguously**
268270

269271
```java
270272
interface A {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /