1 / 28

Objects

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[] ) {

kerryn
Download Presentation

Objects

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objects

  2. “class” concept • Combines related variables (members) and methods into a single program module • Encapsulation • Information hiding • public vs. private

  3. Recall how we started this semester: class MyClass { public static void main ( String p[] ) { Scanner in = new Scanner( System.in ); int i = in.nextInt(); … } }

  4. What did we call this: { … }

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

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

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

  8. Class variables class 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.

  9. The meaning of “static” • Why do we keep using “static” for everything we define? • What does “static” mean?

  10. 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?

  11. 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”) );

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

  13. 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) … }

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

  15. 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?

  16. Shape object class 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) … } }

  17. Shape object class 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 )

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

  19. 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? }

  20. 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? }

  21. Line class 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 Shape mY = starty; //ditto mX2 = endx; mY2 = endy; } //what else do we need? }

  22. Line class 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 Shape mY = starty; //ditto mX2 = endx; mY2 = endy; } public void paint ( Graphics g ) { //draw a line starting at (x,y) and ending at (x2,y2) … } }

  23. 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 Rect Line

  24. 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? }

  25. 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; 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? }

  26. 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; 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 { … } } }

  27. Circle • Can you define a Circle class? Shape Rect Circle Line

  28. Triangle • Can you define a Triangle class?

More Related