ECE%20122 - PowerPoint PPT Presentation

About This Presentation
Title:

ECE%20122

Description:

animal[0] = new Animal();//object instantiation. Create an array of every animal in the Zoo? ... if we don't use polymorphism? performance(dog); performance ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 13
Provided by: wwwaliC
Category:
Tags: ece | use

less

Transcript and Presenter's Notes

Title: ECE%20122


1
ECE 122
2
We know this
  • Cat cat new Cat()
  • Animal animal new Animal()

3
and this?
  • Animal animal new Cat()
  • An object reference of super class type points to
    an object of sub class type.
  • It is legal and it is called polymorphism.

4
Motivation
  • Lets introduce array first.

5
Array
  • Syntax
  • Type array-name new typesize
  • int number new int3
  • Animal animal new Animal3//array
    declaration
  • Array index start from 0 number0, number1,
    number2
  • animal0 new Animal()//object instantiation

6
Create an array of every animal in the Zoo?
  • Animal animal new Animal1000
  • animal0 new Dog()
  • animal1 new Cat()
  • animal2 new Tiger()
  • animal3 new Lion()

7
Super class object in parameter list of an method
  • public void performance(Animal animal)
  • animal.makeNoise()

8
Let each animal in the zoo to give a performance
in sequence
  • for (int i 0 i lt 1000 i)
  • performance(animali)

9
What if we dont use polymorphism?
  • performance(dog)
  • performance(cat)
  • performance(tiger)
  • performance(lion)
  • .//total 1000 times.

10
Demo MyAnimalList.java

11
Demo MyDoctor.java

12
Assignments
  • Read Head first Java Chapter 8
Write a Comment
User Comments (0)
About PowerShow.com