100 likes | 145 Views
Learn about access modifiers public and private in Java, their usage for data hiding and defining public interfaces. Explore the importance of return types and methods in Java programming with examples from GradeBook.java and GradeBookTest.java.
E N D
Department of Computer and Information Science,School of Science, IUPUI Introduction to Java- Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu
Access Modifiers public and private • private keyword • Used for most instance variables • private variables and methods are accessible only to methods of the class in which they are declared • Declaring instance variables private is known as data hiding • public keyword • Exposes variables or methods outside the Class. • Declaring public methods is know as defining the class’ public interface. • Return type • Indicates item returned by method • Declared in method header
Instance variable courseName set method for courseName get method for courseName Call get method Outline • GradeBook.java
Call get method for courseName Outline • GradeBookTest.java • (1 of 2)
Constructor to initialize courseName variable Outline • GradeBook.java • (1 of 2)
Outline • GradeBook.java • (2 of 2)
Call constructor to create first grade book object Create second grade book object Outline • GradeBookTest.java
Call constructor to create first grade book object Create second grade book object Outline • GradeBookTest.java
Acknowledgements • http://java.sun.com/docs/books/tutorial/getStarted/TOC.html • Pearson Education, Lewis and Loftus. • Deitel, Java How to Program • http://www.cs.wustl.edu/~plezbert/contcom/thesis/node6.html • http://www.cs.usfca.edu/~parrt/course/652/lectures-Spring-2004/language.impl.overview.pdf • http://ei.cs.vt.edu/~history/Youmans.Java.html