1 / 27

COMP 121

COMP 121. Week 03. Agenda. Review this week’s expected outcomes Review Guided Learning Activity solutions Introduce homework problems Question and answer session. Outcomes. Explain the purpose, uses, and scope of inner classes.

sage
Télécharger la présentation

COMP 121

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 121 Week 03

  2. Agenda • Review this week’s expected outcomes • Review Guided Learning Activity solutions • Introduce homework problems • Question and answer session

  3. Outcomes • Explain the purpose, uses, and scope of inner classes. • Describe inheritance and the Java mechanisms that implement it. • Distinguish between overloaded and overridden methods. • Organize a set of related classes into an inheritance hierarchy. • Determine when and how to call superclass methods from within a subclass. • Apply the rules of implicit and explicit object conversion.

  4. Inner Classes • Defined at same level as members and methods • Are associated with a “parent” object of the outer class type • Can access members of the “parent” object • Each inner class can independently inherit from an implementation (“multiple implementation inheritance”)

  5. Inner Classes

  6. Guided Learning Activity Solutions for Week 03

  7. Learning Activities Activity 3-1 Outcome: Explain the purpose, uses, and scope of inner classes.

  8. Inheritance • A mechanism for enhancing existing classes • Base Class - the existing class that is used to define the new class (also called a superclass) • Derived Class - the new class defined using the base class (also called a subclass) • The subclass inherits behavior and state from the superclass • Purpose: code reuse

  9. Inheritance (cont’d) A base class and a derived class have an “is a” relationship (represented by a solid arrow with a “hollow triangle” tip that points to the superclass). Animal Dog Retriever

  10. Inheritance class Animal { public void eat() {…} } class Dog extends Animal { public void bark() {…} } class Retriever extends Dog { public void retrieve() {} } class Collie extends Dog { public void saveTimmy() {} } • Animal is the base class for Dog. • Dog is derived from Animal. • Retriever is derived from Dog. • Dog is the base class for Retriever. • Dog and Animal are superclasses of Retriever and Collie. • Dog, Collie, and Retriever are subclasses of Animal.

  11. Inheritance public class Animal { private String type; public Animal(String t) { type = t; } public String toString() { return “This is a “ + type; } }

  12. Inheritance class Animal { void toString() { return “This is a “ + type; } } class Dog extends Animal { void toString() { return super.toString() + “\nIt’s “ + name + “ the “ + breed; } public static void main(String[] args) { Dog d = new Dog(“Rex”, “German Shepherd”); Dog d2 = new Dog(“Lassie”, “Collie”); System.out.println(d + “ “ + d2); } }

  13. Learning Activities Activity 3-2 Outcome: Describe inheritance and the Java mechanisms that implement it. Activity 3-3 Outcome: Distinguish between overloaded and overridden methods.

  14. Learning Activities public class A { void method1() {} void method3(String s) {} } public class B extends A { void method1(int x) { /* Overload – parameters are different */ } void method3(String s) { /* Overrides A.method3 */ } } public class C extends A { void method1(){ /* Overrides A.method1 */ } void method2(int x, int y) { /* New method */ } void method2(int y, char c) { /* Overloads method2 */ } }

  15. Learning Activities public class D { void method3() {} void method1() {} void method2(int y, char c) {} } public class C { void method1() {} void method2(int x, int y) {} void method2(int y, char c) {} } public class B extends C { void method1(int x) { /* Overloads method1 */ } void method3(String s) { /* New method */ } } public class A extends B { void method1() { /* Overrides C.method1 */ } void method3(String s) { /* Overrides B.method3 */ } }

  16. Learning Activities Activity 3-4 Outcome: Organize a set of related classes into an inheritance hierarchy.

  17. Learning Activities public class Publication { private String isbn; private int state; // writing, editing, preproduction, done private Date writingDeadline; private Date editingDeadline; private Date preproductionDeadline; private Date publicationDeadline; public String getISBN() { return isbn; } public void setISBN(String isbn) { this.isbn = isbn; } // More get/set methods }

  18. Learning Activities public class Book extends Publication { private booleanisPaperback; private String author; private double royaltiesPaid; // get/set methods for isPaperback, author, and royaltiesPaid } public class Magazine extends Publication { private String editor; // get/setEditor } public class Pamphlet extends Publication { // Nothing here }

  19. Inheritance public class Dog extends Animal { private String name; private String breed; // Will this work? public Dog(String n, String b) { name = n; breed = b; } public Dog(String n, String b) { super(“Dog”); name = n; breed = b; }  }

  20. Inheritance class Animal { void display() { System.out.println(“This is a “ + type); } } class Dog extends Animal { void display() { super.display(); System.out.println(“\nIt’s “ + name + “ the “ + breed); } public static void main(String[] args) { Dog d = new Dog(“Rex”, “German Shepherd”); Dog d2 = new Dog(“Lassie”, “Collie”); d.display(); d2.display(); } }

  21. Inheritance and Casting class Dog {…} class Husky extends Dog {…} Dog d = new Husky(); // Legal – implicit cast Husky h = d; // Illegal – needs explicit cast Husky h = (Husky)d; // Legal Husky h = (Husky)new Dog(); // Throws exception

  22. Learning Activities Activity 3-5 Outcome: Determine when and how to call superclass methods from within a subclass. Activity 3-6 Outcome: Apply the rules of implicit and explicit object conversion.

  23. Learning Activities Polygon is the superclass, Triangle is the subclass Polygon p = new Triangle(); Triangle t = (Triangle)p; // cast required Triangle t = (Triangle)new Polygon(); // ClassCastException GraduateStudent is the subclass, Student is the superclass Student s = new GraduateStudent(); GraduateStudentgs = (GraduateStudent)s; // cast required Person is the superclass, Student is the subclass Person p = new Student(); Student s = (Student)p;

  24. Learning Activities Vehicle is the superclass, Car is the subclass Vehicle v = new Car(); Car c = (Car)v; // cast required Vehicle is the superclass, Minivan is the subclass Vehicle v = new Minivan(); Minivan m = (Minivan)v; // cast required Vehicle is the superclass, Truck is the subclass Vehicle v = new Truck(); Truck t = (Truck)v; // cast required

  25. Homework Assignments • Due this week • Homework 1 • Due next week • Lab 1 • Homework 2 • Draft Reflection Paper

  26. Question and Answer Session

More Related