220 likes | 345 Vues
This presentation delves into the fundamental concepts of Object-Oriented Programming (OOP). It covers essential elements such as objects, states, and behaviors, including their roles and responsibilities within a program. Learn about the declaration of classes, the use of constructors for initializing objects, and the principles of encapsulation and abstraction. We also explore instance methods like mutators and accessors, alongside practical examples using Java's Point class. By the end of this session, you'll grasp how OOP facilitates better software design and coding practices.
E N D
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
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
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
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
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
Point point1; x 5 y 30 Point point2; x 17 y 58 Class Objects or Class Instances class.ppt
Client • Point point1 = new Point(); • point1.x = 7; • point1.y = 3; • point1.shift(2,-1); • System.out.println(p1.distanceFromOrigin()); class.ppt
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
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
Constructors public Point() { x = 0; y = 0; } public Point(int initialX, int initialY) { x = initialX; y = initialY; } class.ppt
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
Client code • Point p1 = new Point(); //calls default • Point p2 = new Point(6,1); class.ppt
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
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
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
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
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
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
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
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
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