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. • 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.
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”)
Inner Classes
Guided Learning Activity Solutions for Week 03
Learning Activities Activity 3-1 Outcome: Explain the purpose, uses, and scope of inner classes.
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
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
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.
Inheritance public class Animal { private String type; public Animal(String t) { type = t; } public String toString() { return “This is a “ + type; } }
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); } }
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.
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 */ } }
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 */ } }
Learning Activities Activity 3-4 Outcome: Organize a set of related classes into an inheritance hierarchy.
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 }
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 }
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; } }
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(); } }
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
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.
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;
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
Homework Assignments • Due this week • Homework 1 • Due next week • Lab 1 • Homework 2 • Draft Reflection Paper
Question and Answer Session