H OW O BJECTS B EHAVE. O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation.

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

Written by: Dr. JJ Shepherd
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
J AVA AND V ARIABLES. O VERVIEW Declaring a Variable Primitive Types Java Keywords Reference Variables Object Declaration and Assignment Objects and Garbage.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Lecture 9 Concepts of Programming Languages
Encapsulation CMSC 202. Types of Programmers Class programmers – Developers of new classes – Goal: Expose the minimum interface necessary to use a new.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Week 3: Classes, constructors, destructors, new operator Operator and function overloading.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
CPS120: Introduction to Computer Science Functions.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Objects & Classes Weiss ch. 3. So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API.
CMSC 202 Classes and Objects: Reusing Classes with Composition.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Week 8 - Friday.  What did we talk about last time?  Static methods.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Written by: Dr. JJ Shepherd
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Comp1004: Building Better Objects II Encapsulation and Constructors.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Objects as a programming concept
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Class Definitions and Writing Methods
Creating Your OwnClasses
Anatomy of a class Part I
Using local variable without initialization is an error.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Lecture 9 Concepts of Programming Languages
Corresponds with Chapter 7
Encapsulation and Constructors
Classes, Encapsulation, Methods and Constructors (Continued)
Chapter 9 Objects and Classes
How Objects Behave Chapter 4.
Programs and Classes A program is made up from classes
Tonga Institute of Higher Education
Java Programming Language
Agenda Warmup Lesson 2.2 (parameters, etc)
Anatomy of a class Part I
CSG2H3 Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Day 11 The Last Week!.
Presentation transcript:

H OW O BJECTS B EHAVE

O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation Using references in an Array

M ETHODS USE OBJECT STATE In a non-OOP language, functions need to have lots of parameters Alternatively, you can have lots of functions Using methods, many objects contain instance variables that can be used to change methods.

E XAMPLE if (dog_a_weight < 14){ small_bark(); } else { large_bark(); } We had to have more functions for each ‘state’ of dog. We would also need to do this for each dog!

J AVA E XAMPLE The Java example on the webpage shows how a dog’s barks changes with size

A RGUMENTS AND RETURN TYPES IN METHODS Just like in C functions, Java methods can take in arguments and return values The syntax is identical to C E.g. int somefunction(int somevalue){ //Some code that uses the variable somevalue return 45; // Returns 45 }

A RGUMENTS AND RETURN TYPES IN METHODS You can send multiple arguments as well The order of the arguments matters void someotherfunction(int x, int y)

J AVA PASSES BY VALUE When calling a method, a copy of each argument is made for the function In C, you can pass pointers where the function manipulates the original value If you want a function to manipulate a value in Java, the value needs to be returned, or be an instance variable

G ETTERS AND S ETTERS Getters = Accessors Setters = Mutators Getters and Setters are simple methods that let programs manipulate an objects instance variables Why does this matter? Cant we access these values already with the dot operator?

E NCAPSULATION This is the concept of keeping our data safe so that only valid value can be put in instance variables We set instance variables to private We set setters and getters to public

E XAMPLE Lets modify our previous example to use the concept of encapsulation We will even add some simple error checking

I NSTANCE V ARIABLES VS. L OCAL V ARIABLES Just like in C, where variables are defined sets their ‘scope’ Instance variables are accessible by all methods and are declared in the class outside of any method Local variables only apply to one method and are defined at the top of the method they are used in

C OMPARING O BJECTS Simple comparison of primitives can be done with ‘==‘ How can we do this with objects? To see if two object reference variables are the same, still use ‘==‘ To see of the instance values in two objects are the same, write an ‘equals()’ method and use it