Container Vs. Definition Classes. Container Class

Size: px
Start display at page:

Download "Container Vs. Definition Classes. Container Class"

Transcription

1 Overview Abstraction Defining new classes Instance variables Constructors Defining methods and passing parameters Method/constructor overloading Encapsulation Visibility modifiers Static members 14 November 2007 Ariel Shamir 1 Container Vs. Definition Classes Container classes: A collection of static methods that are not bound to any particular object. These static methods usually have something in common. Definition classes: These classes define new objects, in a way that we will soon see. 14 November 2007 Ariel Shamir 2 Container Class The Math class is an example of the first kind. It is a container for math utility methods: Math.sqrt() Math.abs() Math.max() November 2007 Ariel Shamir 3 1

2 Definition Class The class Turtle is an example of the second kind. It defines a new type of objects, Turtle objects. We will focus more on the second kind. 14 November 2007 Ariel Shamir 4 Abstraction Think of the object in its ideal form: what does it represent? What behaviors it should have? It is often useful to think of a real-world comparison, to help choose the name for the class and its methods. Often we include methods that we don t immediately need only because they make sense for the abstraction of the object. 14 November 2007 Ariel Shamir 5 The Clock Abstraction Clock(int hours, int minutes, int seconds) void secondelapsed() int getseconds() int getminutes() int gethours() November 2007 Ariel Shamir 6 2

3 Using a Clock Clock newyorkclock = new Clock(12,59,59); // advance New York clock by three seconds for (int j = 0; j < 3; j++) { System.out.println(newYorkClock.getHours() + : + newyorkclock.getminutes() + : + newyorkclock.getseconds()); newyorkclock.secondelapsed(); 14 November 2007 Ariel Shamir 7 Using a Clock (Cont.) Clock israelclock = new Clock(6,0,0); // advance Israel clock by one hour for (int j = 0; j < 60 * 60; j++) { System.out.println(israelClock.getHours() + : + israelclock.getminutes() + : + israelclock.getseconds()); israelclock.secondelapsed(); 14 November 2007 Ariel Shamir 8 Instance Variables Recall that a class must define the state of an object and its behavior. The state of an object is the state of the data it holds. The data of a class are its instance variable. Instance variables (or fields) are defined in a similar way to that of regular variables, only they appear outside methods, inside the class. 14 November 2007 Ariel Shamir 9 3

