Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110 More about inheritance

Similar presentations


Presentation on theme: "COMP 110 More about inheritance"— Presentation transcript:

1 COMP 110 More about inheritance
Michele Weigle - COMP 14 - Spr 04 COMP 110 More about inheritance Catie Welsh April 13, 2011

2 Michele Weigle - COMP 14 - Spr 04
Announcements Program 4 early code check due Friday, April 15th in class Bring a print out of your code with you – whatever you have so far Also include a copy of any pseudocode you’ve written out To earn the 25 points, I’d like to see a significant amount of code written that does 1 of the following Game board code written – numbers randomly assigned, 2 of each # Mouse clicking code written so that your shapes are centered in the correct square of the grid You can NOT use late days for this! I must have a copy of your code by 1pm on Friday!

3 Michele Weigle - COMP 14 - Spr 04
Questions? Any other questions?

4 Michele Weigle - COMP 14 - Spr 04
Today in COMP 110 Lab 8 Some review More inheritance

5 Silly exercise time What’s the point? Well…

6 The difference between return and printing
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

7 The difference between return and printing
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?

8 The difference between return and printing
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?

9 The difference between return and printing
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

10 Review: Inheritance What is a base class? What is a derived class?

11 Review: Inheritance We have discussed before how classes of objects can have relationships Person Transportation Student Employee Car Airplane Animal Undergrad Grad Faculty Staff Horse Elephant Camel Masters Doctoral Nondegree

12 Put these classes into an inheritance heirarchy
Crocodile Human Reptile Animal Mammal Whale Animal Reptile Mammal Crocodile Human Whale

13 Review: the is-a relationship
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!

14 Review: Inheritance Define a general class
Later, define specialized classes based on the general class These specialized classes inherit properties from the general class

15 Review: How does this work in Java?
public class Person { private String name; public Person() name = “No name yet”; } public void setName(String newName) name = newName; public String getName() return name; Person - name + setName(String newName) : void + getName() : String

16 Review: How does this work in Java?
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; Person - name + setName(String newName) : void + getName() : String Student - id + setID(int idNumber) : void + getID() : int

17 Overriding methods What if the class Person had a method called printInfo? public class Person { // a bunch of other stuff // ... public void printInfo() System.out.println(name); }

18 Overriding methods What if the class Student also had a method called printInfo? public class Student extends Person { // a bunch of other stuff // ... public void printInfo() System.out.println("Name: " + getName()); System.out.println("ID: " + getID()); }

19 Overriding methods: example
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

20 Overriding methods: why is it useful?
You often want derived classes to perform custom behavior For example, drawing shapes 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…); }

21 Type compatibilities Given this inheritance heirarchy… Person Athlete
HighJumper ExtremeAthlete Skydiver XGamesSkater

22 Is this code legal? Yes! Person p = new Person(); Person Athlete
HighJumper ExtremeAthlete Skydiver XGamesSkater

23 Is this code legal? Yes! HighJumper h = new HighJumper(); Person
Athlete HighJumper ExtremeAthlete Skydiver XGamesSkater

24 Is this code legal? Yes! An Athlete is a Person, so this is okay
Person p = new Athlete(); Yes! An Athlete is a Person, so this is okay Person Athlete HighJumper ExtremeAthlete Skydiver XGamesSkater

25 Is this code legal? Skydiver s = new Person(); No! A Person is not necessarily a Skydiver, so this is illegal Person Athlete HighJumper ExtremeAthlete Skydiver XGamesSkater

26 Is this code legal? Athlete ath = new Athlete(); XGamesSkater xgs = ath; No! An Athlete is not necessarily an XGamesSkater, so this is illegal Person Athlete HighJumper ExtremeAthlete Skydiver XGamesSkater

27 What’s going on? 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

28 The class Object Every class in Java is derived from the class Object
Every class in Java is an Object Object Animal Person Reptile Mammal Student Employee Crocodile Human Whale

29 Friday Program 4 help – In-class code check


Download ppt "COMP 110 More about inheritance"

Similar presentations


Ads by Google