Access Control to Class Members tMyn1 Access Control to Class Members In its support for encapsulation, the class provides two major benefits. First, it.

Slides:



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

Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Programming Methodology (1). Implementing Methods main.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan
Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.
INTRODUCTION TO JAVA PROGRAMMING Chapter 1. What is Computer Programming?
The switch statement: an N-way selection statement.
UML Basics & Access Modifier
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
CSC 212 Object-Oriented Programming and Java Part 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 10 Thinking in Objects.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4_1 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Assignment statements using the same variable in LHS and RHS.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Defining a ClasstMyn1 Defining a Class A class is a template that defines the form of an object. It specifies both the data and the code that will operate.
PART 1 Part 1: The Material. Whether you knew it or not, all the programming that you have performed until now was in the boundaries of procedural programming.
1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
CITA 342 Section 1 Object Oriented Programming (OOP)
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Topics Instance variables, set and get methods Encapsulation
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Topic: Classes and Objects
Chapter 2 Clarifications
OOP: Encapsulation &Abstraction
Classes (Part 1) Lecture 3
Programming Logic and Design Seventh Edition
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
The Lifetime of a Variable
Objects as a programming concept
COMPUTER 2430 Object Oriented Programming and Data Structures I
Table of Contents Class Objects.
Data types, Expressions and assignment, Input from User
Something about Java Introduction to Problem Solving and Programming 1.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Classes Variables That Are Not of a Built-in Type Are Objects
Corresponds with Chapter 7
Classes & Objects: Examples
OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS
Week 4 Lecture-2 Chapter 6 (Methods).
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Chapter 9 Carrano Chapter 10 Small Java
ITE “A” GROUP 2 ENCAPSULATION.
C++ Object Oriented 1.
CS 240 – Advanced Programming Concepts
CSG2H3 Object Oriented Programming
Presentation transcript:

Access Control to Class Members tMyn1 Access Control to Class Members In its support for encapsulation, the class provides two major benefits. First, it links data with the code that manipulates it. Second, it provides the means by which access to members can be controlled. In essence, there are two basic types of class members: public and private. A public member can be freely accessed by code defined outside of its class.

Access Control to Class Members tMyn2 A private member can be accessed only by other methods defined by its class. It is through the use of private members that access is controlled. Restricting access to a class’s members is a fundamental part of object-oriented programming because it helps prevent the misuse of an object. By allowing access to private data only through a well- defined set of methods, you can prevent improper values from being assigned to that data – by performing a range check, for example. It is not possible for code outside the class to set the value of a private member directly.

Access Control to Class Members tMyn3 You can also control precisely how and when the data within an object is used. Thus, when correctly implemented, a class creates a “black box” that can be used, but the inner workings of which are not open to tampering. The default setting is essentially public. Member access control is achieved through the use of three access specifiers (access modifiers): public, private, and protected. The protected access specifier applies only when inheritance is involved and it is described later.

Access Control to Class Members tMyn4 When a member of a class is modified by the public access specifier, that member can be accessed by any other code in your program wherever the program has a reference to an object of that class or one of its derived classes. When a member of a class is specified as private, that member can be accessed only by other members of its class. Thus, methods in other classes cannot access a private member of another class. The default access setting (in which no access specifier is used) is the same as public unless your program is broken down into packages.

Access Control to Class Members tMyn5 A package is, essentially, a grouping of classes. Packages are both an organizational and access control feature, and those topics will be discussed later. An access specifier must begin a member’s declaration statement.

Access Control to Class Members tMyn6 package TimoSoft; public class Student { private String firstName; private String lastName; public void getFirstName() { System.out.println("The first name is "+firstName+"."); } public void getLastName() { System.out.println("The last name is "+lastName+"."); } public void setFirstName(String fVal) { firstName=fVal; }

Access Control to Class Members tMyn7 public void setLastName(String lVal) { lastName=lVal; } public Student(String first, String last) { setFirstName(first); setLastName(last); }

Access Control to Class Members tMyn8 Let’s modify the previous example: the instance variables will be asked from the user of the program:

Access Control to Class Members tMyn9 package TimoSoft; import java.util.Scanner; public class Student { private String firstName; private String lastName; public void getFirstName() { System.out.println("The first name is "+firstName+"."); } public void getLastName() { System.out.println("The last name is "+lastName+"."); } public void setFirstName() { Scanner fromKeyboard=new Scanner(System.in); System.out.print("Enter the first name, please: "); firstName=fromKeyboard.nextLine(); }

Access Control to Class Members tMyn10 public void setLastName() { Scanner fromKeyboard=new Scanner(System.in); System.out.print("Enter the last name, please: "); lastName=fromKeyboard.nextLine(); } public Student() { setFirstName(); setLastName(); }

Access Control to Class Members tMyn11 package TimoSoft; public class StudentTest { public static void main(String[] args) { Student first=new Student(); first.getFirstName(); first.getLastName(); } run: Enter the first name, please: Susan "YouTube" Enter the last name, please: Boyle The first name is Susan "YouTube". The last name is Boyle. BUILD SUCCESSFUL (total time: 23 seconds)