4 Method Local Data Vs. Object Data. Data inside the method is called local data. Local data is created when method invoked and destroyed when method returned. Instance variables are part of the object and hence are not destroyed (only when the object itself is destroyed). 14 November 2007 Ariel Shamir 10 The Clock Class Instance Variables /** * A clock representation class. Clock instances * represent a point of time during the day in a * precision of seconds. */ public class Clock { // The current hours, minutes, and second private int hours, minutes, seconds; // November 2007 Ariel Shamir 11 The Private Modifier Roughly speaking, the private modifier means that the variables are not part of the object s interface: private int hours, minutes, seconds; Other possible modifiers are: public, protected 14 November 2007 Ariel Shamir 12 4

5 Constructors Objects must be initialized before they can be used (similar to primitive data types). We must specify what is the initial state of the object before we can use it. We specify the way an object is initialized using a constructor, which is a special method which is invoked every time we create a new object. 14 November 2007 Ariel Shamir 13 Clock Constructor public class Clock { // The current hours, minutes, and second private int hours, minutes, seconds; /** * Constructs a new clock, sets the * clock to the time 00:00:00. **/ public Clock() { hours = 0; minutes = 0; seconds = 0; 14 November 2007 Ariel Shamir 14 Creating a Clock The statement new Clock() does the following: 14 November 2007 Ariel Shamir 15 5

6 Creating a Clock The statement new Clock() does the following: 1. Allocates the memory for a new clock object clock hours minutes seconds 14 November 2007 Ariel Shamir 16 Creating a Clock The statement new Clock() does the following: 1. Allocates the memory for a new clock object 2. Initializes its state by calling the constructor clock hours 0 minutes 0 seconds 0 14 November 2007 Ariel Shamir 17 Methods To make the clock object useful, we must provide methods that define its behavior and operations. We declare methods in a similar way to the way the method main was declared: public static void main(string[] args) { // November 2007 Ariel Shamir 18 6

7 Methods Purpose We have seen: Simplify sub-tasks Reuse the same piece of code Now: Operations on the objects Access to objects data (queries) 14 November 2007 Ariel Shamir 19 Non Static Methods static methods (such as main() method) are not bound to a specific object We want to declare instance methods, which operate on a specific instance of an object 14 November 2007 Ariel Shamir 20 Clock Method Example public class Clock { private int hours, minutes, seconds; public Clock() { //... /** * Advances the clock by one hour. */ public void hourelapsed() { hours = (hours + 1) % 24; 14 November 2007 Ariel Shamir 21 7

8 Methods Modifiers The modifier public denotes that the method hourelapsed() is part of the interface of the class (it is one of the services that the class exposes to outside world clients) The keyword void denotes that the method hourelapsed() has no return value 14 November 2007 Ariel Shamir 22 Another Clock Method public class Clock { //... instance variables and constructor public void hourelapsed() { hours = (hours + 1) % 24; /** * Returns the hour read. */ public int gethours() { return hours; 14 November 2007 Ariel Shamir 23 Instance Methods gethours() and hourelapsed() are instance methods, which means they act on a particular instance of the class. They cannot be invoked without a particular object and they execute in the context of this object: Clock c = new Clock(); gethours(); // of which clock? c.gethours(); // will return 0 14 November 2007 Ariel Shamir 24 8

9 Using Instance Methods public class ClockTest { public static void main(string[] args) { Clock swatch = new Clock(); Clock seiko = new Clock(); System.out.println(swatch.getHours()); // 0 System.out.println(seiko.getHours()); // 0 swatch.hourelapsed(); System.out.println(swatch.getHours()); // 1 System.out.println(seiko.getHours()); // 0 14 November 2007 Ariel Shamir 25 Clocks Memory Layout swatch seiko Clock object hours 1 minutes 0 seconds 0 Clock object hours 0 minutes 0 seconds 0 14 November 2007 Ariel Shamir 26 The this Reference When inside an instance method, the this keyword denotes a reference to the object that the method is acting upon. The following are equivalent: public int gethours() { return hours; public int gethours() { return this.hours; 14 November 2007 Ariel Shamir 27 9

10 Method Parameters A method can be defined to accept zero or more parameters Each parameter in the parameter list is defined by its type and name The parameters in the method definition are called formal parameters The values passed to a method when it is invoked are called actual parameters 14 November 2007 Ariel Shamir 28 SetTime Method Documentation public class Clock { private int hours, minutes, seconds; /** * Sets the clock to the specified time. * If one of the parameters is not in the allowed * range, the call does not have any effect on the * clock. hours The hours to be set (0-23) minutes The minutes to be set (0-59) seconds The seconds to be set (0-59) */ 14 November 2007 Ariel Shamir 29 SetTime Method Implementation public void settime(int hours, int minutes, int seconds) { if ((seconds >= 0) && (seconds < 60) && (minutes >= 0) && (minutes < 60) && (hours >= 0) && (hours < 24)) { this.hours = hours; this.minutes = minutes; this.seconds = seconds; // no effect if input is illegal 14 November 2007 Ariel Shamir 30 10

11 Using This Reference public class Clock { private int hours, minutes, seconds; /** */ public void settime(int hours, int minutes, int seconds) { if ((seconds >= 0) && (seconds < 60) && (minutes >= 0) && (minutes < 60) && (hours >= 0) && (hours < 24)) { this.hours = hours; this.minutes = minutes; this.seconds = seconds; // no effect if input is illegal 14 November 2007 Ariel Shamir 31 Passing Parameters When a parameter is passed, a copy of the value is made and assigned to the formal parameter: Clock beritling = new Clock(); int lunchhour = 12; breitling.settime(lunchhour,32,14); 14 November 2007 Ariel Shamir 32 Value Passing Clock beritling = new Clock(); int lunchhour = 12; breitling.settime(lunchhour,32,14); public void settime(int hours, int minutes, int seconds) { breitling hours = 12 minutes = 32 seconds = November 2007 Ariel Shamir 33 11

12 Object Reference as Parameters Both primitive types and object references can be passed as parameters When a primitive type is passed the actual value is passed ( call by value ) When an object reference is passed, the formal parameter becomes an alias of the actual parameter ( call by reference ) 14 November 2007 Ariel Shamir 34 A Bank Account Object Interface BankAccount public BankAccount(long accountnumber) public void deposit(float amount) public void withdraw(float amount) public void transfer (float amount, BankAccount targetaccount) 14 November 2007 Ariel Shamir 35 /** * A bank account. */ public class BankAccount { private long accountnumber; // The balance in dollars private float balance; /** * Constructs a new empty account. */ public BankAccount(long accountnumber) { this.accountnumber = accountnumber; this.balance = 0; // continued in next slide November 2007 Ariel Shamir 36 12

13 /** * Deposites a given amount into the account. */ public void deposit(float amount) { //... perhaps perform some security checks balance = balance + amount; /** * Withdraws a given amount from the account. */ public void withdraw(float amount) { //... perhaps perform some security checks balance = balance - amount; // continued in next slide November 2007 Ariel Shamir 37 /** * Transfers a given amount into * another bank account */ public void transfer(float amount, BankAccount targetaccount) { //... perhaps perform some security checks this.withdraw(amount); targetaccount.deposit(amount); amount += ; // just for fun :-) 14 November 2007 Ariel Shamir 38 Usage // Usage... BankAccount gadiaccount = new BankAccount( ); BankAccount saritaccount = new BankAccount( ); gadiaccount.deposit(500); // gadi s balance = 500 int amount = 700; gadiaccount.transfer(amount,saritaccount); // gadi s balance = -200, sarit s balance = 700 System.out.println( Gadi has just transferred + amount + dollars to Sarit ); Output: Gadi has just transferred 700 dollars to Sarit 14 November 2007 Ariel Shamir 39 13

14 Method Signature The name of the method together with the list of its formal parameters is called the signature of the method public void settime(int hours, int minutes, int seconds) void_settime_int_int_int 14 November 2007 Ariel Shamir 40 Overloading Methods You may define two (or more) methods with the same name and same return type but they must differ in the number or type of parameters (hence they have different signature). Note: they must return the same type! This is called overloading! 14 November 2007 Ariel Shamir 41 Calling an Overloaded Method When we call a method, the compiler (or JVM) decides which method to invoke according to the type and number of the actual parameters. 14 November 2007 Ariel Shamir 42 14

15 Method Overloading Example class mymath { //... int add(int x, int y); int add(double a, double b); MyMath m; int sum; sum = m.add(3,4); // will use add(int,int) sum = m.add(3.5,5.2); // will use add(double,double) sum = m.add(3,5.2); // will use add(double,double) 14 November 2007 Ariel Shamir 43 More on Constructors Constructors have no declared return type and cannot return anything. Constructors are always used with the new operator. Constructors must have exactly the same name as class. A class can have many ways to initialize instances. This is done by defining several constructors. 14 November 2007 Ariel Shamir 44 Constructor Overloading Different constructors differ by the number and/or type of parameters they receive. When we construct an object, the compiler decides which constructor to invoke according to the type of the actual parameters. If no other constructor is defined, the compiler creates the default constructor for the class automatically. 14 November 2007 Ariel Shamir 45 15

16 Default Constructor The default constructor receives no parameters and it initializes all the instance variables to zero or null (depending on the type). When we define a constructor with no parameter it is sometimes called a default constructor: public class Clock { private int hours, minutes, seconds; public Clock() { // November 2007 Ariel Shamir 46 Overloading Clock Constructor /** * Constructs a new clock with the specified hours, * minutes and seconds read. * If one of the paramenters is not in the allowed * range, the time will be reset to 00:00:00. hours The hours to be set (0-23) minutes The minutes to be set (0-59) seconds The seconds to be set (0-59) */ public Clock(int hours, int minutes, int seconds) { // implementation on the next slide 14 November 2007 Ariel Shamir 47 Clock Constructor Implementation public Clock(int hours, int minutes, int seconds) { if ((seconds >= 0) && (seconds < 60) && (minutes >= 0) && (minutes < 60) && (hours >= 0) && (hours < 24)) { this.hours = hours; this.minutes = minutes; this.seconds = seconds; else { this.hours = 0; this.minutes = 0; this.seconds = 0; 14 November 2007 Ariel Shamir 48 16

17 Using Overloaded Constructors // default constructor... Clock c1 = new Clock(); // Clock(int, int, int) constructor... Clock c2 = new Clock(23,12,50); The constructor must always check the parameters: in no way may an object be initialized to an inconsistent state! What is an inconsistent state in a clock? 14 November 2007 Ariel Shamir 49 Method Overloading Discussion Advantages: Opens up namespace Means shorter names An overloaded constructor provides multiple ways to set up a new object Disadvantages: Can accidentally invoke wrong version as a result of human error (leaving off parameter, forgetting a cast) 14 November 2007 Ariel Shamir 50 Interface Vs. Implementation Objects have two kinds of behavior: Outer send & receive messages, relations with other objects. Inner processing and computing the messages. Usually these are called the interface (outer) vs. Implementation (inner). 14 November 2007 Ariel Shamir 51 17

18 Implement & Interface Example: Point class Point { private float m_x,m_y; public float getx() { return m_x; public float gety() { return m_y; public void setx(float x) { m_x=x; public void setx(float x) { m_x=x; Interface implementation 14 November 2007 Ariel Shamir 52 Encapsulation Object should generally hide their implementation and internal representation (data). This makes the use of objects more easy and allows later changes in the implementation. 14 November 2007 Ariel Shamir 53 Black Box An encapsulated object can be thought of as a black box; its inner workings are hidden from other objects, clients or user. a Point object getx() gety() setx() sety() object interface client 14 November 2007 Ariel Shamir 54 18

19 Encapsulation Advantages In black box programming user gets object interface but should not have to be aware of internal implementation of it. The interface cannot be bypassed. Encapsulation makes an object easy to manage because all communication with the object are done through well defined services. 14 November 2007 Ariel Shamir 55 Modularity The use of objects ease the design and maintenance of complex systems. We divide the system into several autonomous components (objects). Each has a well defined role and interface. These objects interact between them to achieve the functionality of the whole system. The components themselves can be further divided into smaller components. 14 November 2007 Ariel Shamir 56 Components & Reuse Building our system from smaller components, has several advantages: It is easier to understand the systems in terms of a collection of few interoperating object. We can correct/improve the implementation of one component, without affecting the others. A component may be used later in other places. We may find components written by others that are suitable for our needs. 14 November 2007 Ariel Shamir 57 19

20 Reusable Objects Design Autonomous - a component should be an autonomous entity, so it could work anywhere. Abstraction - it should have a clear abstraction, so others can easily understand it s behavior (know what to expect from it). Clear interface - it should have a clear interface so it will be easy to work with, and to maintain. Documentation & Naming - without documentation and good naming for interface methods, no one will understand how to use it. 14 November 2007 Ariel Shamir 58 Visibility Modifiers We accomplish encapsulation through the appropriate use of visibility modifiers A modifier is a Java reserved word that specifies particular characteristics of a programming construct (The final modifier is used for constants) Java has three visibility modifiers: public, private, and protected 14 November 2007 Ariel Shamir 59 Classes Visibility Modifiers A class can be defined either with the public modifier or without a visibility modifier. If a class is declared as public it can be used by any other class If a class is declared without a visibility modifier it has a default visibility. 14 November 2007 Ariel Shamir 60 20

21 Default Visibility The default visibility draws a limit to which other classes can use this class. We will discuss default visibility later in the course. Classes that define a new type of objects, that are supposed to be used anywhere, should be declared public. 14 November 2007 Ariel Shamir 61 Using a Public Class Any other class can use a MyClass which is defined as a public class. public MyClass { // 14 November 2007 Ariel Shamir 62 Class Members We use the generic term member to describe a variable, a method or a constructor of the class. Class instances (object) share the class method members (the code for execution) but have unique copy of the class data members (variables). 14 November 2007 Ariel Shamir 63 21

22 Access to Members You access a class member using an object reference +. + member name: leon.taildown() this.hours Or using class name if it is static: Math.sqrt() 14 November 2007 Ariel Shamir 64 Data Members Vs. Method Members P1 m_x 4 m_y 7.5 getx() gety() setx() sety() class point { float m_x,m_y; getx() { gety() { setx() { sety() { P2 m_x 0.5 m_y 33 getx() gety() setx() sety() 14 November 2007 Ariel Shamir 65 Members Visibility Modifiers Members of a class can be declared as private, protected, public or without a visibility modifier: private int hours; protected int hours; int hours; public int hours; later Members that are declared without a visibility modifier are said to have default visibility. 14 November 2007 Ariel Shamir 66 22

23 Public Visibility Members that are declared as public can be accessed from any class that can access the class of the member. We expose methods that are part of the interface of the class by declaring them as public. Example: the methods gethours(), hourelapsed() and settime() are part of the interface of class Clock so we define them as public. 14 November 2007 Ariel Shamir 67 Private Visibility A class member that is declared as private, can be accessed only by code that is within the class of this member. We hide the internal implementation of the class by declaring its data variables and auxiliary methods as private. Data hiding is essential for encapsulation. 14 November 2007 Ariel Shamir 68 Illegal Access // Example of illegal access public class BankAccount { private float balance; //... class BankAccountTest { public static void main(string[] args) { BankAccount victim = new BankAccount( ); victim.balance = victim.balance - 500; // this will not compile! 14 November 2007 Ariel Shamir 69 23

24 Encapsulation Level Encapsulation comes in the class level and not in the object level. Private access disallowed! Private access allowed! 14 November 2007 Ariel Shamir 70 Encapsulation Among Instances of Same Class Sometimes object instances of the same class need to access each other s guts (e.g., for state copying - if we want to create an identical instance of an object we have). In Java object of a certain class (instances) can access private members of other object of the same class! 14 November 2007 Ariel Shamir 71 BankAccount Example From within a BankAccount object, any private member of a different BankAccount object can be accessed: /** * Transfers a given amount into another bank account */ public void transfer(float amount, BankAccont other) { //... perhaps perform some security checks this.withdraw(amount); other.deposit(amount); // an alternative way of doing the same thing: // other.balance = other.balance + amount 14 November 2007 Ariel Shamir 72 24

25 Class Vs. Objects Most of the data and methods reside and work on specific objects of a class. The class is only a blueprint used as object definition. This view is not totally correct. There are situations in which data and/or methods reside in the class itself. 14 November 2007 Ariel Shamir 73 Static Data Members Only one instance of a static variable exists for the whole class The value of the variable is one for all instances The variable exist even before any objects of the class are instantiated By default static variables are initialized to 0 or null 14 November 2007 Ariel Shamir 74 Static Variable Chart P1 m_x 4 m_y 7.5 m_count getx() gety() setx() sety() class point { float m_x,m_y; static int m_count 2 getx() { gety() { setx() { sety() { P2 m_x 0.5 m_y 33 m_count getx() gety() setx() sety() 14 November 2007 Ariel Shamir 75 25

26 Static Methods Static methods can operate only on static variables or call other static methods. They are not connected to a specific instance and therefore cannot use this reference. Instance methods can access static variables! 14 November 2007 Ariel Shamir 76 Static Method Call The access to a static method is not done through an object but through the class name itself: float i = Math.random(); String s = String.valueOf(i); If a static method calls another static method of the same class than the classname can be omitted. 14 November 2007 Ariel Shamir 77 Defining Static Members All static data/methods are marked with a static modifier. static <type> variable_name; static <type> function_name(...); Examples: static int count; static void foo(); 14 November 2007 Ariel Shamir 78 26

27 The Main Method The main method is static; it is invoked by the system without creating an instance object! public static void main(string[] args); 14 November 2007 Ariel Shamir 79 Static Variables Example Frequent usage of a static variable is as a counter holding the number of objects of the class. class Visitor { static int m_count; public Visitor() { m_count++; 14 November 2007 Ariel Shamir 80 Static Variables Value Change Follow the changes in m_count variable as we instantiate objects of Visitor class: class Theater { public static void main(...) { // Visitor avi = new Visitor(); // Visitor beni = new Visitor(); // m_count = 0 m_count = 1 m_count = 2 14 November 2007 Ariel Shamir 81 27

28 /** * A bank account. */ public class BankAccount { private long number; private static int nextid = 1; // The balance in dollars private float balance; /** * Constructs a new empty account. */ public BankAccount() { this.accountnumber = nextid++; this.balance = 0; // more methods November 2007 Ariel Shamir 82 Usage // Usage... BankAccount gadiaccount = new BankAccount(); BankAccount saritaccount = new BankAccount(); BankAccount arikaccount = new BankAccount(); 14 November 2007 Ariel Shamir 83 Static Variable Chart GadiAccount balance 0 number 1 nextid class BankAccount { float balance; int number; static int nextid 1 BankAccount() { November 2007 Ariel Shamir 84 28

29 Static Variable Chart GadiAccount balance 0 number 1 nextid class BankAccount { float balance; int number; static int nextid 2 BankAccount() {... SariAccount balance 0 number 2 nextid 14 November 2007 Ariel Shamir 85 Static Variable Chart GadiAccount balance 0 number 1 nextid class BankAccount { float balance; int number; static int nextid 3 BankAccount() {... ArikAccount balance 0 number 3 nextid SariAccount balance 0 number 2 nextid 14 November 2007 Ariel Shamir 86 A Collection of Static Methods Even if no static variables exist static methods can still be used to implement general functionality. They just encapsulate a given task, a given algorithm. We can write a class that is a collection of static methods. Such a class isn t meant to define new type of objects. 14 November 2007 Ariel Shamir 87 29

30 Example java.lang.math Class /** * A library of mathematical methods. */ public class Math { // Computes the trigonometric sine of an angle. public static double sin(double x) { //... // Computes the logarithm of a given number. public static double log(double x) { //... // November 2007 Ariel Shamir 88 Use of Math Methods It is just used as a library for utilities that are related in some way: double x = Math.sin(alpha); int c = Math.max(a,b); double y = Math.random(); 14 November 2007 Ariel Shamir 89 30

Objects and Classes: Working with the State and Behavior of Objects

Objects and Classes: Working with the State and Behavior of Objects Objects and Classes: Working with the State and Behavior of Objects 1 The Core Object-Oriented Programming Concepts CLASS TYPE FACTORY OBJECT DATA IDENTIFIER Classes contain data members types of variables

More information

Objects and Classes -- Introduction

Objects and Classes -- Introduction Objects and Classes -- Introduction Now that some low-level programming concepts have been established, we can examine objects in more detail Chapter 4 focuses on: the concept of objects the use of classes

More information

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Class Encapsulation Anatomy of a Method Writing Classes Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: class definitions instance data encapsulation and

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation Outline Problem: Create, read in and print out four sets of student grades Setting up the problem Breaking

More information

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 Objects 3 1 Static member variables So far: Member variables

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of January 21, 2013 Abstract Review of object-oriented programming concepts: Implementing

More information

Lecture 5: Methods CS2301

Lecture 5: Methods CS2301 Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int

More information

Chapter 4: Writing Classes

Chapter 4: Writing Classes Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own

More information

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others COMP-202 Objects, Part III Lecture Outline Static Member Variables Parameter Passing Scopes Encapsulation Overloaded Methods Foundations of Object-Orientation 2 Static Member Variables So far, member variables

More information

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 - Week 7 1 Static member variables So far: Member variables

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:

More information

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects CmSc 150 Fundamentals of Computing I Lesson 28: Introduction to Classes and Objects in Java 1. Classes and Objects True object-oriented programming is based on defining classes that represent objects with

More information

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods Encapsulation You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods external - the interaction of the object with other objects in the program

More information

CS110: PROGRAMMING LANGUAGE I

CS110: PROGRAMMING LANGUAGE I CS110: PROGRAMMING LANGUAGE I Computer Science Department Lecture 8: Methods Lecture Contents: 2 Introduction Program modules in java Defining Methods Calling Methods Scope of local variables Passing Parameters

More information

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. Chapter 5 Methods 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Problem int sum = 0; for (int i = 1; i

More information

C++ & Object Oriented Programming Concepts The procedural programming is the standard approach used in many traditional computer languages such as BASIC, C, FORTRAN and PASCAL. The procedural programming

More information

JAVA GUI PROGRAMMING REVISION TOUR III

JAVA GUI PROGRAMMING REVISION TOUR III 1. In java, methods reside in. (a) Function (b) Library (c) Classes (d) Object JAVA GUI PROGRAMMING REVISION TOUR III 2. The number and type of arguments of a method are known as. (a) Parameter list (b)

More information

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong: Exception Handling Run-time errors The exception concept Throwing exceptions Handling exceptions Declaring exceptions Creating your own exception 22 November 2007 Ariel Shamir 1 Run-time Errors Sometimes

More information

Example: Fibonacci Numbers

Example: Fibonacci Numbers Example: Fibonacci Numbers Write a program which determines F n, the (n + 1)-th Fibonacci number. The first 10 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The sequence of Fibonacci numbers

More information

CS112 Lecture: Defining Instantiable Classes

CS112 Lecture: Defining Instantiable Classes CS112 Lecture: Defining Instantiable Classes Last revised 2/3/05 Objectives: 1. To describe the process of defining an instantiable class 2. To discuss public and private visibility modifiers. Materials:

More information

An introduction to Java II

An introduction to Java II An introduction to Java II Bruce Eckel, Thinking in Java, 4th edition, PrenticeHall, New Jersey, cf. http://mindview.net/books/tij4 jvo@ualg.pt José Valente de Oliveira 4-1 Java: Generalities A little

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug, 1 To define methods, invoke methods, and pass arguments to a method ( 5.2-5.5). To develop reusable code that is modular, easy-toread, easy-to-debug, and easy-to-maintain. ( 5.6). To use method overloading

More information

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong: Exception Handling Run-time errors The exception concept Throwing exceptions Handling exceptions Declaring exceptions Creating your own exception Ariel Shamir 1 Run-time Errors Sometimes when the computer

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

Encapsulation. Mason Vail Boise State University Computer Science

Encapsulation. Mason Vail Boise State University Computer Science Encapsulation Mason Vail Boise State University Computer Science Pillars of Object-Oriented Programming Encapsulation Inheritance Polymorphism Abstraction (sometimes) Object Identity Data (variables) make

More information

Recursion 1. Recursion is the process of defining something in terms of itself.

Recursion 1. Recursion is the process of defining something in terms of itself. Recursion 1 Recursion is the process of defining something in terms of itself. A method that calls itself is said to be recursive. Recursion is an alternative form of program control. It is repetition

More information

Chapter 5 Methods / Functions

Chapter 5 Methods / Functions Chapter 5 Methods / Functions 1 Motivations A method is a construct for grouping statements together to perform a function. Using a method, you can write the code once for performing the function in a

More information

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. OOPs Concepts 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. Type Casting Let us discuss them in detail: 1. Data Hiding: Every

More information

Imperative Languages!

Imperative Languages! Imperative Languages! Java is an imperative object-oriented language. What is the difference in the organisation of a program in a procedural and an objectoriented language? 30 class BankAccount { private

More information

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation Anatomy of a Method September 15, 2006 HW3 is due Today ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev Midterm 1 Next Tuesday Sep 19 @ 6:30 7:45pm. Location:

More information

Chapter 6 Methods. Dr. Hikmat Jaber

Chapter 6 Methods. Dr. Hikmat Jaber Chapter 6 Methods Dr. Hikmat Jaber 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Problem int sum = 0; for (int i = 1; i

More information

Lecture 06: Classes and Objects

Lecture 06: Classes and Objects Accelerating Information Technology Innovation http://aiti.mit.edu Lecture 06: Classes and Objects AITI Nigeria Summer 2012 University of Lagos. What do we know so far? Primitives: int, float, double,

More information

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition Chapter 4: Writing Classes Objects An object has: Presentation slides for state - descriptive characteristics Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus,

More information

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects Administrative Stuff September 12, 2007 HW3 is due on Friday No new HW will be out this week Next Tuesday we will have Midterm 1: Sep 18 @ 6:30 7:45pm. Location: Curtiss Hall 127 (classroom) On Monday

More information

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos. Lecture 05: Methods AITI Nigeria Summer 2012 University of Lagos. Agenda What a method is Why we use methods How to declare a method The four parts of a method How to use (invoke) a method The purpose

More information

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Chapter 5 Methods 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Problem int sum = 0; for (int i = 1; i

More information

Agenda: Notes on Chapter 3. Create a class with constructors and methods.

Agenda: Notes on Chapter 3. Create a class with constructors and methods. Bell Work 9/19/16: How would you call the default constructor for a class called BankAccount? Agenda: Notes on Chapter 3. Create a class with constructors and methods. Objectives: To become familiar with

More information

The return Statement

The return Statement The return Statement The return statement is the end point of the method. A callee is a method invoked by a caller. The callee returns to the caller if the callee completes all the statements (w/o a return

More information

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited Table of Contents Date(s) Title/Topic Page #s 11/6 Chapter 3 Reflection/Corrections 56 Chapter 4: Writing Classes 4.1 Objects Revisited 57 58-59 look over your Ch 3 Tests and write down comments/ reflections/corrections

More information

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1 CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining

More information

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class CS112 Lecture: Defining Classes Last revised 2/3/06 Objectives: 1. To describe the process of defining an instantiable class Materials: 1. BlueJ SavingsAccount example project 2. Handout of code for SavingsAccount

More information

STUDENT LESSON A5 Designing and Using Classes

STUDENT LESSON A5 Designing and Using Classes STUDENT LESSON A5 Designing and Using Classes 1 STUDENT LESSON A5 Designing and Using Classes INTRODUCTION: This lesson discusses how to design your own classes. This can be the most challenging part of

More information

JAVA: A Primer. By: Amrita Rajagopal

JAVA: A Primer. By: Amrita Rajagopal JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently

More information

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226 Method Invocation Note that the input parameters are sort of variables declared within the method as placeholders. When calling the method, one needs to provide arguments, which must match the parameters

More information

Methods and Data (Savitch, Chapter 5)

Methods and Data (Savitch, Chapter 5) Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private 2 public class Temperature { public static void main(string[] args) {

More information

Methods (Deitel chapter 6)

Methods (Deitel chapter 6) Methods (Deitel chapter 6) 1 Plan 2 Introduction Program Modules in Java Math-Class Methods Method Declarations Argument Promotion Java API Packages Random-Number Generation Scope of Declarations Methods

More information

Methods (Deitel chapter 6)

Methods (Deitel chapter 6) 1 Plan 2 Methods (Deitel chapter ) Introduction Program Modules in Java Math-Class Methods Method Declarations Argument Promotion Java API Packages Random-Number Generation Scope of Declarations Methods

More information

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types COMP-202 Unit 6: Arrays Introduction (1) Suppose you want to write a program that asks the user to enter the numeric final grades of 350 COMP-202

More information

Lecture 3. Lecture

Lecture 3. Lecture True Object-Oriented programming: Dynamic Objects Static Object-Oriented Programming Reference Variables Eckel: 30-31, 41-46, 107-111, 114-115 Riley: 5.1, 5.2 D0010E Object-Oriented Programming and Design

More information

Chapter 4. Defining Classes I

Chapter 4. Defining Classes I Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object-oriented programming (OOP) possible Programming in Java consists of defining a number of classes

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

JAVA Programming Concepts

JAVA Programming Concepts JAVA Programming Concepts M. G. Abbas Malik Assistant Professor Faculty of Computing and Information Technology University of Jeddah, Jeddah, KSA mgmalik@uj.edu.sa Find the sum of integers from 1 to 10,

More information

Object Oriented Modeling

Object Oriented Modeling Object Oriented Modeling Object oriented modeling is a method that models the characteristics of real or abstract objects from application domain using classes and objects. Objects Software objects are

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

Chapter 5 Methods. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

Chapter 5 Methods. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk Chapter 5 Methods Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk ١ Introducing Methods A method is a collection of statements that

More information

Object Oriented Methods : Deeper Look Lecture Three

Object Oriented Methods : Deeper Look Lecture Three University of Babylon Collage of Computer Assistant Lecturer : Wadhah R. Baiee Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces,

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

ITI 1120 Lab #9. Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa

ITI 1120 Lab #9. Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa ITI 1120 Lab #9 Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa 1 Objectives Review fundamental concepts Example: the Time class Exercises Modify the Time class

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

CS110D: PROGRAMMING LANGUAGE I

CS110D: PROGRAMMING LANGUAGE I CS110D: PROGRAMMING LANGUAGE I Computer Science department Lecture 7&8: Methods Lecture Contents What is a method? Static methods Declaring and using methods Parameters Scope of declaration Overloading

More information

Implementing Classes (P1 2006/2007)

Implementing Classes (P1 2006/2007) Implementing Classes (P1 2006/2007) Fernando Brito e Abreu (fba@di.fct.unl.pt) Universidade Nova de Lisboa (http://www.unl.pt) QUASAR Research Group (http://ctp.di.fct.unl.pt/quasar) Chapter Goals To become

More information

Programming Language Concepts: Lecture 2

Programming Language Concepts: Lecture 2 Programming Language Concepts: Lecture 2 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2011 PLC 2011, Lecture 2, 6 January 2011 Classes and

More information

Chapter 15: Object Oriented Programming

Chapter 15: Object Oriented Programming Chapter 15: Object Oriented Programming Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey How do Software Developers use OOP? Defining classes to create objects UML diagrams to

More information

Creating an object Instance variables

Creating an object Instance variables Introduction to Objects: Semantics and Syntax Defining i an object Creating an object Instance variables Instance methods What is OOP? Object-oriented programming (constructing software using objects)

More information

A Foundation for Programming

A Foundation for Programming 2.1 Functions A Foundation for Programming any program you might want to write objects functions and modules build bigger programs and reuse code graphics, sound, and image I/O arrays conditionals and

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 4 Chapter 4 1 Basic Terminology Objects can represent almost anything. A class defines a kind of object. It specifies the kinds of data an object of the class can have.

More information

Lecture 04 FUNCTIONS AND ARRAYS

Lecture 04 FUNCTIONS AND ARRAYS Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions

More information

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Announcements PS 3 is due Thursday, 10/6 Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Room TBD Scope: Lecture 1 to Lecture 9 (Chapters 1 to 6 of text) You may bring a sheet of paper (A4, both sides) Tutoring

More information

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. Chapter 5 Methods rights reserved. 0132130807 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. rights reserved. 0132130807 2 1 Problem int sum =

More information

Lecture 7 Objects and Classes

Lecture 7 Objects and Classes Lecture 7 Objects and Classes An Introduction to Data Abstraction MIT AITI June 13th, 2005 1 What do we know so far? Primitives: int, double, boolean, String* Variables: Stores values of one type. Arrays:

More information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are

More information

Object Oriented Programming in C#

Object Oriented Programming in C# Introduction to Object Oriented Programming in C# Class and Object 1 You will be able to: Objectives 1. Write a simple class definition in C#. 2. Control access to the methods and data in a class. 3. Create

More information

Implementing Classes

Implementing Classes Implementing Classes Advanced Programming ICOM 4015 Lecture 3 Reading: Java Concepts Chapter 3 Fall 2006 Slides adapted from Java Concepts companion slides 1 Chapter Goals To become familiar with the process

More information

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation COMP-202 Unit 8: Defining Your Own Classes CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation Defining Our Own Classes (1) So far, we have been creating

More information

CH. 2 OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED

More information

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple

More information

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems Basics of OO Programming with Java/C# Basic Principles of OO Abstraction Encapsulation Modularity Breaking up something into smaller, more manageable pieces Hierarchy Refining through levels of abstraction

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

BBM 102 Introduction to Programming II Spring 2017

BBM 102 Introduction to Programming II Spring 2017 BBM 102 Introduction to Programming II Spring 2017 Classes and Objects in Java Instructors: Ayça Tarhan, Fuat Akal, Gönenç Ercan, Vahid Garousi TAs: Selma Dilek, Selim Yılmaz, Selman Bozkır 1 Today Defining

More information

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com CHAPTER 8 OBJECTS AND CLASSES Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/2011 Chapter Goals To understand the concepts of classes, objects and encapsulation To implement instance variables,

More information

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Fields vs local variables and scope Program Structure; the keyword static Classes vs objects Creating and using objects

More information

Software Development With Java CSCI

Software Development With Java CSCI Software Development With Java CSCI-3134-01 D R. R A J S I N G H Outline Week 8 Controlling Access to Members this Reference Default and No-Argument Constructors Set and Get Methods Composition, Enumerations

More information

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Classes Chapter 4 Classes and Objects Data Hiding and Encapsulation Function in a Class Using Objects Static Class members Classes Class represents a group of Similar objects A class is a way to bind the

More information

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class

More information

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.! True object-oriented programming: Dynamic Objects Reference Variables D0010E Object-Oriented Programming and Design Lecture 3 Static Object-Oriented Programming UML" knows-about Eckel: 30-31, 41-46, 107-111,

More information

Chapter 5 Methods. Modifier returnvaluetype methodname(list of parameters) { // method body; }

Chapter 5 Methods. Modifier returnvaluetype methodname(list of parameters) { // method body; } Chapter 5 Methods 5.1 Introduction A method is a collection of statements that are grouped together to perform an operation. You will learn how to: o create your own mthods with or without return values,

More information

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

ICOM 4015: Advanced Programming

ICOM 4015: Advanced Programming ICOM 4015: Advanced Programming Lecture 3 Reading: Chapter Three: Implementing Classes Copyright 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes Chapter Goals To become

More information

CS 170, Section /3/2009 CS170, Section 000, Fall

CS 170, Section /3/2009 CS170, Section 000, Fall Lecture 18: Objects CS 170, Section 000 3 November 2009 11/3/2009 CS170, Section 000, Fall 2009 1 Lecture Plan Homework 5 : questions, comments? Managing g g Data: objects to make your life easier ArrayList:

More information

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() { Scoping, Static Variables, Overloading, Packages In this lecture, we will examine in more detail the notion of scope for variables. We ve already indicated that variables only exist within the block they

More information

Chapter 2: Java OOP I

Chapter 2: Java OOP I Chapter 2: Java OOP I Yang Wang wyang AT njnet.edu.cn Outline OO Concepts Class and Objects Package Field Method Construct and Initialization Access Control OO Concepts Object Oriented Methods Object An

More information

COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi

COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi Chapter 4 Loops for while do-while Last Week Chapter 5 Methods Input arguments Output Overloading Code reusability Scope of

More information

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing) 2.1 Functions Functions Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing) Return one output value Input Arguments x y z f Return Value f (x, y, z)

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 15 Class Relationships Outline Problem: How can I create and store complex objects? Review of static methods Consider static variables What about objects

More information

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS121/IS223. Object Reference Variables. Dr Olly Gotel CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223

More information