 The position of a method in a program is not critical.  Why?

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Summary of the lecture We discussed –variable scope –instance variable declarations –variable lifetime.
Road Map Introduction to object oriented programming. Classes
Access to Names Namespaces, Scopes, Access privileges.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Classes, Encapsulation, Methods and Constructors
Terms and Rules Professor Evan Korth New York University (All rights reserved)
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Writing Classes (Chapter 4)
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Objects Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Arranging the border values of methods. import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
10-Nov-15 Java Object Oriented Programming What is it?
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Puzzle 1  what does the following program print? public class Puzzle01 { public static void main(String[] args) { System.out.print("C" + "S" + "E"); System.out.println('1'
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Topics Instance variables, set and get methods Encapsulation
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.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Class Fundamentals BCIS 3680 Enterprise Programming.
A First Book of ANSI C Fourth Edition
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
2 Chapter Classes & Objects.
University of Central Florida COP 3330 Object Oriented Programming
The Lifetime of a Variable
Storage class in C Topics Automatic variables External variables
University of Central Florida COP 3330 Object Oriented Programming
Namespaces, Scopes, Access privileges
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Encapsulation and Constructors
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Group Status Project Status.
Classes and Objects.
Object Oriented Programming Using C++
Namespaces, Scopes, Access privileges
Chapter 5: Classes and Objects in Depth
Submitted By : Veenu Saini Lecturer (IT)
Chapter 5: Classes and Objects in Depth
Classes, Objects and Methods
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

 The position of a method in a program is not critical.  Why?

 Variables can exist for: ◦ The lifetime of the program ◦ The lifetime of a method  This is known as the ‘scope’ of a variable.

 Variable can be used: ◦ Throughout the entire program ◦ In only one method  These types of variable are (respectively): ◦ Global (instance) ◦ Local

 A variables scope is determined by where in the program it is declared

 Variables declared inside the body of a method are usable only in that method.  The variables, known as local variables, will disappear when the method is finished.

 Variables declared outside any method but inside the body of a class are usable by every method.  These variables, known as global variables, will disappear only when the object is destroyed  In object orientation global variables are usually referred to as instance variables.

 Once arguments are passed to a method, they are local to the method (local variables), once the method has completed its operation, these local variables are removed from memory.  Instance variable are different to local variables as they are declared outside of methods and are thus available to all methods in the class - instance variables are not removed from memory until the object itself is.  Scope is the part of a program in which a variable exists and can be used. If you try to use a variable outside of its scope, an error will occur

public class scopeClass { int globalVar;//instance variable with global scope public static void main(String args[]) { scopeClass scopeClassObject = new scopeClass(); } public scopeClass() { int localVar;//local scope localVar = 1; globalVar = 4; System.out.println(globalVar+":"+localVar); compute(); System.out.println(globalVar+":"+localVar); } public void compute() { this.globalVar = globalVar + 1; }

 Variables names should always be unique, however if variables can be distinguished in a way other than their names the compiler will allow two variables with the same name.  Two variables with different scope can have the same name.  Note: although permitted doing this is not recommended as it often leads to confusion for readers of a program.

 Two forms of visibility in an object ◦ Internal – what can be seen by the methods inside the object: Local & Global  Controlled by placing variables in relevant positions ◦ External – what can be seen by other objects:Public and Private  Controlled using the visibility modifiers public & private

 We have seen the idea of public/ private, we now look at it in more detail

 Public is the equivalent of making something publicly available, e.g. publishing it on a web site or in a newspaper.

 Private methods and variables can only be accessed by methods within the object.

 Person object:  Public attributes – things people can get at without your permission ◦ Phone number ◦ Car registration ◦ Name  Private attributes – things no-one can get without your express permission ◦ ATM password

 Note that there is a distinct difference between the scope of a variable and its modifier.  Local variables do not need modifiers as they can never be seen outside the object since they are contained within a method. The method that contains the local variables will need a modifier.  Global variables do need modifiers as they are outside all the methods and therefore are part of the object thus needing an indication as to whether they are public or private.

 One of the benefits of classes is that you can protect member variables and methods from access by other objects (variables in particular)  It is good programming practice to make instance variables private and to access/modify the values of these using public getter and setter methods  Private: A private member is accessible only to its own class and nowhere else. Using the private modifier is the main way in which an object encapsulates itself.  Useful when:  other classes have no reason to use a variable/method  an inappropriate value could cause major problems