Download Interfaces and Inheritance

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
3.6 Interfaces and Inheritance
Section 3.6
1
Interfaces
Client doesn't care about implementation:
public class Transactions {
public static void main(String[] args) {
QueueOfDoubles q = new QueueOfDoubles();
while (!StdIn.isEmpty()) {
q.enqueue(StdIn.readDouble());
}
for (int i = 0; !q.isEmpty(); i++) {
StdOut.println("Processing trans. " + i);
q.dequeue();
}
}
}
Section 3.6
2
Interfaces
Client doesn't care about implementation:
public interface QueueOfDoubles {
public void enqueue(double d);
public void dequeue();
public boolean isEmpty();
}
public class LinkedQueueOfDoubles implements QueueOfDoubles {
public void enqueue(double d) { ... }
public void dequeue() { ... }
public boolean isEmpty() { ... }
} public class SentinelLinkedQueueOfDoubles implements QueueOfDoubles {
public void enqueue(double d) { ... }
public void dequeue() { ... }
public boolean isEmpty() { ... }
public class RecursiveLinkedQueueOfDoubles implements QueueOfDoubles {
}
public void enqueue(double d) { ... }
public void dequeue() { ... }
public boolean isEmpty() { ... }
}
Section 3.6
3
Interfaces
Client doesn't care about implementation:
public class Transactions {
public static void main(String[] args) {
QueueOfDoubles q = new LinkedQueueOfDoubles();
while (!StdIn.isEmpty()) {
q.enqueue(StdIn.readDouble());
}
for (int i = 0; !q.isEmpty(); i++) {
StdOut.println("Processing trans. " + i);
q.dequeue();
}
}
}
Section 3.6
4
Inheritance: Specialization
"Grunt"
Section 3.6
5
Inheritance: Specialization
"Grunt"
"Woof "
Section 3.6
6
Inheritance: Specialization
"Grunt"
"Oink"
"Woof "
Section 3.6
7
Inheritance: Specialization
"Grunt"
"I t hink I taw
a puddy tat."
"Oink"
"Woof "
Section 3.6
8
Inheritance: Specialization
public class Animal {
public String toString() {
return "Grunt";
}
}
public class Dog extends Animal {
public String toString() {
return "Woof";
}
}
public class Pig extends Animal {
public String toString() {
return "Oink";
}
}
public class Bird extends Animal {
public String toString() {
return "I think I taw a puddy tat";
}
}
Section 3.6
9
Inheritance: Specialization
public class Animal {
public String toString() {
public class AnimalTest {
return "Grunt";
public static void main(String[] args) {
}
Animal a = new Animal();
}
Animal b = new Dog();
Animal c = new Pig();
public class Dog extends Animal { Animal d = new Bird();
public String toString() {
System.out.println(a);
return "Woof";
System.out.println(b);
}
System.out.println(c);
}
System.out.println(d);
}
public class Pig extends Animal {
}
public String toString() {
% java AnimalTest
return "Oink";
Grunt
}
Woof
}
public class Bird extends Animal {
public String toString() {
return "I think I taw a puddy tat";
}
}
Oink
I think I taw a puddy tat
Section 3.6
10
Inheritance: Extending Queues
Need a queue that prints the total of all its values:
• Why re-write queue from scratch
• enqueue(), dequeue(), and isEmpty() are all fine
• Just rewrite toString()!
public class SummingQueue extends LinkedQueueOfDoubles {
public String toString() {
String s = super.toString();
int sum = 0;
for (Node n = head; n != null; n = n.next)
sum += n.val;
s += "The total is " + sum;
return s;
}
}
Section 3.6
11
Related documents