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.

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: Ch. 8 #1-9 exercises:
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Encapsulation, this, Subclasses.
1 Building Java Programs Chapter 8: Classes These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-1: Intro to Classes and Objects reading:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
Based on slides at buildingjavaprograms.com Objects and Classes, take 2 Managing Complexity with Programmer-Defined Types  Classes are programmer-defined.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 19: encapsulation, inheritance reading: (Slides adapted from Stuart.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
1 Object state: fields. Fields field: A variable inside an object that represents part of its internal state.  Each object will have its own copy of.
CSC 142 Computer Science II Zhen Jiang West Chester University
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Topic 27 classes and objects, state and behavior Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Object Oriented Programming
Building Java Programs
Lecture 11: More on Classes
CSC240 Computer Science III
Classes and Objects, State and Behavior
Lecture 8-3: Encapsulation, this
Building Java Programs
Object initialization: constructors
Topic 28 classes and objects, part 2
Building Java Programs
CLASS DEFINITION (FIELDS)
Topic 29 classes and objects, part 3
Building Java Programs
Chapter 8 Lecture 8-1: Classes and Objects
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Lecture 12: Classes II Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 11: Intro to Classes
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
Building Java Programs
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Building Java Programs
Dr. R Z Khan Handout-3 Classes
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
reading: 8.6 self-check: #18, exercises: #9, 14
Building Java Programs
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topic 29 classes and objects, part 3
Building Java Programs
Building Java Programs
Topic 29 classes and objects, part 3
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.