Presentation is loading. Please wait.

Presentation is loading. Please wait.

More OOP. Extending other’s classes extend Java platform classes, e.g. class Applet public class MyApplet extends Applet { public void init() { } public.

Similar presentations


Presentation on theme: "More OOP. Extending other’s classes extend Java platform classes, e.g. class Applet public class MyApplet extends Applet { public void init() { } public."— Presentation transcript:

1 More OOP

2 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

3 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()); }

4 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

5 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

6 Overriding - rules 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) (non-private instance methods) you cannot override private methods – you do not inherit them

7 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); ? }

8 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.

9 Exercise 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()); What is the output?

10 Exercise 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()); What is the output?

11 Exercise 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()); What is the output?

12 Exercise 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()); What is the output?

13 Exercise 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()); What is the output?

14 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); }

15 Exercise 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()); What is the output?

16 Exercise 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()); What is the output?

17 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) { ? } }

18 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!

19 Polymorphism Shape Circle Polygon Square We want to have list of various shapes which are placed on the screen. We want to move them, draw them, compute area.

20 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

21 Shape Circle 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) {... } }

22 Shape Square 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) {... } } calling constructor of the parent class

23 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();

24 Shape is not a Circle Circle a=new Shape(); Circle b=new Rectangle(); Shape c=new Shape();

25 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();

26 Applets

27 public class MyApplet extends Applet { public void init() { } public void paint(Graphics g) { } Simple applets executed at the beginning executed when the applet is redrawn

28 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); } Simple applets Import? java.applet.Applet java.awt.Graphics java.awt.Color MyApplet

29 Applets – start(), stop(), destroy()

30 public class MyApplet extends Applet implements Runnable { public void init() { } public void paint(Graphics g) { } public void run() { } Applets that run executed at the beginning executed when the applet is redrawn we will create a new Thread which will execute this method

31 Interfaces 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 (new Thread(this)).start(); expects parameter of type Runable

32 public class MyApplet extends Applet implements Runnable { public void init() { (new Thread(this)).start(); } public void paint(Graphics g) { } public void run() { } Applets that 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!

33 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(); } Applets that run request for repainting (If there are enough resources – it is repainted) MyApplet different Thread

34 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(); } Applets that run – wasting resources request for repainting (If there are enough resources – it is repainted) MyApplet

35 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(); } Applets that run MyApplet try { Thread.currentThread().sleep(40); } catch (InterruptedException e) {};

36 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

37 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 the thread which runs init needs to do some things – e.g. start the paint thread...


Download ppt "More OOP. Extending other’s classes extend Java platform classes, e.g. class Applet public class MyApplet extends Applet { public void init() { } public."

Similar presentations


Ads by Google