COMP 121 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

COMP 121

Description:

{ Dog d = new Dog('Rex', 'German Shepherd'); Dog d2 = new Dog('Lassie', 'Collie' ... { Dog d = new Dog('Rex', 'German Shepherd'); Dog d2 = new Dog('Lassie' ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 26
Provided by: toddwhi
Category:
Tags: comp | german | shepherd

less

Transcript and Presenter's Notes

Title: COMP 121


1
COMP 121
  • Week 03

2
Agenda
  • Review this weeks 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

5
Inner Classes
  • public class ProgramGuide
  • private Listing listings
  • private interface ListingComparator
  • int compare(Listing first, Listing second)
  • private class ChannelListingComparator
  • implements ListingComparator
  • public int compare(Listing first, Listing
    second)
  • // code
  • // NameListingComparator here, too.

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
  • Derivation - The process of defining a new class
    based on an existing class.
  • Base Class - The existing class that is used to
    define the new class.
  • Derived Class - The new class defined using the
    base class.
  • Inheritance - The inclusion of members and
    methods from a base class in a derived class.
  • A base class and a derived class should have an
    is a relationship.

9
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.

10
Inheritance
  • public class Animal
  • private String type
  •  
  • public Animal(String t) type t
  •  
  • public String toString()
  • return This is a type
  •  

11
Inheritance
  • class Animal
  • void toString() return This is a type
  •  
  • class Dog extends Animal
  • void toString()
  • return super.toString() \nIts
  • 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)

12
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.

13
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 /

14
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 /

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

16
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

17
Learning Activities
  • public class Book extends Publication
  • private boolean isPaperback
  • 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

18
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
  •  

19
Inheritance
  • class Animal
  • void display() System.out.println(This is a
    type)
  •  
  • class Dog extends Animal
  • void display()
  • super.display()
  • System.out.println(\nIts 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()

20
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

21
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.

22
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

23
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

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

25
  • Question and Answer Session
Write a Comment
User Comments (0)
About PowerShow.com