comp 121 week 03. agenda review this week’s expected outcomesreview this week’s expected...

27
COMP 121 COMP 121 Week 03 Week 03

Upload: derrick-glenn

Post on 13-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

COMP 121COMP 121

Week 03Week 03

Page 2: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

AgendaAgenda

• Review this week’s expected Review this week’s expected outcomesoutcomes

• Review Guided Learning Activity Review Guided Learning Activity solutionssolutions

• Introduce homework problemsIntroduce homework problems• Question and answer sessionQuestion and answer session

Page 3: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

OutcomesOutcomes• Explain the purpose, uses, and scope of inner Explain the purpose, uses, and scope of inner

classes.classes.• Describe inheritance and the Java mechanisms Describe inheritance and the Java mechanisms

that implement it.that implement it.• Distinguish between overloaded and overridden Distinguish between overloaded and overridden

methods.methods.• Organize a set of related classes into an Organize a set of related classes into an

inheritance hierarchy.inheritance hierarchy.• Determine when and how to call superclass Determine when and how to call superclass

methods from within a subclass.methods from within a subclass.• Apply the rules of implicit and explicit object Apply the rules of implicit and explicit object

conversion.conversion.

Page 4: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Inner ClassesInner Classes• Defined at same level as members and Defined at same level as members and

methodsmethods• Are associated with a “parent” object of Are associated with a “parent” object of

the outer class typethe outer class type• Can access members of the “parent” Can access members of the “parent”

objectobject• Each inner class can independently Each inner class can independently

inherit from an implementation inherit from an implementation (“multiple implementation inheritance”)(“multiple implementation inheritance”)

Page 5: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Inner ClassesInner Classes

Page 6: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided
Page 7: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Guided LearningGuided Learning

Activity Solutions for Week 03Activity Solutions for Week 03

Page 8: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

Activity 3-1Activity 3-1

Outcome: Explain the purpose, uses, Outcome: Explain the purpose, uses, and scope of inner classes.and scope of inner classes.

Page 9: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

InheritanceInheritance• A mechanism for enhancing existing A mechanism for enhancing existing

classesclasses• Base Class - the existing class that is Base Class - the existing class that is

used to define the new class (also called used to define the new class (also called a a superclasssuperclass))

• Derived Class - the new class defined Derived Class - the new class defined using the base class (also called a using the base class (also called a subclasssubclass))

• The subclass inherits behavior and state The subclass inherits behavior and state from the superclassfrom the superclass

• Purpose: code reusePurpose: code reuse

Page 10: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Inheritance (cont’d)Inheritance (cont’d)

• A base class and a derived class have an A base class and a derived class have an “is a” relationship (represented by a solid “is a” relationship (represented by a solid arrow with a “hollow triangle” tip that arrow with a “hollow triangle” tip that points to the points to the superclasssuperclass).).

Animal

Dog

Retriever

Page 11: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

InheritanceInheritanceclass Animal { public void eat() {…} }class Animal { public void eat() {…} }

class Dog extends Animal { public void bark() {…} }class Dog extends Animal { public void bark() {…} }

class Retriever extends Dog {class Retriever extends Dog { public void retrieve() {} }public void retrieve() {} }

class Collie extends Dog { public void saveTimmy() {} }class Collie extends Dog { public void saveTimmy() {} }

  • Animal is the base class for Dog.Animal is the base class for Dog.• Dog is derived from Animal.Dog is derived from Animal.• Retriever is derived from Dog.Retriever is derived from Dog.• Dog is the base class for Retriever.Dog is the base class for Retriever.• Dog and Animal are superclasses of Retriever and Collie.Dog and Animal are superclasses of Retriever and Collie.• Dog, Collie, and Retriever are subclasses of Animal.Dog, Collie, and Retriever are subclasses of Animal.

Page 12: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

InheritanceInheritancepublic class Animal {public class Animal {

private String type;private String type;

  

public Animal(String t) { type = t; }public Animal(String t) { type = t; }

  

public String toString() {public String toString() {

return “This is a “ + type;return “This is a “ + type;

}}

}}

  

Page 13: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

InheritanceInheritanceclass Animal {class Animal {

void toString() { return “This is a “ + type; }void toString() { return “This is a “ + type; }

}}

  

