objects
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 PresentationTRANSCRIPT
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[] ) {
Scanner in = new Scanner( System.in );int i = in.nextInt();…
}}
What did we call this:
{…
}
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.
Methods
Methods perform information hiding. Other methods can’t look at vars in other methods unless they are passed (either by ? or by ?).
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.
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.
The meaning of “static”
Why do we keep using “static” for everything we define?
What does “static” mean?
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?
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”) );
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.
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)…
}
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.
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?
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 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 )
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
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?
}
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?
}
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?
}
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)…
}}
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
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?}
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?
}
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 {
…}
}}
Circle
Can you define a Circle class?
Shape
Line Rect Circle
Triangle
Can you define a Triangle class?