Access. Overview Today we will discuss: –How do we access fields and methods? –Why have access restrictions? –What can have access restrictions? –How.

Slides:



Advertisements
Similar presentations
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Reminder Check the course web site on a regular basis:
Access to Names Namespaces, Scopes, Access privileges.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
20-Jun-15 Methods. Complexity The programmer's biggest adversary is complexity The programmer's biggest adversary is complexity Primitive types and the.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
28-Jun-15 Everything You Ever Wanted To Know About Java O-O.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
16-Jul-15 Access. 2 Overview Questions covered in this talk: How do we access fields and methods? Why have access restrictions? What can have access restrictions?
UML Basics & Access Modifier
Writing Classes (Chapter 4)
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
CSC 212 – Data Structures Lecture 12: Java Review.
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.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
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.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.
1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)
Basic Object-Oriented Concepts – towards implementation CS3340.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
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.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Class Fundamentals BCIS 3680 Enterprise Programming.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
OOP: Encapsulation &Abstraction
Objects as a programming concept
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Creating Your OwnClasses
Namespaces, Scopes, Access privileges
Class Structure 16-Nov-18.
Classes & Objects: Examples
CLASS DEFINITION (FIELDS)
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Unit 1 Lab16.
Namespaces, Scopes, Access privileges
Class Structure 25-Feb-19.
Dr. R Z Khan Handout-3 Classes
Methods 16-May-19.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Classes and Objects CGS3416 Spring 2019.
ITE “A” GROUP 2 ENCAPSULATION.
Classes and Methods 15-Aug-19.
CSG2H3 Object Oriented Programming
Chapter 5 Classes.
Presentation transcript:

Access

Overview Today we will discuss: –How do we access fields and methods? –Why have access restrictions? –What can have access restrictions? –How do we provide or restrict access?

Instance and class variables You can declare variables within a class –These variables are called instance variables, or fields –Every object of that class has its own copy of those fields –The fields describe something about the object You can also declare static variables within a class –There is only one of each static variable –A static variable is also called a class variable –The static variable describes something about the class as a whole

Method variables You can declare variables within a method or within a constructor –These are called method variables, not fields –Method variables are basically used for computation –Method variables are strictly temporary, and are used only within that method –When a method returns (completes), all its variables are discarded

Example: a “Rabbit” class class Rabbit { static int population; // class variable double hunger; //instance variable double fear; // instance variable double courage = 0.75; // instance variable void eat() { double temp; // method variable temp = courage * hunger; if (temp > fear) { System.out.println(“Eating!”); hunger = hunger - 1; } } }

Statements You can declare variables inside a class or inside a method or a constructor You can put statements (executable code) only within methods and constructors, not inside a class Declarations with initializations are still declarations, not statements

Statements must be in methods (or in constructors) class Rabbit { double hunger; // OK--declaration double fear = 5.0; // OK--still a declaration hunger = 5.0; // illegal--assignment stmt. Rabbit ( ) { hunger = 5.0; // OK--in a constructor } void eat ( ) { hunger = hunger - 1; // OK--in a method } }

Access from inside a class Inside a class, you can access other fields and methods inside the class just by naming them Example: class Person { int age; void birthday( ) { age = age + 1; } void growOlder( ) { birthday( ); } }

Accessing from outside a class, 1 Outside a class (from some other class) you access instance variables and methods by –Naming the object you want to talk to –Putting a dot –Naming the variable or method Example: // NOT in class Person, do this: if (john.age < 75) john.birthday();

Accessing from outside a class, 2 Outside a class (from some other class) you access class variables and methods by –Naming the class you want to talk to –Putting a dot –Naming the variable or method Examples: Person.population = Person.population + 1; x = Math.abs(y);

Responsibility In Java, objects are considered to be active –They have behaviors –They are responsible for their own data –Data (variables) must be kept consistent Example: population should never be negative In order for a class or object to be responsible for its own data, it must keep control of that data

Loss of control Suppose a Rabbit object, bugsBunny, has a variable named hunger Inside the class, this method is fine: void eat ( ) { hunger = hunger - 1; } From outside the class, the following is legal: bugsBunny.hunger = bugsBunny.hunger - 1; –But should we be allowed to “reach inside” a rabbit?

private variables and methods If you declare a variable or method to be private, that variable or method can only be accessed from within the class –private methods also make sense, e.g. digest() If you declare a variable or method to be public, then any code anywhere can access it Typically, a class or object has both –Methods for use by the rest of the program –Methods and variables that it alone should control

Levels of access private -- access only from within the class “package” -- access from within the class, or from any class in the same directory (“folder”) –This is the default; there is no package keyword protected -- access from within the class, or from within any subclass, or from any other class in the same directory public -- access from anywhere at all

Levels of access, II To make a variable or method visible –Only within this class: private –From this class and its subclasses: not possible –From this class and its subclasses, and any other class in this directory: “package” (default) –From this subclass and its subclasses, and any other classes in this directory: protected –From anywhere: public

Getters and setters One way to control access is via getters and setters: class Rabbit { private double hunger; public getHunger() { return hunger; } public setHunger(double hunger) { this.hunger = hunger; } This seems silly, but it gives great flexibility Probably overkill for small programs

Immutable objects Suppose a Planet has a mass, and you want to be able to see its mass but not change it: class Planet { private long mass; // Constructor: Planet(long mass) { this.mass = mass; } //getter long getMass() { return mass; } // Notice there is no setter! }

The End