Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.

Similar presentations


Presentation on theme: "Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity."— Presentation transcript:

1 Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity that contains state and behavior State – set of values (internal data) stored in an object Behavior – set of actions an object can perform, often reporting or modifying its state class.ppt1

2 Objects Objects are not complete programs Objects are components with distinct roles and responsibilities Java contains over 3000 classes of objects –String, Point, Scanner, File Used by clients class.ppt2

3 3 Class State –Fields: variable inside an object that makes up part of its internal state –Using data types that already exist –Implicit initialization Behavior –Instance Methods: a method inside an object that operates on that object –Mutator: an instance method that modifies the object’s internal state (name begins with set) –Accessor: an instance method that provides information about the state of an object without modifying it (name often begins with get or is) Make each class its own file

4 public class Point { int x; int y; public double distanceFromOrigin() { return Math.sqrt(x*x + y*y); } //shifts this points location by the given amount public void shift (int dx, int dy) { x += dx; y += dy; } class.ppt4

5 5 Declaring objects Done in client Point p1; // no space allocated Example: Point point1 = new Point(); //allocates space Point point2 = new Point(); Note: The declarations create 2 new objects of the Point class. Each object has its own copies of x and y.

6 class.ppt6 Class Objects or Class Instances Point point1; x5 y30 Point point2; x17 y 58

7 Client Point point1 = new Point(); point1.x = 7; point1.y = 3; point1.shift(2,-1); System.out.println(p1.distanceFromOrigin()); class.ppt7

8 Constructor A special method that initializes the state of new objects as they are created Same name as class No type Executed when an instance is made class.ppt8

9 Constructors If you create an alternate constructor, then you must (also) create the default constructor. If you do not include any constructor in a class, then Java automatically provides the default constructor. class.ppt9

10 Constructors public Point() { x = 0; y = 0; } public Point(int initialX, int initialY) { x = initialX; y = initialY; } class.ppt10

11 public class Point { int x; int y; public Point() //default constructo { x = 0; y = 0; } public Point(int initialX, int initialY) // constructor { x = initialX; y = initialY; } //returns the distance between this point and (0,0) public double distanceFromOrigin() { return Math.sqrt(x*x + y*y); } //shifts this points location by the given amount public void shift (int dx, int dy) { x += dx; y += dy; } class.ppt11

12 Client code Point p1 = new Point(); //calls default Point p2 = new Point(6,1); class.ppt12

13 Client code public class PointMain { public static void main(String[] args) { Point p1 = new Point(7,4); Point p2 = new Point(6,1); //print each point and its distance from the origin System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("distance from origin = " + p1.distanceFromOrigin()); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); System.out.println("distance from origin = " + p2.distanceFromOrigin()); //shift p1.shift(11,6); p2.shift(1,7); //print the points again System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); } class.ppt13

14 Encapsulation Hiding the implementation details of an object from the clients of the object Abstraction – focusing on essential properties rather than inner details Encapsulation leads to abstraction Private fields class.ppt14

15 public class Point { private int x; private int y; public Point() //default constructor { x = 0; y = 0; } public Point(int initialX, int initialY) // constructor { x = initialX; y = initialY; } //returns the distance between this point and (0,0) public double distanceFromOrigin() { return Math.sqrt(x*x + y*y); } //shifts this points location by the given amount public void shift (int dx, int dy) { x += dx; y += dy; } class.ppt15

16 Accessing Class Members objects within a class definition can access public and private members objects out side of class definition can access only public members class.ppt16

17 Client Point point1 = new Point(); point1.x = 7; point1.y = 3; Yields errors x had private access point in Point y has private access point in Point class.ppt17

18 Add new methods public int getX() { return x; } public int getY() { return y; } public void setX(int newX) { x = newX; } public void getY(int newY) { y = newY; } class.ppt18

19 Client code public class PointMain { public static void main(String[] args) { Point p1 = new Point(7,4); Point p2 = new Point(6,1); //print each point and its distance from the origin System.out.println("p1 is (" + p1.getX() + ", " + p1.getY() + ")"); System.out.println("distance from origin = " + p1.distanceFromOrigin()); System.out.println("p2 is (" + p2.getX() + ", " + p2.getY() + ")"); System.out.println("distance from origin = " + p2.distanceFromOrigin()); / /shift p1.setX(11); p1.setY(6); p2.shift(1,7); //print the points again System.out.println("p1 is (" + p1.getX() + ", " + p1.getY() + ")"); System.out.println("p2 is (" + p2.getX() + ", " + p2.getY() + ")"); } class.ppt19

20 Built-in Operations You cannot perform arithmetic operations on class objects –cannot use +, -, etc You cannot perform relational operations on objects –cannot use, == can use. to access public members assignment is a shallow copy class.ppt20

21 class.ppt21 List of terms used: Class = a structured type that is used to represent an ADT Class member = component of a class. Can be data or methods Class object (class instance) = a variable of a class type Client = software that declares and manipulates objects of a particular class. Data is generally private Methods are generally declared public Private class members can be accessed only by the class member functions, not by client code.


Download ppt "Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity."

Similar presentations


Ads by Google