Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.

Similar presentations


Presentation on theme: "Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed."— Presentation transcript:

1 Chapter 10 Defining Classes

2 The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed and modified only by means of operations Class – software package or template that describes the characteristics of similar objects –These characteristics are of two sorts: Variable declarations that define and object’s data requirements Methods that define its behavior in response to messages Encapsulation – the combining of data and behavior into a single software package –An object is an instance of its class Instantiation – the process of creating a new object

3 Characteristics of an Object 1)An object has behavior as defined by the methods of its class 2)An object has state, which is another way of saying that at any particular moment its instance variables (explained later) have particular values 3)An object has its own unique identity, which distinguishes it from all other objects in the computer’s memory

4 Types of Variables 1)Primitive – contains a value -int, double, boolean, char, and the shorter and longer versions of these 2)Reference – contains a pointer to an object containing data stored elsewhere -all classes, for instance, String, reader, etc.. -Think of a reference variable as variable that stores an address to an object

5 The Structure of a Class Template All classes have a similar structure consisting of four parts: 1)The class’s name and some modifying phrases 2)A description of the instance variables 3)One or more methods that indicate how to initialize a new object (called constructor methods) 4)One or more methods that specify how an object responds to messages

6 Class Template public class extends { //Declaration of instance variables private ; …. //Code for the constructor method public { //initialize the instance variables …. } //Code for the other methods public ( ) {.... } //more methods }

7 Instance Variables Instance Variable – Storage for data in an instance of a class – Variable that can be used in any method (global variable for the class) – Instance variables are nearly always declared to be private. This prevents clients from referencing to the instance variables directly. Making instance variables private is an important aspect of information hiding

8 Clients, Servers, and Interfaces **When messages are sent, two objects are involved** client – sender of the message –Ex: You the user, another method, etc… server – receiver of the message –Ex: Object or method you are calling **A client’s interactions with a server are limited to sending it messages, so consequently a client needs to know nothing about the internal workings of a server** **A client needs to know only a server’s interface, or, the list of the methods supported by the server**

9 Visibility Modifiers public and private are examples of visibility modifiers – decides who is able to refer to them Methods are usually declared to be public, which allows clients to refer to them.

10 Constructor Methods The principal purpose of a constructor is to initialize the instance variables of a newly instantiated object. Constructors are activated when the keyword new is used and at no other time. A class template can include more than one constructor, provided each has a unique parameter list; however, all the constructors must have the same name, the name of the class. If a class template has no constructors, the JVM provides a default constructor behind the scenes. The constructor initialized numeric variables to zero and object variables to null.

11 Accessor Method Accesses a class object without altering the object. Returns some information about the object. Ex: method that returns a value or a method that prints

12 Mutator Method Changes the state of an object by modifying at least one of its instance variables Ex: Method that receives a value from a client and makes a change to an instance variable

13 Scope The scope of a variable or method is the region in which that variable or method is visible and can be accessed.

14 Static Methods vs. Instance Methods Instance Methods – operate on individual objects of a class –Ex: constructors, accessors and mutators Static Methods – perform an operation for the entire class, not its individual objects –Ex: when the client and server are the same class

15 Method Overloading When two or more methods in the same class have the same name but different parameter lists. Two methods can be named the same as long as the parameter lists are different. –Example: public int product(int number1, int number2) public int product(double number1, double number2) The return type of the method is irrelevant This allows a choice of ways to initialize objects of a class.

16 equals Method vs. == The equals method is used when we need to compare the content of the text present in the String objects. This method returns true when two String objects hold the same content (i.e. the same values). The == operator is used when we have to compare the String object references. If two String variables point to the same object in memory, the comparison returns true. Otherwise, the comparison returns false. Note that the ‘==’ operator does not compare the content of the text present in the String objects. It only compares the references the 2 Strings are pointing to.


Download ppt "Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed."

Similar presentations


Ads by Google