28
Objects

Objects

  • Upload
    kerryn

  • View
    34

  • Download
    0

Embed Size (px)

DESCRIPTION

Objects. “class” concept. Combines related variables (members) and methods into a single program module Encapsulation Information hiding public vs. private. Recall how we started this semester:. class MyClass { public static void main ( String p[] ) { - PowerPoint PPT Presentation

Citation preview

Page 1: Objects

Objects

Page 2: Objects

“class” concept

Combines related variables (members) and methods into a single program module

Encapsulation Information hiding

public vs. private

Page 3: Objects

Recall how we started this semester:

class MyClass {public static void main ( String p[] ) {

Scanner in = new Scanner( System.in );int i = in.nextInt();…

}}

Page 4: Objects

What did we call this:

{…

}

Page 5: Objects

What did we call this: a block

{…

}

The block is an example of information hiding. Variables are created at the beginning of the block when they are declared and destroyed at the end of the block.

But we need more.

Page 6: Objects

Methods

Methods perform information hiding. Other methods can’t look at vars in other methods unless they are passed (either by ? or by ?).

Page 7: Objects

Methods

Methods perform information hiding. Other methods can’t look at vars in other methods unless they are passed (either by value or by reference).

The code within the method is also hidden from other methods.

But we need more.

Page 8: Objects

Class variablesclass MyClass {

public static int N = 100;

public static void main ( String p[] ) {System.out.println( “hello world ” + N );…

}}Class vars are available to all methods within a class. If

we use “public” for the class vars, then other classes can see them as well. If we use “private” for them, then they are hidden outside of the defining class.

But we need more.

Page 9: Objects

The meaning of “static”

Why do we keep using “static” for everything we define?

What does “static” mean?

Page 10: Objects

The meaning of “static” Why do we keep using “static” for

everything we define? What does “static” mean?

Static means that there is only one of these per class. We will call them class variables.

Note that we never have to “new” any of the classes that we have defined so far. But we always have to “new” Scanner and Random. Why?

Page 11: Objects

new “new Scanner” and “new Random” create

new instances of the classes Scanner and Random.

Just as int x=12; and int y=19; create different instances of ints. In fact, we can have many different instances of Scanner or Random.

For example,Scanner in1 = new Scanner( System.in );Scanner in2 = new Scanner( new FileInputStream(“in.txt”) );Scanner in3 = new Scanner( new FileInputStream(“infile.txt”) );

Page 12: Objects

new

These are all different yet in many ways the same.

“new Scanner()” creates (constructs) a new instance of the class Scanner.

Scanner() is a special function defined within class input that is called a constructor.

Page 13: Objects

General structure of a class.class name [extends other-class-name]{

//class (static) vars…//instance vars…//class (static) methods…//instance methods// (including “special” instance methods(s)// w/ same name as class name = constructor(s)…

}

Page 14: Objects

public vs. private vs. protected

public Anything can look at or change this.

private Only the particular instance of the class

can look at or change this.

protected Only the particular instance of the class

OR subclass can look at or change this.

Page 15: Objects

Defining our own classes.

Say we wish to create classes that will help us draw shapes in a window.

Generally, a shape looks like something with a center coordinate and a way to draw the shape.

How can we define an object for shapes?

Page 16: Objects

Shape objectclass Shape {

protected int mX, mY;

public Shape ( int x, int y ) {mX = x;mY = y;

}

public void paint ( Graphics g ) {//draw a point at (x,y)…

}}

Page 17: Objects

Shape objectclass Shape {

protected int mX, mY;

public Shape ( int x, int y ) {mX = x;mY = y;

}

public void paint ( Graphics g ) {//draw a point at (x,y)…

}}

Shape

int mX int mY Shape ( int x, int y ) paint ( Graphics g )

Page 18: Objects

Line A Line is like a Shape but a Shape draws a

point and Line should draw a line. So Line is a subclass of (extends) Shape.

Shape

Line

Page 19: Objects

Line A Line is like a Shape but a Shape draws a

point and Line should draw a line. So Line is a subclass of (extends) Shape.

class Line extends Shape {//we can use the Shape’s mX and mY// as the starting point of our line.//what else do we need for a line?

}

Page 20: Objects

Line

class Line extends Shape {//we can use the Shape’s mX and mY// as the starting point of our line.//what else do we need for a line?private int mX2, mY2;//what else do we need?

}

Page 21: Objects

Lineclass Line extends Shape {

//we can use Shape’s mX and mY as the starting point// of our line.private int mX2, mY2;

public Line ( int startx, int starty, int endx, int endy ) {mX = startx; //inherited from ShapemY = starty; //ditto

mX2 = endx;mY2 = endy;

}//what else do we need?

}

Page 22: Objects

Lineclass Line extends Shape {

//we can use Shape’s mX and mY as the starting point of our line.private int mX2, mY2; //endpoint of line

public Line ( int startx, int starty, int endx, int endy ) {mX = startx; //inherited from ShapemY = starty; //ditto

mX2 = endx;mY2 = endy;

}

public void paint ( Graphics g ) {//draw a line starting at (x,y) and ending at (x2,y2)…

}}

Page 23: Objects

Rectangle

class Rect extends Shape {//we can use Shape’s mX,mY as the// top left corner of our rectangle.what else is needed?

} Shape

Line Rect

Page 24: Objects

Rectangle

class Rect extends Shape {//we can use Shape’s mX,mY as the// top left corner of our rectangle.int mRight, mBottom;boolean mFilled;

what about the constructor?}

Page 25: Objects

Rectangleclass Rect extends Shape {

//we can use Shape’s mX,mY as the// top left corner of our rectangle.int mRight, mBottom;boolean mFilled;

public Rect ( int left, int top, int right, int bottom, boolean f ) {mX = left;mY = top;mRight = right;mBottom = bottom;mFilled = f;

}What else do we need?

}

Page 26: Objects

Rectangleclass Rect extends Shape {

//we can use Shape’s mX,mY as the top left corner of our rectangle.int mRight, mBottom;boolean mFilled;

public Rect ( int left, int top, int right, int bottom, boolean f ) {mX = left;mY = top;mRight = right;mBottom = bottom;mFilled = f;

}public void paint ( Graphics g ) {

//draw a rect starting at (x,y) and ending at (right,bottom)if (mFilled) {

…} else {

…}

}}

Page 27: Objects

Circle

Can you define a Circle class?

Shape

Line Rect Circle

Page 28: Objects

Triangle

Can you define a Triangle class?