1 / 21

Object Oriented Programming Concepts

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

shada
Télécharger la présentation

Object Oriented Programming Concepts

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

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

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

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

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

  6. Point point1; x 5 y 30 Point point2; x 17 y 58 Class Objects or Class Instances class.ppt

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  21. 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. class.ppt

More Related