Presentation is loading. Please wait.

Presentation is loading. Please wait.

Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.

Similar presentations


Presentation on theme: "Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University."— Presentation transcript:

1

2 Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University

3 Evan Korth New York University Road Map Introduction to object oriented programming. Classes Encapsulation Members Objects Constructors Reading: –Liang 5: chapter 6: 6.1 – 6.4 –Liang 6: chapter 7: 7.1 – 7.4 –Liang 7: chapter 7: 7.1 – 7.4

4 Evan Korth New York University Object Oriented Programming Emphasis is placed on nouns or objects. Nouns (objects) have properties and behaviors. How do we build these objects? How do we represent their properties? How do we define their behaviors?

5 Evan Korth New York University Classes The main building blocks of Java programs. Defines objects of the same type. Like a blueprint.

6 Evan Korth New York University Classes (cont) Every.java file has one or more classes. Only one of the classes can be a public class. –That class must have the same name as the.java file. If the class has an method called main(), execution can begin in that class. (Therefore, you can test a class by adding a main method to it.) If there are other classes in the file, they cannot be public classes.

7 Evan Korth New York University Encapsulation Encapsulation refers to the process of combining elements to create a new entity. You encapsulate the properties (attributes) and behaviors (activities) of an entity into a class. Encapsulation also enables us to hide the implementation of a class to other classes (information hiding / abstraction).

8 Evan Korth New York University Designing Classes A class declaration includes members of the class. A member can be either a data member or a method member. A data member (AKA field) is used to define state (attributes or properties) of the entity. A method member is used to define the behaviors of the entity.

9 Evan Korth New York University Data members Data members can be a primitive type or a reference to another object*. –Primitive types are integer types, floating point types, characters and booleans. (Note: an int is not the same as an object of type Integer) The scope of a data member is the entire class, no matter where within the class it is declared. * More on object references in a moment

10 Evan Korth New York University Default values for data members 0 for all numeric type variables (including both floating point types and all integer types) \u0000 for char variables null for reference variables* false for boolean type variables Note: No default values for local variables (variables declared inside a method). * More on object references in a moment

11 Evan Korth New York University Objects An object is an instance of a class. If we think of a class as a blueprint, an object is one model created from that blueprint. You can create any number of objects from one class. An object is distinctly identified by an object reference (except for anonymous objects).

12 Evan Korth New York University Declaring object references In order to reference an object, we need an object reference variable. To declare an object reference variable we use the syntax: ClassName objectReferenceName; The above statement creates a variable objectReferenceName which can reference a ClassName object. It does NOT create an object.

13 Evan Korth New York University Instantiating objects In order to create an object, we use the new keyword along with a constructor* for the class of the object we wish to create. To refer to the object, we “point” an object reference variable to the new object. objectReferenceName = new Constructor(); The declaration and instantiation can be combined as follows: ClassName objectReferenceName = new ClassName(); –Note: the name of a constructor is the same as the name of the class * More on constructors soon

14 Evan Korth New York University Accessing Members of a Class Within a class you can access a member of the class the same way you would any other variable or method. Outside the class, a class member is accessed by using the syntax: –Referencing variables: objectReferenceName.varName –Calling methods (sending messages): objectReferenceName.methodName(params)

15 Evan Korth New York University Constructors Constructors are special methods that instantiate objects. A constructor is invoked with the new operator. A constructor should initialize the class variables. If the variables are not initialized, default values are used. A constructor does not have a return type. A constructor’s identifier (name) is the same as the class it constructs.

16 Evan Korth New York University Constructors continued Constructors can be overloaded but each one must have its own signature. A constructor with fewer arguments can call a constructor with more arguments (we will see how to do this soon). If no constructor is defined, a default constructor is automatically supplied which accepts no parameters. Variables are initialized to their default values. If one constructor is explicitly defined, the automatic default constructor is no longer available. In such case, if you want a no parameter constructor, you must define it yourself.


Download ppt "Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University."

Similar presentations


Ads by Google