class Dog extends Animal {class Dog extends Animal {

void toString() {void toString() {

return super.toString() + “\nIt’s “ +return super.toString() + “\nIt’s “ +

name + “ the “ + breed;name + “ the “ + breed;

}}

  

public static void main(String[] args) {public static void main(String[] args) {

Dog d = new Dog(“Rex”, “German Shepherd”);Dog d = new Dog(“Rex”, “German Shepherd”);

Dog d2 = new Dog(“Lassie”, “Collie”);Dog d2 = new Dog(“Lassie”, “Collie”);

System.out.println(d + “ “ + d2);System.out.println(d + “ “ + d2);

}}

}}

Page 14: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

Activity 3-2Activity 3-2

Outcome: Describe inheritance and Outcome: Describe inheritance and the Java mechanisms that implement the Java mechanisms that implement it.it.

Activity 3-3Activity 3-3

Outcome: Distinguish between Outcome: Distinguish between overloaded and overridden methods.overloaded and overridden methods.

Page 15: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activitiespublic class Apublic class A

{{

void method1() {}void method1() {}

void method3(String s) {}void method3(String s) {}

}}

  

public class B extends Apublic class B extends A

{{

void method1(int x) { /* Overload – parameters are different */ }void method1(int x) { /* Overload – parameters are different */ }

void method3(String s) { /* Overrides A.method3 */ }void method3(String s) { /* Overrides A.method3 */ }

}}

  

public class C extends Apublic class C extends A

{{

void method1(){ /* Overrides A.method1 */ }void method1(){ /* Overrides A.method1 */ }

void method2(int x, int y) { /* New method */ }void method2(int x, int y) { /* New method */ }

void method2(int y, char c) { /* Overloads method2 */ }void method2(int y, char c) { /* Overloads method2 */ }

}}

Page 16: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activitiespublic class D {public class D {

void method3() {}void method3() {}

void method1() {}void method1() {}

void method2(int y, char c) {}void method2(int y, char c) {}

}}

public class C {public class C {

void method1() {}void method1() {}

void method2(int x, int y) {}void method2(int x, int y) {}

void method2(int y, char c) {}void method2(int y, char c) {}

}}

public class B extends C {public class B extends C {

void method1(int x) { /* Overloads method1 */ }void method1(int x) { /* Overloads method1 */ }

void method3(String s) { /* New method */ }void method3(String s) { /* New method */ }

}}

public class A extends B {public class A extends B {

void method1() { /* Overrides C.method1 */ }void method1() { /* Overrides C.method1 */ }

void method3(String s) { /* Overrides B.method3 */ }void method3(String s) { /* Overrides B.method3 */ }

}}

Page 17: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activities

Activity 3-4Activity 3-4

Outcome: Organize a set of related Outcome: Organize a set of related classes into an inheritance classes into an inheritance hierarchy.hierarchy.

Page 18: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activitiespublic class Publication {public class Publication {

private String isbn;private String isbn;

private int state; // writing, editing, preproduction, doneprivate int state; // writing, editing, preproduction, done

private Date writingDeadline;private Date writingDeadline;

private Date editingDeadline;private Date editingDeadline;

private Date preproductionDeadline;private Date preproductionDeadline;

private Date publicationDeadline;private Date publicationDeadline;

public String getISBN() { return isbn; }public String getISBN() { return isbn; }

public void setISBN(String isbn) { this.isbn = isbn; }public void setISBN(String isbn) { this.isbn = isbn; }

// More get/set methods// More get/set methods

}}

Page 19: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning Activitiespublic class Book extends Publication {public class Book extends Publication {

private boolean isPaperback;private boolean isPaperback;

private String author;private String author;

private double royaltiesPaid;private double royaltiesPaid;

// get/set methods for isPaperback, author, and royaltiesPaid// get/set methods for isPaperback, author, and royaltiesPaid

}}

public class Magazine extends Publication {public class Magazine extends Publication {

private String editor;private String editor;

// get/setEditor// get/setEditor

}}

public class Pamphlet extends Publication {public class Pamphlet extends Publication {

// Nothing here// Nothing here

}}

Page 20: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

