Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!

Chris Devine

Greenhorn
+ Follow
since Jun 13, 2012
Chris likes ...
Mac Clojure Linux
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Chris Devine

Clojure is a functional programming language and a derivative of Lisp. As a result of its functional and declarative nature it can be difficult to understand for those (like me) coming from an object-oriented/imperative programming background.

The most obvious difference is the syntax of its functions. As a Lisp it uses Polish or prefix notation. This allows an arbitrary number of arguments to be passed to any function (which must be the first item in a list). For example, the addition operator '+' only needs to be given once as the first argument in a list and it can take any number of other arguments to add:
=> (+ 1 2 3 4 5)
15

In Java you would have to use the '+' operator between every value being added:
int sum = 1 + 2 + 3 + 4 + 5;

You can also pass functions as arguments to other functions, since Clojure views data as code and code as data. I won't try to explain all the implications of this statement, but it's crucial for understanding functional programming.
9 years ago

Alan Cowap wrote:

Chris Devine wrote:In the Chapter 1 Self-test, question 6:

The answer given (A. Compilation succeeds) seems incorrect. If the Device interface contains the method public void doIt(), then all classes implementing that interface should implement that method. However, the class Phone 3 implements the Device interface but only implements a doStuff() method. This would cause a compile error due to a mistake on line 7. Answer E should be the correct answer in that case.



Hi Chris,

The answer in the book is correct.
Note that "Phone3 extends Electronic" and that "Electronic implements Device { public void doIt() { } }"
Hence, Phone3 inherits the "public void doIt() { }" implementation from Electronic.
Aside, there's no need for Phone3 to explicitly state it implements Device, since Phone3 already inherits that interface from Electronic.

Regards,
Al




Thanks, Al. I see that now. One my biggest difficulties (being new to Java and coding in general) is being able to see when and how things work even if they violate good programming practices and conventions. Having a class implement an interface that its superclass already implements appears to be one of those cases where just because it's legal doesn't mean it's right.

All the best,
Chris
In the Chapter 1 Self-test, question 6:

6. Given:

1. public class Electronic implements Device { public void doIt() { } }
2.
3. abstract class Phone1 extends Electronic { }
4.
5. abstract class Phone2 extends Electronic { public void doIt(int x) { } }
6.
7. class Phone3 extends Electronic implements Device { public void doStuff() { } }
8.
9. interface Device { public void doIt(); }

What is the result? (Choose all that apply.)
A. Compilation succeeds
B. Compilation fails with an error on line 1
C. Compilation fails with an error on line 3
D. Compilation fails with an error on line 5
E. Compilation fails with an error on line 7
F. Compilation fails with an error on line 9

Answer:
A is correct; all of these are legal declarations.
B, C, D, E, and F are incorrect based on the above information. (Objective 1.2)


The answer given (A. Compilation succeeds) seems incorrect. If the Device interface contains the method public void doIt(), then all classes implementing that interface should implement that method. However, the class Phone 3 implements the Device interface but only implements a doStuff() method. This would cause a compile error due to a mistake on line 7. Answer E should be the correct answer in that case.