Darrell Bethea June 7, 2011 3 Some review More inheritance 4 - - PowerPoint PPT Presentation

darrell bethea june 7 2011 3 some review more inheritance
SMART_READER_LITE
LIVE PREVIEW

Darrell Bethea June 7, 2011 3 Some review More inheritance 4 - - PowerPoint PPT Presentation

Darrell Bethea June 7, 2011 3 Some review More inheritance 4 Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller to do


slide-1
SLIDE 1

Darrell Bethea June 7, 2011

slide-2
SLIDE 2

3

slide-3
SLIDE 3

 Some review  More inheritance

4

slide-4
SLIDE 4

 Returning a value from a method does NOT

print it out to the screen

  • The returned value is given to the caller of the

method

  • It is up to the method caller to do something with

this value

6

slide-5
SLIDE 5

public class NumberUtils { private static Random rnd = new Random(); public static int roll20SidedDie() { return rnd.nextInt(20) + 1; } } public class Tester { public static void main(String[] args) { NumberUtils.roll20SidedDie(); // what does this do? } }

 Generates a random die roll, but it ignores the value

returned from the roll20SidedDie method

 Have we printed anything to the screen?

7

slide-6
SLIDE 6

public class NumberUtils { private static Random rnd = new Random(); public static int roll20SidedDie() { return rnd.nextInt(20) + 1; } } public class Tester { public static void main(String[] args) { int num = NumberUtils.roll20SidedDie(); // what does this do? } }

 Puts the random number returned by roll20SidedDie

into the variable num

 Have we printed anything to the screen?

8

slide-7
SLIDE 7

public class NumberUtils { private static Random rnd = new Random(); public static int roll20SidedDie() { return rnd.nextInt(20) + 1; } } public class Tester { public static void main(String[] args) { int num = NumberUtils.roll20SidedDie(); // what does this do? System.out.println(num); } }

 Puts the random number returned by roll20SidedDie into the

variable num

 Then prints the value of num to the screen

9

slide-8
SLIDE 8

 What is a base class?  What is a derived class?

10

slide-9
SLIDE 9

 We have discussed before how classes of

  • bjects can have relationships

11

Person Student Employee Undergrad Grad Masters Doctoral Nondegree Faculty Stafg Transportation Car Airplane Animal Elephant Horse Camel

slide-10
SLIDE 10

12

Animal Reptile Mammal Human Crocodile Whale Animal Reptile Mammal Human Crocodile Whale

slide-11
SLIDE 11

 This inheritance relationship is known as an

is-a relationship

 A Doctoral student is a Grad student  A Grad student is a Student  A Student is a Person  Is a Person a Student?

  • Not necessarily!

13

slide-12
SLIDE 12

 Define a general class  Later, define specialized classes based on the

general class

 These specialized classes inherit properties

from the general class

14

slide-13
SLIDE 13

public class Person { private String name; public Person() { name = “No name yet”; } public void setName(String newName) { name = newName; } public String getName() { return name; } }

15

Person

  • name

+ setName(String newName) : void + getName() : String

slide-14
SLIDE 14

public class Student extends Person { private int id; public Student() { super(); id = 0; } public Student(String stdName, int idNumber) { setName(stdName); setID(idNumber); } public void setID(int idNumber) { id = idNumber; } public int getID() { return id; } }

16

Student

  • id

+ setID(int idNumber) : void + getID() : int

Person

  • name

+ setName(String newName) : void + getName() : String

slide-15
SLIDE 15

 What if the class Person had a method called

printInfo?

public class Person { // a bunch of other stufg // ... public void printInfo() { System.out.println(name); } }

17

slide-16
SLIDE 16

 What if the class Student also had a method

called printInfo?

public class Student extends Person { // a bunch of other stufg // ... public void printInfo() { System.out.println("Name: " + getName()); System.out.println("ID: " + getID()); } }

18

slide-17
SLIDE 17

 Both Person and Student have a printInfo()

method

Student std = new Student("John Smith", 37183); std.printInfo(); // calls Student’s printInfo method, // not Person’s  Output would be: Name: John Smith ID: 37183

19

slide-18
SLIDE 18

 You often want derived classes to perform

custom behavior

 For example, drawing shapes

20

public class Shape { public void draw(Graphics g) { } } public class Circle extends Shape { public void draw(Graphics g) { g.drawOval(…arguments…); } } public class Rectangle extends Shape { public void draw(Graphics g) { g.drawRect(…arguments…); } }

slide-19
SLIDE 19

 Given this inheritance heirarchy…

21

Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

slide-20
SLIDE 20

 Person p = new Person();

  • Yes!

22

Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

slide-21
SLIDE 21

 HighJumper h = new HighJumper();

  • Yes!

23

Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

slide-22
SLIDE 22

 Person p = new Athlete();

  • Yes! An Athlete is a Person, so this is okay

24

Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

slide-23
SLIDE 23

 Skydiver s = new Person();

  • No! A Person is not necessarily a Skydiver, so

this is illegal

25

Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

slide-24
SLIDE 24

 Athlete ath = new Athlete();

XGamesSkater xgs = ath;

  • No! An Athlete is not necessarily an XGamesSkater,

so this is illegal

26

Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

slide-25
SLIDE 25

 We are creeping up to the idea of polymorphism

  • Enables the substitution of one object for another as

long as the objects have the same interface

  • More details later

27

slide-26
SLIDE 26

 Every class in Java is derived from the class

Object

  • Every class in Java is an Object

28

Animal Reptile Mammal Human Crocodile Whale Object Person Student Employee