MIT Lec Inherit
-
Upload
naimun-siraj -
Category
Documents
-
view
15 -
download
3
description
Transcript of MIT Lec Inherit
![Page 1: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/1.jpg)
Lecture 7
Review
Exceptions
IO
![Page 2: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/2.jpg)
Review
![Page 3: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/3.jpg)
Interfaces? Interfaces!
• It’s a contract!
• If you must implement ALL the methods• All fields are final (cannot be changed)
public interface ICar {boolean isCar = true;
int getNumWheels();}
![Page 4: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/4.jpg)
BigRig
class BigRig implements ICar { int getNumWheels() {
return 18; }
}
![Page 5: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/5.jpg)
That Homework!
• Bouncer draws a Sprite that – Moves around
– Bounces in a box
• A Sprite is an interface – You can draw anything
• Mover – Keeps updating the coordinates of a Sprite
![Page 6: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/6.jpg)
An Oval Sprite
public class Oval implements Sprite { private int width, height; private Color color;
public Oval(int width, int height, Color color) { // set the fields ...
}
public void draw(Graphics surface, int x, int y) { surface.setColor(color);surface.fillOval(x, y, width, height); surface.drawOval(x, y, width, height);
} ...
}
![Page 7: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/7.jpg)
A Mover that doesn’t bouncepublic class StraightMover {
private int x, y, xDirection, yDirection;private Sprite sprite;
public StraightMover(int startX, int startY, Sprite sprite) { x = startX; y = startY; this.sprite = sprite;
} public void setMovementVector(int xIncrement, int yIncrement) {
xDirection = xIncrement; yDirection = yIncrement;
} public void draw(Graphics graphics) {
sprite.draw(graphics, x, y);x += xDirection;y += yDirection;
} }
![Page 8: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/8.jpg)
Inheritance
Exceptions
I/O
![Page 9: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/9.jpg)
Inheritance
![Page 10: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/10.jpg)
Very Very Basic Inheritance
• Making a Game
public class Dude {public String name;public int hp = 100public int mp = 0;
public void sayName() {System.out.println(name);
}public void punchFace(Dude target) {
target.hp -= 10;}
}
![Page 11: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/11.jpg)
Inheritance..
• Now create a Wizard…
public class Wizard { // ugh, gotta copy and paste // Dude’s stuff
}
![Page 12: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/12.jpg)
Inheritance?
• Now create a Wizard…
But Wait! A Wizard does and has everything a
Dude does and has!
![Page 13: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/13.jpg)
Inheritance?
• Now create a Wizard…
Don’t Act Now! You don’t have to Copy & Paste!
![Page 14: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/14.jpg)
Buy Inheritance!
• Wizard is a subclass of Dude
public class Wizard extends Dude { }
![Page 15: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/15.jpg)
Buy Inheritance!
• Wizard can use everything* the Dude has!wizard1.hp += 1;
• Wizard can do everything* Dude can do! wizard1.punchFace(dude1);
• You can use a Wizard like a Dude too! dude1.punchface(wizard1);
*except for private fields and methods
![Page 16: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/16.jpg)
Buy Inheritance!
• Now augment a Wizard
public class Wizard extends Dude { ArrayList<Spell> spells; public class cast(String spell) {
// cool stuff here...mp -= 10;
}}
![Page 17: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/17.jpg)
Inheriting from inherited classes
• What about a Grand Wizard?
public class GrandWizard extends Wizard { public void sayName() { System.out.println(“Grand wizard” + name)
}}
grandWizard1.name = “Flash”grandWizard1.sayName();((Dude)grandWizard1).sayName();
![Page 18: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/18.jpg)
How does Java do that?
• What Java does when it sees grandWizard1.punchFace(dude1)
1. Look for punchFace()in the GrandWizard class
2. It’s not there! Does GrandWizard have a parent?
3. Look for punchFace()in Wizard class
4. It’s not there! Does Wizard have a parent?
5. Look for punchFace()in Dude class
6. Found it! Call punchFace() 7. Deduct hp from dude1
![Page 19: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/19.jpg)
How does Java do that? pt2
• What Java does when it sees
((Dude)grandWizard1).sayName()
1. Cast to Dude tells Java to start looking in Dude 2. Look for sayName()in Dude class
3. Found it! Call sayName()
![Page 20: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/20.jpg)
What’s going on?
Parent of Wizard, Elf..
Subclass of Dude
Subclass of Wizard
Dude
Wizard Thief Elf
Grand Wizard
![Page 21: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/21.jpg)
You can only inherit from one class
Dude
Thief Elf
Bad Elf
![Page 22: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/22.jpg)
You can only inherit from one class
Dude
Thief Elf
Bad Elf
![Page 23: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/23.jpg)
You can only inherit from one class
What if Thief and Elf both implement
public void sneakUp()
If they implemented differently, which sneakUp()does BadElf call?
Java Doesn’t Know!!
Dude
Thief Elf
Bad Elf
![Page 24: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/24.jpg)
Inheritance Summary
• class A extends B {} == A is a subclass of B
• A has all the fields and methods that B has
• A can add it’s own fields and methods
• A can only have 1 parent
• A can replace a parent’s method by re-implementing it
• If A doesn’t implement something Java searches ancestors
![Page 25: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/25.jpg)
So much more to learn!
• http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html
• http://home.cogeco.ca/~ve3ll/jatutor5.htm
• http://en.wikipedia.org/wiki/Inheritance_(computer_science)
• http://www.google.com
![Page 26: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/26.jpg)
Exceptions
![Page 27: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/27.jpg)
Exceptions
• NullPointerException
• ArrayIndexOutOfBoundsException
• ClassCastException
• RuntimeException
![Page 28: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/28.jpg)
What is an “Exception”?
• Event that occurs when something “unexpected” happens –null.someMethod();
–(new int[1])[1] = 0;
–int i = “string”;
![Page 29: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/29.jpg)
Why use an Exception?
• To tell the code using your method that something went wrong
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5at RuntimeException.main(RuntimeException.java:8)
Accessed index 5, which isn’t in the array
The method that called it was main
• Debugging and understanding control flow
![Page 30: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/30.jpg)
How do exceptions “happen”?
• Java doesn’t know what to do, so it – Creates an Exception object
– Includes some useful information
– “throws” the Exception
• You can create and throw Exceptions too!
![Page 31: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/31.jpg)
public class Exception
• Exception is a class
• Just inherit from it!
public class MyException extends Exception{ }
• Or use existing ones – http://rymden.nu/exceptions.html
![Page 32: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/32.jpg)
Warn Java about the Exception
public Object get(int index) throws ArrayOutOfBoundsException { If (index < 0 || index >= size())
throw new ArrayOutOfBoundsException(“”+index);
}
• throws tells Java that get may throw the ArrayOutOfBoundsException
• throw actually throws the Exception (sorry)
![Page 33: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/33.jpg)
Catching an Exception
• Java now expects code that calls get to deal with the exception by –Catching it
–Rethrowing it
![Page 34: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/34.jpg)
Catching it
• What it does – try to run some code that may throw an exception
– Tell Java what to do if it sees the exception (catch)
try {get(-1);
} catch (ArrayOutOfBoundsException err) { System.out.println(“oh dear!”);
}
![Page 35: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/35.jpg)
Rethrowing it
• Maybe you don’t want to deal with the Exception
• Tell Java that your method throws it too
void doBad() throws ArrayOutOfBoundsException { get(-1);
}
![Page 36: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/36.jpg)
Rethrowing it
main
![Page 37: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/37.jpg)
Rethrowing it
doBad
main
![Page 38: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/38.jpg)
Rethrowing it
get
doBad
main
![Page 39: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/39.jpg)
Rethrowing it
get
doBad
main
![Page 40: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/40.jpg)
Rethrowing it
get
doBad
main
Exception
![Page 41: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/41.jpg)
Rethrowing it
get
doBad
main
Exception
![Page 42: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/42.jpg)
Rethrowing it
doBad
main
Exception
![Page 43: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/43.jpg)
Rethrowing it
doBad
main
Exception
![Page 44: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/44.jpg)
Rethrowing it
main Exception
![Page 45: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/45.jpg)
Rethrowing it
main Exception
![Page 46: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/46.jpg)
What it no one catches it?
• If you ran public static void main(String[] args) throws Exception { doBad();
}
• Java will print that error message you see
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at YourClass.get(YourClass.java:50)at YourClass.doBad(YourClass.java:11)at YourClass.main(YourClass.java:10)
![Page 47: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/47.jpg)
More Info?
• http://java.sun.com/docs/books/tutorial/essential/exceptions
• http://en.wikipedia.org/wiki/Exceptions
![Page 48: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/48.jpg)
I/O
![Page 49: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/49.jpg)
We’ve seen Output
System.out.println(“some string”);
![Page 50: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/50.jpg)
The Full Picture
Hard drive Network
100101010101000101 ... InputStreamSystem.in
‘O’ ‘k’ ‘a’ ‘y’ ‘ ‘ ‘a’ ‘w’ ‘e’ … InputStreamReader
“Okay awesome, cool\n” … BufferedReader
![Page 51: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/51.jpg)
InputStream
• InputStream is a stream of bytes – Read one byte after another using read()
• A byte is just a number – Data on your hard drive is stored in bytes
– Bytes can be interpreted as characters, numbers..
InputStream stream = System.in;
![Page 52: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/52.jpg)
InputStreamReader
• Reader is a class for character streams –Read one character after another using read()
• InputStreamReader takes an InputStream and converts bytes to characters
• Still inconvenient –Can only read a character at a time
new InputStreamReader(stream)
![Page 53: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/53.jpg)
BufferedReader
• BufferedReader buffers a character stream so you can read line by line –String readLine()
new BufferedReader( new InputStreamReader(System.in));
![Page 54: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/54.jpg)
User Input
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
br.readLine();
![Page 55: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/55.jpg)
FileReader
• FileReader takes a text file – converts it into a character stream
– FileReader(“PATH TO FILE”);
• Use this + BufferedReader to read files!
FileReader fr = new FileReader(“readme.txt”); BufferedReader br = new BufferedReader(fr);
![Page 56: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/56.jpg)
FileReader Codeimport java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;
public class ReadFile {
public static void main(String[] args) throws IOException{// Path names are relative to project directory (Eclipse Quirk )FileReader fr = new FileReader("./src/readme");BufferedReader br = new BufferedReader(fr);String line = null;while ((line = br.readLine()) != null) {
System.out.println(line);}br.close();
}}
![Page 57: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/57.jpg)
More about I/O
• http://java.sun.com/docs/books/tutorial/essential/io/
![Page 58: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/58.jpg)
Assignment
• Magic Squares
• Read two files
• Check that all rows and columns sum to 15
2 7 6
9 5 1
4 3 8
15
15
15
1515 15 15 15
Figure by MIT OpenCourseWare.
![Page 59: MIT Lec Inherit](https://reader030.fdocuments.us/reader030/viewer/2022020202/577cc7841a28aba711a1308a/html5/thumbnails/59.jpg)
MIT OpenCourseWarehttp://ocw.mit.edu
6.092 Introduction to Programming in Java January (IAP) 2010
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.