Download presentation
Presentation is loading. Please wait.
1
Encapsulation & Visibility Modifiers
2
Encapsulation We can take one of two views of an object:
internal - the variables the object holds and the methods that make the object useful external - the services that an object provides and how the object interacts From the external view, an object is an encapsulated entity, providing a set of specific services These services define the interface to the object An object is an abstraction, hiding details from the rest of the system Processing information by restricting its access is important in software development. Since classes are public, all the objects from the class are public. The way that we hide information or encapsulate data in Java is to use private access modifiers. It is expected on the AP test that all instance variables of a class are declared private. The only way to gain access to them is to use the accessor or mutator methods provided by the class.
3
Encapsulation An object should be self-governing
Any changes to the object's state (its variables) should be made only by that object's methods We should make it difficult, if not impossible, to access an object’s variables other than via its methods The user, or client, of an object can request its services, but it should not have to be aware of how those services are accomplished
4
Encapsulation An encapsulated object can be thought of as a black box
Its inner workings are hidden to the client, which invokes only the interface methods Methods Client In Java we create object encapsulation using visibility modifiers. Data
5
Visibility Modifiers In Java, we accomplish encapsulation through the appropriate use of visibility modifiers A modifier is a Java reserved word that specifies particular characteristics of a method or data value We've used the modifier final to define a constant We will study two visibility modifiers: public and private
6
Visibility Modifiers Members of a class that are declared with public visibility can be accessed from anywhere Public variables violate encapsulation Members of a class that are declared with private visibility can only be accessed from inside the class Members declared without a visibility modifier have default visibility and can be accessed by any class in the same package The words private and public are called visibility modifiers (or access level modifiers). Using the word private in an instance variable declaration ensures that other classes do not have access to the data stored in the variable without asking the object to provide it. Using the word public in the class declaration ensure that any programmer can make objects from the class. On the AP exam, always give instance variables private access visibility. This ensures that the data in these variables is hidden.
7
Visibility Modifiers Methods that provide the object's services are usually declared with public visibility so that they can be invoked by clients Public methods are also called service methods A method created simply to assist a service method is called a support method Since a support method is not intended to be called by a client, it should not be declared with public visibility
8
Visibility Modifiers public private Violate encapsulation Enforce
Variables Provide services to clients Support other methods in the class See page 191 Methods
9
Driver Programs A driver program drives the use of other, more interesting parts of a program Driver programs are often used to test other parts of the software The CountFlip class contains a main method that drives the use of the Coin class, exercising its services A class can’t do anything by itself. To be useful, objects are created in class that is outside of the object class. Each object that is created from a class is called an instance pf the class and the act of constructing an object is call instantiating the object A class that contains a main method is sometimes called a runner class, driver class, or tester class. Other times, they are called client programs. The main method is the point of entry for a program. That means that when the programmer runs the program, Java finds the main method first and executes it.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.