Presentation is loading. Please wait.

Presentation is loading. Please wait.

Corresponds with Chapter 7

Similar presentations


Presentation on theme: "Corresponds with Chapter 7"— Presentation transcript:

1 Corresponds with Chapter 7
Classes and Objects Corresponds with Chapter 7

2 Principles of Object Orientation
Encapsulation Data and behavior pertaining to an object are bundled together into a single unit Information Hiding Provide an interface to the user of a class via public members Hide details about the implementation of the class via private members Inheritance Grouping classes into hierarchies of categories and subcategories Subclasses inherit the members of the base class Poymorphism Poly = many, Morph = form  Many Forms The same method call (using identical signature) produces different behaviors depending on the class

3 Classes and Instances (Objects)
Class = category Like a data type Declared using the class keyword Contains declarations of members Variables Methods Instance = object An actual piece of data (created on the heap) Member variables contain actual data Method calls apply to the particular instance

4 Example – The SimpleCircle Class (similar to Listing 7. 1, 7
Example – The SimpleCircle Class (similar to Listing 7.1, 7.2 pp ) This is the application class with main method This is the another class representing a specific kind of object.

5 Overloaded Constructor
Class Declaration Member variable declaration Default constructor (no parameters) Overloaded Constructor Method declaration Entire class declaration: bundles the data (member variables) with the behavior (methods)  encapsulation

6 Unified Modeling Language
Diagrams for representing object oriented systems. Static class diagram: Represents classes in the system Boxes for representing classes Top part of box contains name Middle part contains attributes (member variables) Lower part contains operations (methods, including constructors)

7 UML Class Diagram for SimpleCircle Class
Class name Attributes Operations Class Visio drawing of UML diagram

8 Declaring Objects Example: ClassName objectName;
SimpleCircle myCircle; Class is like data type Variable name indicates a reference to an instance of the class

9 Creating Objects Instantiation
objectName=new ClassName(); Example: myCircle=new SimpleCircle(); assignment operation puts the reference into the variable. new keyword creates the object on the heap (instantiation) Note the need for ()….instantiation automatically invokes a constructor method (more on this later) Specify the class of object being instantiated.

10 Declaring/Creating Objects in a Single Step
ClassName objectName = new ClassName(); Example: SimpleCircle myCircle = new SimpleCircle();

11 The SimpleCircle Class (cont.)
Instantiating an object: new keyword creates the object on the heap Overloaded constructor is invoked to initialize member variables

12 Constructors A constructor is a special method that is automatically called when an instance of the class is created. Constructor’s purpose – perform initialization operations (e.g. set initial values for variables). The constructor’s identifier is EXACTLY the same as the class’s name. Constructors have no return type (not even void). When the new operator creates an instance of a class, the constructor is automatically invoked. Default constructor – no parameters Can overload constructor, just like any other method.

13 Frame Stack Heap Declaring the reference variable Creating the object
myCircle Creating the object on the heap (instantiation) SimpleCircle object radius main’s frame args Frame Stack Heap

14 Constructor method is invoked
SimpleCircle constructor frame newRadius 5.0 this main’s frame SimpleCircle object radius args myCircle Frame Stack Heap

15 The this pointer An instance method is a method that is associated with a specific instance (NOT a static method). Constructors are instance methods. Every instance method has a special reference called this. this points to the instance for which the method was invoked. this is the way that an instance method knows which object to access when using or manipulating its data this is also a keyword in the Java language, and can be used to access members from within the instance (more on this later).

16 The constructor (an instance method) can access the member variables of the object via the this pointer. SimpleCircle constructor frame newRadius 5.0 this main’s frame SimpleCircle object radius args 5.0 myCircle Frame Stack Heap

17 After constructor terminates (and its frame popped from the frame stack), the new operator returns the address where the object was created. This is assigned into the variable myCircle. main’s frame SimpleCircle object radius args 5.0 myCircle Frame Stack Heap

18 Accessing Objects Referencing the object’s data: objectName.data
myCircle.radius Referencing the object’s method: objectName.method myCircle.findArea() The dot (.) is a member access operator. (Sometimes just called the “dot operator”)

19 The SimpleCircle Class (cont.)
Variable used outside the class. Accessed via dot operator. Member variable declaration. Available to multiple methods (scope is wider than a single method). Variable used inside the class. Note: radius is an instance variable. Every instance of the Circle class will have a variable called radius. Later we’ll compare this to static, or class variables.

20 Instance Methods objectName.methodName();
Instance methods belong to instances. These methods can only be applied after the instances are created. Later we’ll compare these to static, or class methods. Instance methods are called by the following: objectName.methodName(); The name of the reference variable The dot operator The name of the method.

21 The SimpleCircle Class (cont.)
The method call The method declaration

22 Calling the method Frame Stack Heap
NOTE: the instance method knows which object to access via its this pointer. This is identical to the reference variable through which the method was called. findArea’s frame this main’s frame SimpleCircle object radius args 5.0 myCircle Frame Stack Heap

23 The SimpleCircle Class (cont.)
Instantiating an second object: new keyword creates the object on the heap Default constructor is invoked to initialize member variables

24 Frame Stack Heap Creating the second object
Declaring the reference variable yourCircle Creating the second object on the heap (instantiation) SimpleCircle object radius main’s frame SimpleCircle object radius args 5.0 myCircle Frame Stack Heap

25 The constructor (this time the default constructor) can access the member variables of the object via the this pointer. SimpleCircle constructor frame SimpleCircle object radius 1.0 this main’s frame SimpleCircle object radius args 5.0 myCircle yourCircle Frame Stack Heap

26 After constructor terminates (and its frame popped from the frame stack), the new operator returns the address where the object was created. This is assigned into the variable yourCircle. SimpleCircle object radius 1.0 main’s frame SimpleCircle object radius args 5.0 myCircle yourCircle Frame Stack Heap

27 The SimpleCircle Class (cont.)
Accessing the member variable via the reference variable

28 We know which radius variable is being accessed because the reference variable points us to the appropriate object. SimpleCircle object radius 100.0 main’s frame SimpleCircle object radius args 5.0 myCircle yourCircle Frame Stack Heap

29 The SimpleCircle Class (cont.)
Invoking the method via the reference variable

30 The instance method’s this pointer will point at the same object as the reference variable through which the method was invoked. This is how the method knows which object it is associated with. findArea’s frame SimpleCircle object radius 100.0 this main’s frame SimpleCircle object radius args 5.0 myCircle yourCircle Frame Stack Heap

31 Principles of Object Orientation
Encapsulation Data and behavior pertaining to an object are bundled together into a single unit Information Hiding Provide an interface to the user of a class via public members Hide details about the implementation of the class via private members Inheritance Grouping classes into hierarchies of categories and subcategories Subclasses inherit the members of the base class Poymorphism Poly = many, Morph = form  Many Forms The same method call (using identical signature) produces different behaviors depending on the class

32 Visibility Modifiers for Members
public The class, data, or method is visible to any class in any package. private The data or methods can be accessed only by the declaring class. protected The data or methods can be accessed by the declaring class, its subclasses, and other classes in the same package. (more on this later) By default, a class’s members are protected

33 Public vs. Private Use public is you want everyone to be able to access a member Use private if you want to restrict access to a member: For example, you may want to make member variables private so that users of a class cannot arbitrarily set values For these private members, access would be done via public methods called “accessor methods” (for obtaining values) and “mutator methods” of changing values. Mutator methods can perform input verification to ensure that values being assigned to a member variable are valid.

34 Accessor and Mutator Methods
Purpose of accessor/mutator methods is to provide controlled access to private member variables Two types: get – accessor returns the value in a private member. Return type is same as data type of the member variable. set – mutator changes the contents of the private member. Takes a parameter of the same type as the private member. Often does some validation to ensure that entered the input parameter value is legitimate before placing it into the member variable. Typically returns a void.  Each private member variable that should be accessed from outside the class will need accessor and mutator methods associated with it.

35 Set mutator validates parameter value
The Circle Class (includes private member variable and accessor/mutator methods) The member variable is private Public constructors Using accessor and mutator to manipulate private member variable Public accessor and mutator Set mutator validates parameter value

36 Unified Modeling Language
Diagrams for representing object oriented systems. Static class diagram: In class box, visibility modifiers are represented with + for public - for private * for protected

37 UML Class Diagram for Circle Class
Attributes are private ( - ) Operations are public (+) Accessor/mutator methods as operations Visio drawing of UML diagram

38 Passing Objects to Methods
Object reference variables can be passed as arguments, just like other variables Since these are reference variables, the method receiving them has access to the original data (similar to when we passed arrays)

39 Passing a Circle Object (similar to Listing 7.9 p249)
Reference to an instance Reference as an actual parameter The corresponding formal parameter


Download ppt "Corresponds with Chapter 7"

Similar presentations


Ads by Google