1.00 Lecture 11 Scope and Access. Variable Lifecycles Instance (or object) variables – Created when their containing object is created – Initialized to.

Slides:



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

Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(Java) public, private, protected (default)
Object Oriented Programming with Java
Basic -2 Classes and Objects. Classes and Objects A class is a complex data TYPE An object is an instance of a class. Example: Class: Person Objects:
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Road Map Introduction to object oriented programming. Classes
Access to Names Namespaces, Scopes, Access privileges.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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,
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
1 Inner Classes Overview  Java Inner Classes: A Definition.  Overview of Nested Classes.  Top level Nested Classes.  Non-static Inner Classes.  Non-static.
Introduction to Programming with Java, for Beginners Scope.
INTRODUCTION TO JAVA PROGRAMMING Chapter 1. What is Computer Programming?
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
2-Dec-15 Inner Classes By Alguien Soy. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
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,
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Classes, Interfaces and Packages
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Topics Instance variables, set and get methods Encapsulation
Object Oriented Programming in Java Habib Rostami Lecture 10.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Classes (Part 1) Lecture 3
3 Introduction to Classes and Objects.
Static data members Constructors and Destructors
Inheritance and Polymorphism
Nested class.
Packages and Interfaces
Inner Classes 29-Nov-18.
Namespaces, Scopes, Access privileges
CIS 199 Final Review.
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
CSG2H3 Object Oriented Programming
Presentation transcript:

1.00 Lecture 11 Scope and Access

Variable Lifecycles Instance (or object) variables – Created when their containing object is created – Initialized to default if not explicitly initialized 0 for numbers, false for boolean, null to objects – Destroyed when Java® garbage collector finds there are no remaining active references to object Static (or class) variables – Created when class is first used in program – Initialized to default if not explicitly initialized 0 for numbers, false for boolean, null to objects – Usually exist for rest of program (unless unloaded) Local variables (or block variables) – Created in the statement where theyre defined – Not initialized by default. Contain unpredictable data – Destroyed when block is exited (at ending brace )

Variable Scope Scope: limiting the parts of the program where a variable or method is defined and visible – Prevents collisions between variables and methods in different parts of a program Variables are the big concern, methods are lesser problem – Limits effects of changes to smallest possible module or scope – Lets multiple people work on large programs simultaneously – Allows testing, bug fixing, maintenance to be done while limiting the new bugs introduced

Scope: Variables, Methods Local variables (in a method or block) – Exist from point of definition to end of block Blocks are defined by curly braces{ } Blocks are most often used to define: – Method body – Multiple statements in if-else and loop operations – Local variables exist only within own method No access is given to any other method – If local and instance variable have same name, the local variable hides the instance variable: Access the instance variable as: this.variableName;

Access: Variables, Methods Instance and static variables and methods (in a class) have 4 access modifiers: – Private: Access only to own class methods Data fields should be private, almost always – Public: Access to all methods, all classes Methods intended for other class use are public Methods for internal use only are private – Package: Access to methods of classes in same package (a package is a group of classes) This is the default, alas. Always specify scope explicitly No package keyword; its the default with no keyword – Protected: Used with inheritance (covered later) Like a private variable, except its visible to derived or subclasses (and, in Java®, to other classes in package)

Access: Packages If you add, at the top of your program package packageName; – This will place the classes in your source file into a package, along with any other source files with the same package declaration at the top Packages are placed in folders in your filesystem in Forte and on your Windows PC or Athena workstation – These classes will then have access to each others package-access methods and data fields – Use Fortes New Package feature as convenience To use the package in another class, add at the top of that class: import packageName.*;

Package Example package Lecture11PkgClass; public class PkgClass { public int publicInt; private int privateInt; int packageInt; // Package access public PkgClass(int pu, int pr, int pa) { publicInt= pu; privateInt= pr; packageInt= pa; } public void publicPrint( ) { System.out.println("Public"); } private void privatePrint() { System.out.println("Private"); } void packagePrint() { // Package access System.out.println("Package"); }

Package Test Class 1 package Lecture11PkgClass; // In same package public class PkgTest { public static void main(String[ ] args) { PkgClass object1= new PkgClass(1, 2, 3); int pu= object1.publicInt; int pr= object1.privateInt; int pa=object1.packageInt; object1.publicPrint(); object1.privatePrint(); object1.packagePrint(); System.exit(0); } // Which statements will not compile?

Package Test Class 1 package Lecture11PkgClass; // In same package public class PkgTest { public static void main(String[ ] args) { PkgClass object1= new PkgClass(1, 2, 3); int pu= object1.publicInt; // int pr= object1.privateInt; No access! int pa= object1.packageInt; object1.publicPrint(); // object1.privatePrint(); No access! object1.packagePrint(); System.exit(0); }

Package Test Class 2 package Lecture11TestPkg; // Different package (or none) import Lecture11PkgClass.*; // Import desired package public class PkgTest { public static void main(String[ ] args) { PkgClass object1= new PkgClass(1, 2, 3); int pu= object1.publicInt; int pr= object1.privateInt; int pa=object1.packageInt; object1.publicPrint(); object1.privatePrint(); object1.packagePrint(); System.exit(0); } // Which statements will not compile?

Package Test Class 2 package Lecture11TestPkg; // Different package (or none) import Lecture11PkgClass.*; // Import desired package public class PkgTest { public static void main(String[ ] args) { PkgClass object1= new PkgClass(1, 2, 3); int pu= object1.publicInt; // int pr= object1.privateInt; No access // int pa= object1.packageInt; No access object1.publicPrint(); // object1.privatePrint(); No access // object1.packagePrint(); No access System.exit(0); }

Searching packages Java® always looks in the default package for classes searching imported packages of form java.util.Vector- A classes's full name can be used, as in Vector v = new java.util.Vector();

Inner classes Java® allows you to define classes within another class An inner class is only defined within its outer class Inner classes cannot have static members

Inner class example public class TopLevelClass { private int x; // more code here class InnerClass { private int y; y = x // more code here }

public class TopLevelClass { private int x=4; public static void main(String[ ] arg) { new TopLevelClass();} public TopLevelClass() { InnerClass inst1 = new InnerClass(); System.out.println("Outer = "+x+ "Inner= " + inst1.y); x = 5; InnerClass inst2 = new InnerClass(); System.out.println("Outer = "+x+ "Inner = inst2.y); } private class InnerClass { private int y; public InnerClass() { y = x*x; } }

Uses for inner classes Sometimes used for very specialized classes that aren't intended for reuse Example: Items that are intended to be used on a list, where each item is an instance of an inner class. Java® is a trademark or registered trademark of Sun Microsystems, Inc. In the United States and other countries.