Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(Java) public, private, protected (default)

Slides:



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

Final and Abstract Classes
Introduction to Java 2 Programming
1.00 Lecture 11 Scope and Access. Variable Lifecycles Instance (or object) variables – Created when their containing object is created – Initialized to.
Lecture 5: Interfaces.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Object Oriented Programming
INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
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.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
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.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
Java Implementation: Part 3 Software Construction Lecture 8.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Programming in Java CSCI-2220 Object Oriented Programming.
C# G 1 CSC 298 Object Oriented Programming Part 2.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces and Inner Classes
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
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.
Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Topic: Classes and Objects
OOP: Encapsulation &Abstraction
Inheritance ITI1121 Nour El Kadri.
Objects as a programming concept
Final and Abstract Classes
Inheritance and Polymorphism
Chapter 5 Classes.
CS240: Advanced Programming Concepts
Interface.
Java Programming Language
Conditional Statements
Interfaces.
CSE 1030: Implementing GUI Mark Shtern.
Inheritance Inheritance is a fundamental Object Oriented concept
Tonga Institute of Higher Education
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CIS 199 Final Review.
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
CSG2H3 Object Oriented Programming
Presentation transcript:

Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(Java) public, private, protected (default)

Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(Java) Specifier class subclass package world private X protected X X public X X X X package X X

Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(Java) Make the member public. Then everybody,everywhere, can access it Private :you cans touch that. The private keyword that means no one can access that member except that particular class. Other classes in the same package cannot access private member Package/Friendly:Same Package classes can touch that protected:Cross package inheritance

Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(C#) private, protected, and public These three modifiers affect the accessibility of methods, fields and attributes. The private modifier ensures that the method, field or attribute is visible only within the class it is defined. The protected modifier extends this visibility to derived classes, and the public modifier declares the method, field or attribute to be visible to everyone.

Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(C#) Internal and protected internal Internal: The members in class A that are marked internal are accessible to methods of class A assembly. Protected internal: The members in class A that are marked protected internal are accessible to methods of class A, to methods of classes derived from class A, and also to any class in As assembly. This is effectively protected OR internal.( There is no concept of protected AND internal).

Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(C#) extern The extern keyword is basically used just for non-C# function calls, which also requires: using System.Runtime.InteropServices; and is used in conjunction with the DllImport attribute, so that your C# program can call functions in other DLLs. This is a very useful capability of the C# language, because there are cases where you need direct access to the underlying Windows API. Example: [DllImport("User32.dll")] public static extern int MessageBox(int h, string m, string c, int type); MessageBox(0, "Test", "My Message Box", 0);

Written by Paul Pu All Right Reservedwww.torontocollege.com Work with the final keyword(JAVA) The final keyword has slightly different meanings depending on the context, but in general it says This cannot be changed. The final can be used: for data, methods and for a class

Written by Paul Pu All Right Reservedwww.torontocollege.com Work with the seal keyword(C#) sealed The sealed modifier ensures that a class cannot be inherited. Therefore, sealed classes cannot be abstract, nor can they have abstract or virtual methods.

Written by Paul Pu All Right Reservedwww.torontocollege.com Important notes to access a class Variable:An item of data named by an identifier. Each variable has a type, such as int or Object, and a scope. See also class variable, instance variable, local variable. class variableinstance variablelocal variable

Written by Paul Pu All Right Reservedwww.torontocollege.com Important notes to access a class instance variable Any item of data that is associated with a particular object. Each instance of a class has its own copy of the instance variables defined in the class. Also called a field.field

Written by Paul Pu All Right Reservedwww.torontocollege.com Important notes to access a class Static key word: Class method Class variable

Written by Paul Pu All Right Reservedwww.torontocollege.com Important notes to access a class class variable A data item associated with a particular class as a whole--not with particular instances of the class. Class variables are defined in class definitions. Also called a static field.static field

Written by Paul Pu All Right Reservedwww.torontocollege.com Important notes to access a class method A function defined in a class. There are instance method, class method. Unless specified otherwise, a method is not static.instance method class method instance method Any method that is invoked with respect to an instance of a class.

Written by Paul Pu All Right Reservedwww.torontocollege.com Important notes to access a class class method A method that is invoked without reference to a particular object. Class methods affect the class as a whole, not a particular instance of the class. Also called a static method.static method

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members When you declare a member variable such as aFloat in MyClass : class Test { int i=47; } you declare an instance variable.

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members class StaticTest { static int i=47; }

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members //Java Code public class TestDriver { public static void main(String[] args) { Test ts1=new Test(); Test ts2=new Test(); ts1.i=ts1.i+1; System.out.println("object ts1 i ="+ts1.i); System.out.println("object ts2 i="+ts2.i); }

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members What is the output? Why?

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members //Java Code public class StDriver { public static void main(String[] args) { StaticTest.i=StaticTest.i+1; StaticTest ts1=new StaticTest(); StaticTest ts2=new StaticTest(); ts1.i=ts1.i+1; System.out.println("object ts i ="+StaticTest.i); System.out.println("object ts1 i ="+ts1.i); System.out.println("object ts2 i="+ts2.i); }

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members Test ts1=new Test(); Test ts2=new Test(); 47ts1.i ts2.i 47 ts1.i=ts1.i+1; 48

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members StaticTest.i=StaticTest+1; StaticTest ts1=new StaticTest(); StaticTest ts2=new StaticTest(); StaticTest.i ts1 ts

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members Methods are similar: Your classes can have instance methods and class methods. Instance methods operate on the current object's instance variables but also have access to the class variables. Class methods, on the other hand, cannot access the instance variables declared within the class (unless they create a new object and access them through the object). Also, class methods can be invoked on the class, you don't need an instance to call a class method.

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members By default, unless otherwise specified, a member declared within a class is an instance member. The class defined below has one instance variable--an integer named x --and two instance methods-- x and setX --that let other objects set and query the value of x :

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members class AnInteger { int x; public int x() { return x; } public void setX(int newX) { x = newX; }

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members... AnInteger myX = new AnIntegerX(); AnInteger anotherX = new AnIntegerX(); myX.setX(1); anotherX.x = 2; System.out.println("myX.x = " + myX.x()); System.out.println("anotherX.x = " + anotherX.x());...

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members What is the output?

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members To specify that a member variable is a class variable, use the static keyword. class AnInX { static int x; public int x() { return x; } public void setX(int newX) { x = newX; }

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members To specify that a member variable is a class variable, use the static keyword. class AnInX { static int x; public int x() { return x; } public void setX(int newX) { x = newX; }

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members... AnInX myX = new AnInX(); AnInX anotherX = new AnInX(); myX.setX(1); anotherX.x = 2; System.out.println("myX.x = " + myX.x()); System.out.println("anotherX.x = " + anotherX.x());...

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members class AnIntegerNamedX { static int x; public int x() { return x; } public void setX(int newX) { x = newX; } // Does this program correct?

Written by Paul Pu All Right Reservedwww.torontocollege.com Understanding Instance and Class Members //Fix it class AnIntegerNamedX { static int x; static public int x() { return x; } static public void setX(int newX) { x = newX; }

Written by Paul Pu All Right Reservedwww.torontocollege.com SingletonPattern //: SingletonPattern.java // The Singleton design pattern: you can // never instantiate more than one. // Since this isn't inherited from a Cloneable // base class and cloneability isn't added, // making it final prevents cloneability from // being added in any derived classes:

Written by Paul Pu All Right Reservedwww.torontocollege.com Singleton Pattern final class Singleton { private static Singleton s = new Singleton(47); private int i; private Singleton(int x) { i = x; } public static Singleton getHandle() { return s; } public int getValue() { return i; } public void setValue(int x) { i = x; } }