370 likes | 570 Views
More OOP. Extending other’s 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. Overriding. class Rectangle {
E N D
Extending other’s 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
Overriding 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(),getHeight()); } }
Overriding BounceRectangleAroundTheScreen(Rectangle a); Rectangle b=new 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
Overriding 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
Overriding - 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 object’s type (casting does not change the object’s type, just the way we look at it) you cannot override private methods – you do not inherit them
Extending 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); ? } }
Constructors - rules you should call super in the first line of a constructor if you don’t then if parent class has constructor with no parameters => it is called (before the body of the constructor) if parent class has no constructor with no parameters => ERROR remember – default constructor has no parameters, but if it is defined only if there is no other constructor.
class Number { protected int x; public Number(int y) { x=y; } 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 a=new Number(10); BadNumber b=new BadNumber(10); Number c=new BadNumber(10); a.increment(); System.out.println(“”+a.getNumber()); a.increment2(); System.out.println(“”+a.getNumber()); b.increment(); System.out.println(“”+b.getNumber()); b.increment2(); System.out.println(“”+b.getNumber()); c.increment(); System.out.println(“”+c.getNumber()); c.increment2(); System.out.println(“”+c.getNumber()); Exercise What is the output?
class Number { protected int x; public Number(int y) { x=y; 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 a=new Number(10); BadNumber b=new BadNumber(10); Number c=new BadNumber(10); a.increment(); System.out.println(“”+a.getNumber()); a.increment2(); System.out.println(“”+a.getNumber()); b.increment(); System.out.println(“”+b.getNumber()); b.increment2(); System.out.println(“”+b.getNumber()); c.increment(); System.out.println(“”+c.getNumber()); c.increment2(); System.out.println(“”+c.getNumber()); Exercise What is the output?
class Number { protected int x; public Number(int y) { x=y; 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 a=new Number(10); BadNumber b=new BadNumber(10); Number c=new BadNumber(10); a.increment2(); System.out.println(“”+a.getNumber()); b.increment2(); System.out.println(“”+b.getNumber()); c.increment2(); System.out.println(“”+c.getNumber()); Exercise What is the output?
Exercise What is the output? class Number { protected int x; public Number(int y) { x=y; } public Number() { x=13; } public int getNumber() { return x; } } class BadNumber extends Number { public BadNumber(int y) { System.out.println(“Yupeee!”); } } Number a=new Number(10); BadNumber b=new BadNumber(10); System.out.println(“”+a.getNumber()); System.out.println(“”+b.getNumber());
Exercise What is the output? class Number { private int x; public Number(int y) { x=y; } public Number() { x=13; } public int getNumber() { return x; } } class BadNumber extends Number { public BadNumber(int y) { System.out.println(“Yupeee!”); x=y; } } Number a=new Number(10); BadNumber b=new BadNumber(10); System.out.println(“”+a.getNumber()); System.out.println(“”+b.getNumber());
Invoking 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); } }
class Number { protected int x; public Number(int y) { x=y; } 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 a=new Number(10); BadNumber b=new BadNumber(10); Number c=new BadNumber(10); a.increment(); System.out.println(“”+a.getNumber()); a.increment2(); System.out.println(“”+a.getNumber()); b.increment(); System.out.println(“”+b.getNumber()); b.increment2(); System.out.println(“”+b.getNumber()); Exercise What is the output?
class Number { protected int x; public Number(int y) { x=y; } 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 a=new Number(10); BadNumber b=new BadNumber(10); Number c=new BadNumber(10); a.increment2(); System.out.println(“”+a.getNumber()); b.increment2(); System.out.println(“”+b.getNumber()); Exercise What is the output?
Inheritance – 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.x=x; this.y=y; } public drawCircumCircle(Graphics g) { ? } }
Inheritance – using others code public drawCircumCircle(Graphics g) { g.drawCircle(x+width/2,y+height/2, diameterOfCircumCircle()); } We can easily change/use other’s code! Private variables cannot be used in the derived class!
Polymorphism 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
abstract class Shape { private int x,y; // coordinates Shape(int initialX,int initialY) { x=initialX; y=initialY; } 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
Shape class Circle extends Shape { public static final double PI=3.1415; private int diameter; Circle(int x,int y,int diam) { super(x,y); diameter=diam; } public double area() { return PI*diameter*diameter/4.0; } public void draw(Graphics g) { ... } } Circle
Shape class Square extends Shape { public static final double PI=3.1415; private int side; Square(int x,int y,int side) { super(x,y); this.side=side; } public double area() { return side*side; } public void draw(Graphics g) { ... } } Square calling constructor of the parent class
Circle is a Shape! Shape a=new Circle(10,10,5); a.moveUp(5); System.out.println(“The area is ”+a.area()); Shape[] a=new Shape[2]; a[0]=new Circle(10,10,5); a[1]=new Square(20,20,10); if (a[1].area()>a[0].area()) a[1].draw(); else a[0].draw();
Shape is not a Circle Circle a=new Shape(); Circle b=new Rectangle(); Shape c=new Shape();
EXERCISE #12: class Animal { void wish() { System.out.println(“I don’t 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 a=new Animal(); a.wish(); Rabbit b=new Rabbit(); b.wish(); Animal c=new Rabbit(); c.wish(); Animal d=new Turtle(); ((Turtle) d).wish();
Simple applets public class MyApplet extends Applet { public void init() { } public void paint(Graphics g) { } } executed at the beginning executed when the applet is redrawn
Simple applets public class MyApplet extends Applet { int x=0; public void init() { setBackground(Color.black); } public void paint(Graphics g) { x++; g.setColor(Color.white); g.drawString(“”+x,20,20); } } Import? java.applet.Applet java.awt.Graphics java.awt.Color MyApplet
Applets 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
Interfaces we will not go into details public class MyApplet extends Applet implementsRunnable { 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();
Applets 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!
Applets that run public class MyApplet extends Applet implements Runnable { int x=0; 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
Applets that run – wasting resources public class MyApplet extends Applet implements Runnable { int repaints=0,requests=0; public void init() { setBackground(Color.black); (new Thread(this)).start(); } public void paint(Graphics g) { g.setColor(Color.white); g.drawString(“Repaints: ”+(++repaints),20,20); 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
Applets that run public class MyApplet extends Applet implements Runnable { int repaints=0,requests=0; public void init() { setBackground(Color.black); (new Thread(this)).start(); } public void paint(Graphics g) { g.setColor(Color.white); g.drawString(“Repaints: ”+(++repaints),20,20); g.drawString(“Requests: “+requests,20,40); } public void run() { while (true) { requests++; repaint(); } } } try { Thread.currentThread().sleep(40); } catch (InterruptedException e) {}; MyApplet
Why not run in init? public class MyApplet extends Applet { int repaints=0,requests=0; public void init() { setBackground(Color.black); while (true) { requests++; repaint(); } } public void paint(Graphics g) { g.setColor(Color.white); g.drawString(“Repaints: ”+(++repaints),20,20); g.drawString(“Requests: “+requests,20,40); } } MyApplet
Why not run in init? public class MyApplet extends Applet { int repaints=0,requests=0; public void init() { setBackground(Color.black); while (true) { requests++; repaint(); } } public void paint(Graphics g) { g.setColor(Color.white); g.drawString(“Repaints: ”+(++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