Download presentation
Presentation is loading. Please wait.
Published byDerrick Glenn Modified over 9 years ago
1
COMP 121 Week 03
2
Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided Learning Activity solutions Introduce homework problemsIntroduce homework problems Question and answer sessionQuestion and answer session
3
Outcomes Explain the purpose, uses, and scope of inner classes.Explain the purpose, uses, and scope of inner classes. Describe inheritance and the Java mechanisms that implement it.Describe inheritance and the Java mechanisms that implement it. Distinguish between overloaded and overridden methods.Distinguish between overloaded and overridden methods. Organize a set of related classes into an inheritance hierarchy.Organize a set of related classes into an inheritance hierarchy. Determine when and how to call superclass methods from within a subclass.Determine when and how to call superclass methods from within a subclass. Apply the rules of implicit and explicit object conversion.Apply the rules of implicit and explicit object conversion.
4
Inner Classes Defined at same level as members and methodsDefined at same level as members and methods Are associated with a “parent” object of the outer class typeAre associated with a “parent” object of the outer class type Can access members of the “parent” objectCan access members of the “parent” object Each inner class can independently inherit from an implementation (“multiple implementation inheritance”)Each inner class can independently inherit from an implementation (“multiple implementation inheritance”)
5
Inner Classes
7
Guided Learning Activity Solutions for Week 03
8
Learning Activities Activity 3-1 Outcome: Explain the purpose, uses, and scope of inner classes.
9
Inheritance A mechanism for enhancing existing classesA mechanism for enhancing existing classes Base Class - the existing class that is used to define the new class (also called a superclass)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)Derived Class - the new class defined using the base class (also called a subclass) The subclass inherits behavior and state from the superclassThe subclass inherits behavior and state from the superclass Purpose: code reusePurpose: code reuse
10
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).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
11
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.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.
12
Inheritance public class Animal { private String type; public Animal(String t) { type = t; } public String toString() { return “This is a “ + type; }}
13
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); }}
14
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.
15
Learning Activities public class A { void method1() {} void method1() {} void method3(String s) {} void method3(String s) {}} public 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 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 */ }}
16
Learning Activities 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 { 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 { 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 { void method1() { /* Overrides C.method1 */ } void method1() { /* Overrides C.method1 */ } void method3(String s) { /* Overrides B.method3 */ } void method3(String s) { /* Overrides B.method3 */ }}
17
Learning Activities Activity 3-4 Outcome: Organize a set of related classes into an inheritance hierarchy.
18
Learning Activities public class Publication { private String isbn; private String isbn; private int state; // writing, editing, preproduction, done private 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}
19
Learning Activities 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 { private String editor; private String editor; // get/setEditor // get/setEditor} public class Pamphlet extends Publication { // Nothing here // Nothing here}
20
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; } }
21
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(); }}
22
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
23
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.
24
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(); GraduateStudent gs = (GraduateStudent)s; // cast required Person is the superclass, Student is the subclass Person p = new Student(); Student s = (Student)p;
25
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
26
Homework 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
27
Question and Answer Session
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.