Title: More%20OOP
1More OOP
2Extending others classes
extend Java platform classes, e.g. class Applet
public class MyApplet extends Applet public
void init() public void paint(Graphics
g)
overriding the method defininitons
3Overriding
class Rectangle private int
x,y,width,height public Rectangle(int
width,int height) ? public double area()
? public void draw(Graphics g) ?
...
class RedRectangle extends Rectangle public
void draw(Graphics g) g.setColor(Color.red)
g.drawRectangle(getX(),getY(),getWidth(),g
etHeight())
4Overriding
BounceRectangleAroundTheScreen(Rectangle a)
Rectangle bnew RedRectangle(10,10)
BounceRectangleAroundTheScreen(b)
RedRectangle is a Rectangle (it behaves slightly
differently)
everytime draw method is called for object of
type RedRectangle it is drawn in RED
A variable of type reference to Rectangle can
point to object of type RedRectangle
5Overriding
class Rectangle private int
x,y,width,height public Rectangle(int
width,int height) ? public double area()
? public void draw(Graphics g) ?
...
class RedRectangle extends Rectangle public
void draw(Graphics g) g.setColor(Color.red)
g.drawRectangle(x,y,w,h)
protected
6Overriding - rules
(non-private instance methods)
methods declared with final cannot be overriden
overriden method has to have the same return type
which method will be used depends only on the
objects type (casting does not change
the objects type, just the way we look at it)
you cannot override private methods you do
not inherit them
7Extending classes constructors
since RedRectangle is Rectangle the
constructor for Rectangle should be called
(something important might be initialized in the
constructor)
class RedRectangle public void draw(Graphics
g) g.setColor(Color.red)
g.drawRectangle(getX(),getY(),getWidth(),getHeight
()) public RedRectangle(int width,int
height) super(width,height) ?
8Constructors - rules
you should call super in the first line of a
constructor if you dont then if parent
class has constructor with no parameters
gt it is called (before the body of the
constructor) if parent class has no
constructor with no parameters gt ERROR
remember default constructor has no parameters,
but if it is defined only if there is no other
constructor.
9Exercise
class Number protected int x public
Number(int y) xy public void increment()
x public void increment2()
increment() increment() public int
getNumber() return x class BadNumber
extends Number public BadNumber(int y)
super(y) public void increment() x--
Number anew Number(10) BadNumber bnew
BadNumber(10) Number cnew BadNumber(10) a.incre
ment() System.out.println(a.getNumber()) a.in
crement2() System.out.println(a.getNumber())
b.increment() System.out.println(b.getNumber()
) b.increment2() System.out.println(b.getNumb
er()) c.increment() System.out.println(c.getN
umber()) c.increment2() System.out.println(c.
getNumber())
What is the output?
10Exercise
class Number protected int x public
Number(int y) xy increment() public void
increment() x public void increment2()
increment() increment() public int
getNumber() return x class BadNumber
extends Number public BadNumber(int y)
super(y) increment() public void
increment() x-- Number anew
Number(10) BadNumber bnew BadNumber(10) Number
cnew BadNumber(10) a.increment()
System.out.println(a.getNumber()) a.increment2
() System.out.println(a.getNumber()) b.increm
ent() System.out.println(b.getNumber()) b.inc
rement2() System.out.println(b.getNumber()) c
.increment() System.out.println(c.getNumber())
c.increment2() System.out.println(c.getNumbe
r())
What is the output?
11Exercise
class Number protected int x public
Number(int y) xy increment() private
void increment() x public void
increment2() increment() increment()
public int getNumber() return x class
BadNumber extends Number public BadNumber(int
y) super(y) increment() private void
increment() x-- Number anew
Number(10) BadNumber bnew BadNumber(10) Number
cnew BadNumber(10) a.increment2()
System.out.println(a.getNumber()) b.increment2
() System.out.println(b.getNumber()) c.increm
ent2() System.out.println(c.getNumber())
What is the output?
12Exercise
What is the output?
class Number protected int x public
Number(int y) xy public Number() x13
public int getNumber() return x class
BadNumber extends Number public BadNumber(int
y) System.out.println(Yupeee!) Number
anew Number(10) BadNumber bnew
BadNumber(10) System.out.println(a.getNumber()
) System.out.println(b.getNumber())
13Exercise
What is the output?
class Number private int x public
Number(int y) xy public Number() x13
public int getNumber() return x class
BadNumber extends Number public BadNumber(int
y) System.out.println(Yupeee!) xy
Number anew Number(10) BadNumber bnew
BadNumber(10) System.out.println(a.getNumber()
) System.out.println(b.getNumber())
14Invoking an overriden method
class Rectangle private int
x,y,width,height public Rectangle(int
width,int height) ? public double area()
? public void draw(Graphics g) ?
...
class RectangleWithPrintedArea public void
draw(Graphics g) super.draw(g)
g.drawString(area(),get(X),get(Y)20)
15Exercise
class Number protected int x public
Number(int y) xy public void increment()
x public void increment2()
increment() increment() public int
getNumber() return x class BadNumber
extends Number public BadNumber(int y)
super(y) public void increment()
super.increment() x-- Number anew
Number(10) BadNumber bnew BadNumber(10) Number
cnew BadNumber(10) a.increment()
System.out.println(a.getNumber()) a.increment2
() System.out.println(a.getNumber()) b.increm
ent() System.out.println(b.getNumber()) b.inc
rement2() System.out.println(b.getNumber())
What is the output?
16Exercise
class Number protected int x public
Number(int y) xy private void increment()
x public void increment2()
increment() increment() public int
getNumber() return x class BadNumber
extends Number public BadNumber(int y)
super(y) private void increment()
super.increment() x-- Number anew
Number(10) BadNumber bnew BadNumber(10) Number
cnew BadNumber(10) a.increment2()
System.out.println(a.getNumber()) b.increment2
() System.out.println(b.getNumber())
What is the output?
17Inheritance using others code
class Rectangle protected int width,height
public Rectangle(int width,int height) ?
public double diameterOfCircumCircle() ?
class RectangleOnScreen extends Rectangle
int x,y public RectangleOnScreen(int x,int
y,int w,int h) super(w,h)
this.xx this.yy public
drawCircumCircle(Graphics g) ?
18Inheritance using others code
public drawCircumCircle(Graphics g)
g.drawCircle(xwidth/2,yheight/2,
diameterOfCircumCircle())
We can easily change/use others code!
Private variables cannot be used in the derived
class!
19Polymorphism
We want to have list of various shapes which are
placed on the screen. We want to move them, draw
them, compute area.
Shape
Polygon
Square
Circle
20abstract class Shape private int x,y
// coordinates Shape(int initialX,int
initialY) xinitialX yinitialY
public void moveUp(int pixels)
y-pixels .... abstract double
area() abstract void draw(Graphics g)
Shape is too abstract, we cannot compute area
or draw
21Shape
class Circle extends Shape public static
final double PI3.1415 private int diameter
Circle(int x,int y,int diam) super(x,y)
diameterdiam public double area()
return PIdiameterdiameter/4.0 public
void draw(Graphics g) ...
Circle
22Shape
class Square extends Shape public static
final double PI3.1415 private int side
Square(int x,int y,int side) super(x,y)
this.sideside public double area()
return sideside public void
draw(Graphics g) ...
Square
calling constructor of the parent class
23Circle is a Shape!
Shape anew Circle(10,10,5) a.moveUp(5) System.o
ut.println(The area is a.area())
Shape anew Shape2 a0new
Circle(10,10,5) a1new Square(20,20,10) if
(a1.area()gta0.area()) a1.draw() else
a0.draw()
24Shape is not a Circle
Circle anew Shape() Circle bnew
Rectangle() Shape cnew Shape()
25EXERCISE 12
class Animal void wish() System.out.println(
I dont know!) class Rabbit extends Animal
void wish() System.out.println(I want
carrot!) class Turtle extends Animal
void wish() System.out.println(I want
shrimp!) Animal anew Animal()
a.wish() Rabbit bnew Rabbit() b.wish() Animal
cnew Rabbit() c.wish() Animal dnew Turtle()
((Turtle) d).wish()
26Applets
27Simple applets
public class MyApplet extends Applet public
void init() public void paint(Graphics
g)
executed at the beginning
executed when the applet is redrawn
28Simple applets
public class MyApplet extends Applet int
x0 public void init()
setBackground(Color.black) public void
paint(Graphics g) x
g.setColor(Color.white) g.drawString(x,2
0,20)
Import?
java.applet.Applet java.awt.Graphics java.awt.Colo
r
MyApplet
29Applets start(), stop(), destroy()
30Applets that run
public class MyApplet extends Applet implements
Runnable public void init() public
void paint(Graphics g) public void run()
executed at the beginning
executed when the applet is redrawn
we will create a new Thread which will execute
this method
31Interfaces
we will not go into details
public class MyApplet extends Applet implements
Runnable public void init() public
void paint(Graphics g) public void
run()
says which methods must be implemented in MyApplet
expects parameter of type Runable
(new Thread(this)).start()
32Applets that run
public class MyApplet extends Applet implements
Runnable public void init() (new
Thread(this)).start() public void
paint(Graphics g) public void run()
we will create a new Thread out of this Object
(note that this Object is Runnable) its run
method will be this. We will start it!
33Applets that run
public class MyApplet extends Applet implements
Runnable int x0 public void init()
setBackground(Color.black) (new
Thread(this)).start() public void
paint(Graphics g) g.setColor(Color.white)
g.drawString(x,20,20) public
void run() while (true)
x repaint()
different Thread
request for repainting (If there are enough
resources it is repainted)
MyApplet
34Applets that run wasting resources
public class MyApplet extends Applet implements
Runnable int repaints0,requests0 public
void init() setBackground(Color.black)
(new Thread(this)).start() public void
paint(Graphics g) g.setColor(Color.white)
g.drawString(Repaints (repaints),20,2
0) g.drawString(Requests
requests,20,40) public void run()
while (true) requests
repaint()
request for repainting (If there are enough
resources it is repainted)
MyApplet
35Applets that run
public class MyApplet extends Applet implements
Runnable int repaints0,requests0 public
void init() setBackground(Color.black)
(new Thread(this)).start() public void
paint(Graphics g) g.setColor(Color.white)
g.drawString(Repaints (repaints),20,2
0) g.drawString(Requests
requests,20,40) public void run()
while (true) requests
repaint()
try Thread.currentThread().sleep(40)
catch (InterruptedException e)
MyApplet
36Why not run in init?
public class MyApplet extends Applet int
repaints0,requests0 public void init()
setBackground(Color.black) while (true)
requests repaint()
public void paint(Graphics g)
g.setColor(Color.white) g.drawString(Repai
nts (repaints),20,20)
g.drawString(Requests requests,20,40)
MyApplet
37Why not run in init?
public class MyApplet extends Applet int
repaints0,requests0 public void init()
setBackground(Color.black) while (true)
requests repaint()
public void paint(Graphics g)
g.setColor(Color.white) g.drawString(Repai
nts (repaints),20,20)
g.drawString(Requests requests,20,40)
the thread which runs init needs to do some
things e.g. start the paint thread...
MyApplet