DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

1 Inheritance Classes and Subclasses Or Extending a Class.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
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.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Lecture 9 Concepts of Programming Languages
UML Basics & Access Modifier
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
Writing Classes (Chapter 4)
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
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.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
10-Nov-15 Java Object Oriented Programming What is it?
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,
Topics Instance variables, set and get methods Encapsulation
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
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.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Copyright © 2014 Pearson Education, Inc.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Andrew(amwallis) Classes!
Classes Object-oriented programming: Example: Bank transactions
University of Central Florida COP 3330 Object Oriented Programming
Anatomy of a class Part I
Lecture 9 Concepts of Programming Languages
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Your Own Classes Part 1
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Classes, Encapsulation, Methods and Constructors (Continued)
The Object-Oriented Thought Process Chapter 04
Programs and Classes A program is made up from classes
Object-Oriented Programming
Object Oriented Programming in java
Java Basics Data Types in Java.
Object Oriented Programming
Final and Abstract Classes
Unit-2 Objects and Classes
ITE “A” GROUP 2 ENCAPSULATION.
Encapsulation.
Anatomy of a class Part I
CSG2H3 Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Presentation transcript:

DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control

Usually the variables and methods of a class are visible everywhere in the program. However, in some situations, it may be necessary to restrict the access to certain variables and methods from outside the class. For example it may need to restrict the update of the balance field of a bank account object directly for other classes or programs. This can be achieved in Java by applying access modifiers. Java provides for types of access modifiers. 1. public. 2. default. 3. protected. 4. private.. 2

Syntax: for variables: ; E.g.: public int num; private double balance; for methods: ( ) { //body of the method. } E.g.: public double add(int a,int b) { return a+b; } 3

Public Access Any variable or method declared as public has the widest possible visibility and they are accessible everywhere. Note that the main method of a Java application is declared as public so that any class can access it without any restriction because it is the starting point of the program.. 4

Default Access For the default access of a variable or a method no access modifiers are used. This is also known as the friendly access. The difference between the public access and the friendly access is that the public modifier make the fields visible in all classes, regardless of their packages while the friendly access makes fields visible only in the same package, but not in other packages. (Note: package is a group of related classes stored separately). 5

Protected Access The visibility level of a “Protected” field lies in between the public access and default access. That is, the protected modifier makes the fields visible only to all classes and subclasses in the same package but also to subclasses in other packages. Note that, the non-subclasses in other packages cannot access the “protected” members 6

Private Access private fields have the highest degree of protection. They are accessible only within their own class. A method declared as private behaves like a method declared as final. This prevents the method from being sub classed. 7

summarizes the visibility 8

Instance and methods of class Usually the variables of a class are declared as private to provide the highest protection and the methods are declared as public so that they can be accessed by other classes. Eg:-in Student class 9 private String name; private int age; private double marks; private char grade; public void display()

Instance and methods of class Within driver class if you access marks directly i.e. st1.marks=78; This will produce the following compile time “marks has private access in Student” This is because the fields declared as private can not be accessed out side its class. Now to set the marks of a particular student a public method should be included into the Student class. Similarly if it is necessary to get the value of the marks field another public method should facilitate. 10

Setters and Getters Setters and Getters are two types of public methods which are used to set and get the value of an attribute from outside of its class. Naming Conventions: The name of the method starts with the word “set” or “get” followed by the name of the attribute that the method can manipulate. The Java method naming convention is also applied (if more than two words first letter of the first word is lowercase and first letter of other words are uppercase) 11

Setters and Getters… Ex: To set a value to the attribute name a setter method is defined as follows public void setName(String n) { this.name=n; } To get the value of the name attribute can use a getter method. public String getName() { return this.name; } 12