SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Inheritance Inheritance Reserved word protected Reserved word super
Access to Names Namespaces, Scopes, Access privileges.
1 Repetition structures Overview while statement for statement do while statement.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 7 Inheritance, Polymorphism, and Scope.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Saravanan.G.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
The Java Programming Language
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Nested Classes CompSci 230 S Software Construction.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Topics Instance variables, set and get methods Encapsulation
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Abstract classes and interfaces
CompSci 230 S Software Construction
JAVA MULTIPLE CHOICE QUESTION.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Examples of Classes & Objects
Objects as a programming concept
Inheritance and Polymorphism
CompSci 230 S Programming Techniques
University of Central Florida COP 3330 Object Oriented Programming
Objects, Classes, Program Constructs
I/O Basics.
Namespaces, Scopes, Access privileges
CS 302 Week 11 Jim Williams, PhD.
Abstract classes and interfaces
null, true, and false are also reserved.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Introduction to Java Programming
Classes & Objects: Examples
Namespaces, Scopes, Access privileges
Tonga Institute of Higher Education
Chapter 9 Carrano Chapter 10 Small Java
Abstract classes and interfaces
Introduction to Object-Oriented Concepts in Java
CSG2H3 Object Oriented Programming
Chapter 5 Classes.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

SCOPE & I/O CSC 171 FALL 2004 LECTURE 5

CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at UR

Scope and Accessibility Scope is the “lifetime” of a variable when & where does it “live” Scope rules govern where in the program a variable is accessible Most times java provides explicit modifiers to control scope In two cases, scope rules govern accessibility class scope block scope

Class Scope Within a class definition reference variables of this class’s type can be used to access all members regardless of their accessibility modifiers

Class Light { private int noOfWatts; private String location; private boolean indicator; // true or false public void switchOn() {indicator = true;} public void switchOff() {indicator = false;} public boolean isOn() {return indicator;} public static Light duplicate (Light oldLight) {

public static Light duplicateLight (Light oldLight) { Light newLight = new Light(); newLight.noOfWatts = oldLight.noOfWatts; newLight.indicator = oldLight.indicator; newLight.location = new String(oldLight.location); return newLight; } }// end of class parameter Local variable

Class Scope Method duplicateLight has parameter oldLight local variable newLight Even though the instance variables of the class are private, they are accessible though the references

Block Scope Declarations and statements can be grouped into a block using braces {} The body of a method is a block Blocks can be nested General rule a variable declared in a block is in scope inside the block in which it is declared but is not accessible outside of the block

public static void main(String args[]) { //String args = “ “; // cannot redeclare char digit; { int index = 5 ; { int k = 3; } { int k = 4; // ok // int index ; // not ok } int index = 6 ; // ok }

Member Accessibility Modifiers Accessibility modifiers help a class define its relationship to it’s client members Accessibility of members can be one of public protected default (a.k.a “package”) private “+” “#” “-”

public Members Least restrictive Accessible everywhere Both class (static) and instance members

protected Members Accessible in the package containing this class Accessible by all subclases of the class in any package where this class is visible Non-subclasses in other packages cannot access protected members Less restrictive than default accessibility

default Members When no access modifier is specified for a member It is only accessible by another class in the same package where it’s class is defined Even if a class is visible in another (possibly nested) package, the member is not accessible.

private Members Most restrictive Private members are not accessible from any other class. This applies to subclasses Whether in the same package or not Not to be confused with inheritance

Other Modifiers for members static final abstract synchonized native transient volitile

static Members of the class in which they are declared Not part of any instance of the class The class need not be instantiated to reference static members Static members a.k.a class members

final A final variable is a constant It’s value cannot be changed once initialized Applies to instance, static, and local vars Final methods cannot be overridden (redefined in subclasses)

abstract (incomplete) An abstract method has the following syntax abstract ( ) An abstract method does not have an implementation (no method body) Only a prototype is provided No instantiation is allowed Instantiable subclasses are forced to implement the method body

“advanced” modifiers synchronized – used for multi-threaded code native – used for “foreign” (C,C++) methods transient – if you store it, you don’t save the values volatile – some other thread could change it’s value

IO in JAVA If there is “System.out” There must be “System.in” And other methods – Like “pop ups”

import javax.swing.JOptionPane; public class Popup { public static void main(String [] args) { String input1 = JOptionPane.showInputDialog("First number"); String input2 = JOptionPane.showInputDialog("Second number"); int i1 = Integer.parseInt(input1); int i2 = Integer.parseInt(input2); System.out.println(input1 + " + " + input2 + " == " + (input1 + input2)); JOptionPane.showMessageDialog(null, input1 + " + " + input2 + " == " + i1 + i2); System.exit(0); }

import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException ; public class Console { public static void main(String [] args) throws IOException{ InputStreamReader isreader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(isreader); String input1 = console.readLine(); System.out.println(input1); }