Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.

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)
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Access to Names Namespaces, Scopes, Access privileges.
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.
Lecture 9 Concepts of Programming Languages
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
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.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Ingeniería de Sistemas ertificación en AVA.
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
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 1 Sun Certified Java 1.4 Programmer Chapter 8 Notes Gary Lance
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
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.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Interfaces and Inner Classes
Nested Classes CompSci 230 S Software Construction.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Classes, Interfaces and Packages
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.
Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
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.
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Abstract classes and interfaces
OOP: Encapsulation &Abstraction
9.1 Class (static) Variables and Methods
Examples of Classes & Objects
Polymorphism, Abstract Classes & Interfaces
University of Central Florida COP 3330 Object Oriented Programming
UML Class Diagram: class Rectangle
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Abstract classes and interfaces
Corresponds with Chapter 7
null, true, and false are also reserved.
Conditional Statements
Classes & Objects: Examples
Inner Classes 29-Nov-18.
Interfaces.
Polymorphism, Abstract Classes & Interfaces
Module 2 - Part 1 Variables, Assignment, and Data Types
Abstract classes and interfaces
Inner Classes 1-May-19.
Final and Abstract Classes
Agenda Types and identifiers Practice Assignment Keywords in Java
Arrays Arrays are represented by objects but there is no class that array objects are instances of. Variables of array type are declared using bracket.
Chapter 5 Classes.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected 3. private Non-inner classes can only be public

Friendly Features with no access modifier default to friendly Friendly features are accessible to any class in the same package Classes outside the package may not access these features Friendly classes may be subclassed but their variables/methods are not accessible by the subclass

Modifiers public –Can be used in any Java program without restriction private –may only be used by the instance of the class that declares the variable or method protected –only variables, methods, and inner classes can be declared protected –available to all classes in the same package –available to all subclasses( even those in different packages )

Overriding Methods Methods may not be overwritten to be more private PrivateFriendlyProtectedPublic

Final Modifier Final features may not be overwritten A final class may not be subclassed A final variable cannot be changed once it has been assigned a value

Final Modifier Example class Walrus { int weight; Walrus( int w ) { weight = w }; } class Tester { final Walrus w1 = new Walrus(1500); void test() { w1 = new Walrus(1400); // Illegal w1.weight = 1800;// Legal }

Abstract Modifier An abstract class cannot be instantiated This is a way to defer implementation to subclasses An class with one more methods declared abstract cannot be instantiated A class that is declared to implement an interface but does not implement all the methods of that interface must be abstract Similar to virtual in C++

Abstract Example abstract class Stack { protected int count = 0; public abstract void push( Object o ); public abstract void pop(); public abstract Object top(); public abstract boolean isFull(); public boolean isEmpty() { return count==0; }

Static Modifier Associated with a class, not a particular instance of a class public class StaticTest { static int x = 0; StaticTest() { x++; } No matter how many instances of StaticTest we have the ‘x’ variable is the same for all

Accessing Static Variables StaticTest st = new StaticTest(); st.x = 69; OR StaticTest.x = 69

More about Static Static methods cannot use non-static features of their class They can access the class’s static data Since static methods are not associated with an instance of a class there is no this variable

Static Initializers public class Demo { int x = 5; static { x = 69; } public static void main( String[] ) { System.out.println( ‘X = ‘ + x ); } What is the value printed?

Native modifier Can only refer to methods Indicates that the body of the method is to be found elsewhere Namely in a file in another language Call to native method is the same as if it was implemented in Java

Transient modifier Only applies to variables Transient variables will not be serialized Transient variables cannot be final or static

Synchronized modifier Used to control critical code in multi-threaded programs Covered in chapter 7

Volatile modifier Not on exam Used in multiprocessor environments Applies only to variables