Transcript COMP 311

COMP 121
Week 04
Solutions
• Review Homework 1 Solutions
Outcomes
• Distinguish between abstract and concrete classes and
methods.
• Describe the uses and effects of the keywords final,
public, protected, and private as they apply to classes,
methods, and instance fields.
• Supply appropriate equals, toString, and clone
methods for base, derived, and composed classes.
• Apply appropriate access modifiers within class design.
• Recognize and apply the Template Method design
pattern to solve a given problem.
Abstract Classes
• Can’t be instantiated
• May contain abstract methods that derived
classes will be required to implement
• May contain concrete methods with
implementations as well as member variables
• An interface can be thought of as a pure
abstract class
Abstract Classes
abstract class Dog {
private boolean hungry = true;
public abstract void bark();
public void eat() { hungry = false; }
}
class Husky extends Dog {
public void bark() { System.out.println(“Woof!”); }
}
class Chihuahua extends Dog {
public void bark() { System.out.println(“Yip!”); }
}
Guided Learning
Activity Solutions for Week 04
Learning Activities
Activity 4-1
Outcome: Distinguish between abstract
and concrete classes and methods.
Access Modifiers
• public – can be accessed from any class
• private – can only be accessed from
inside the declaring class
• default – can only be accessed from
inside the same package
• protected – can be accessed from the
same package, or a derived class
Access Modifiers
class A {
int a;
public int b;
protected int c;
private int d;
}
// In same package
class B extends A {
void foo() {
a = 1;
b = 1;
c = 1;
d = 1; // Error
}
}
Access Modifiers
class A {
int a;
public int b;
protected int c;
private int d;
}
// In different package
class C extends A {
void foo() {
a = 1; // Error
b = 1;
c = 1;
d = 1; // Error
}
}
Learning Activities
Activity 4-2
Outcome: Describe the uses and effects of
the keywords final, public, protected, and
private as they apply to classes, methods,
and instance fields.
The Object Class
• equals() – compares objects using ==
• clone() – makes a shallow copy
• toString() – prints class name and
hash code (Point@65fab3e)
Implementing equals()
public class Student {
private String firstName;
private String lastName;
public boolean equals(Object o) {
Student s2 = (Student)o;
return firstName.equals(s2.firstName) &&
lastName.equals(s2.lastName);
}
}
Implementing toString()
public class Student {
private String firstName;
private String lastName;
public String toString() {
return firstName + “ “ + lastName;
}
public static void main(String [] args) {
Student s = new Student(“John”, “Doe”);
System.out.println(s);
}
}
Implementing clone()
public class Circle implements Cloneable {
private int r;
private Point center;
public Object clone() {
Circle cloned = super.clone();
cloned.center = new Point(center.getX(), center.getY());
return cloned;
}
public static void main(String [] args) {
Circle c = new Circle(5, new Point(3, 3));
Circle c2 = c.clone();
c.getCenter().setX(2);
System.out.println(c + “ “ + c2);
}
}
Learning Activities
Activity 4-3
Outcome: Supply appropriate equals, toString,
and clone methods for base, derived, and
composed classes.
Activity 4-4
Outcome: Apply appropriate access modifiers
within class design.
Template Method Pattern
• Creates a fixed structure for an
algorithm
• Lets the details of each step change
Template Method Pattern
public class PizzaShop {
public final void makePizza() {
makeDough();
tossDough();
addToppings();
cook();
slice();
}
public
public
public
public
public
}
void
void
void
void
void
makeDough() {…}
tossDough() {…}
addToppings() {…}
cook() {…}
slice() {…}
Learning Activities
Activity 4-5
Outcome: Recognize and apply the
Template Method design pattern to solve
a given problem.
Homework Assignments
• Due this week
• Lab 1
• Homework 2
• Reflection Paper Draft
• Next week
• Exam 1
Question and Answer Session