InheritanceInheritancepublic class Dog extends Animal {public class Dog extends Animal {

private String name;private String name;

private String breed;private String breed;

  

// Will this work?// Will this work?

public Dog(String n, String b) {public Dog(String n, String b) {

name = n;name = n;

breed = b;breed = b;

}}

  

public Dog(String n, String b) {public Dog(String n, String b) {

super(“Dog”);super(“Dog”);

name = n;name = n;

breed = b;breed = b;

}}

  }}

Page 21: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

InheritanceInheritanceclass Animal {class Animal {

void display() { System.out.println(“This is a “ + type); }void display() { System.out.println(“This is a “ + type); }

}}

  

class Dog extends Animal {class Dog extends Animal {

void display() {void display() {

super.display();super.display();

System.out.println(“\nIt’s “ + name + “ the “ + breed);System.out.println(“\nIt’s “ + name + “ the “ + breed);

}}

  

public static void main(String[] args) {public static void main(String[] args) {

Dog d = new Dog(“Rex”, “German Shepherd”);Dog d = new Dog(“Rex”, “German Shepherd”);

Dog d2 = new Dog(“Lassie”, “Collie”);Dog d2 = new Dog(“Lassie”, “Collie”);

d.display();d.display();

d2.display();d2.display();

}}

}}

Page 22: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Inheritance and CastingInheritance and Castingclass Dog {…}class Dog {…}

class Husky extends Dog {…}class Husky extends Dog {…}

Dog d = new Husky(); // Legal – implicit castDog d = new Husky(); // Legal – implicit cast

Husky h = d; // Illegal – needs explicit castHusky h = d; // Illegal – needs explicit cast

Husky h = (Husky)d; // LegalHusky h = (Husky)d; // Legal

Husky h = (Husky)new Dog(); // Throws exceptionHusky h = (Husky)new Dog(); // Throws exception

Page 23: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning ActivitiesActivity 3-5Activity 3-5

Outcome: Determine when and how to call Outcome: Determine when and how to call superclass methods from within a superclass methods from within a subclass.subclass.

Activity 3-6Activity 3-6

Outcome: Apply the rules of implicit and Outcome: Apply the rules of implicit and explicit object conversion.explicit object conversion.

Page 24: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning ActivitiesPolygon is the superclass, Triangle is the subclassPolygon is the superclass, Triangle is the subclass

Polygon p = new Triangle();Polygon p = new Triangle();

Triangle t = (Triangle)p; // cast requiredTriangle t = (Triangle)p; // cast required

Triangle t = (Triangle)new Polygon(); // ClassCastExceptionTriangle t = (Triangle)new Polygon(); // ClassCastException

GraduateStudent is the subclass, Student is the superclassGraduateStudent is the subclass, Student is the superclass

Student s = new GraduateStudent();Student s = new GraduateStudent();

GraduateStudent gs = (GraduateStudent)s; // cast requiredGraduateStudent gs = (GraduateStudent)s; // cast required

Person is the superclass, Student is the subclassPerson is the superclass, Student is the subclass

Person p = new Student();Person p = new Student();

Student s = (Student)p;Student s = (Student)p;

Page 25: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Learning ActivitiesLearning ActivitiesVehicle is the superclass, Car is the subclassVehicle is the superclass, Car is the subclass

Vehicle v = new Car();Vehicle v = new Car();

Car c = (Car)v; // cast requiredCar c = (Car)v; // cast required

Vehicle is the superclass, Minivan is the subclassVehicle is the superclass, Minivan is the subclass

Vehicle v = new Minivan();Vehicle v = new Minivan();

Minivan m = (Minivan)v; // cast requiredMinivan m = (Minivan)v; // cast required

Vehicle is the superclass, Truck is the subclassVehicle is the superclass, Truck is the subclass

Vehicle v = new Truck();Vehicle v = new Truck();

Truck t = (Truck)v; // cast requiredTruck t = (Truck)v; // cast required

Page 26: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Homework AssignmentsHomework Assignments

• Due this weekDue this week• Homework 1Homework 1

• Due next weekDue next week• Lab 1Lab 1• Homework 2Homework 2• Draft Reflection PaperDraft Reflection Paper

Page 27: COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided

Question and Answer SessionQuestion and Answer Session