Slide1 l.jpg
This presentation is the property of its rightful owner.
Sponsored Links
1 / 37

More OOP PowerPoint PPT Presentation


  • 84 Views
  • Uploaded on
  • Presentation posted in: General

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 {

Download Presentation

More OOP

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Slide1 l.jpg

More OOP


Slide2 l.jpg

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


Slide3 l.jpg

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

}

}


Slide4 l.jpg

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


Slide5 l.jpg

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


Slide6 l.jpg

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


Slide7 l.jpg

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

?

}

}


Slide8 l.jpg

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.


Slide9 l.jpg

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?


Slide10 l.jpg

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?


Slide11 l.jpg

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?


Slide12 l.jpg

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


Slide13 l.jpg

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


Slide14 l.jpg

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

}

}


Slide15 l.jpg

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?


Slide16 l.jpg

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?


Slide17 l.jpg

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

}


Slide18 l.jpg

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!


Slide19 l.jpg

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


Slide20 l.jpg

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


Slide21 l.jpg

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


Slide22 l.jpg

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


Slide23 l.jpg

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


Slide24 l.jpg

Shape is not a Circle

Circle a=new Shape();

Circle b=new Rectangle();

Shape c=new Shape();


Slide25 l.jpg

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


Slide26 l.jpg

Applets


Slide27 l.jpg

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


Slide28 l.jpg

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


Slide29 l.jpg

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


Slide30 l.jpg

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


Slide31 l.jpg

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


Slide32 l.jpg

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!


Slide33 l.jpg

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


Slide34 l.jpg

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


Slide35 l.jpg

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


Slide36 l.jpg

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


Slide37 l.jpg

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


  • Login