Classes+and+Objects

Classes and Objects

Topics: 1. Objects - instantiation, encapsulation, info hiding

Instantiation
-The process of creating objects from a class is called instantiation. So an object is always an instance of a class which represents the blueprint. The object is constructed using the class and must be created before being used in a program.

instantiating an object is done in the following steps

1. first you declare a reference variable of the appropriate class which will store a reference to the object. which will simply be the name of the class Book myBook = new Book

where "Book" references a class for the object to mold

2. you then name the object you are creating

Book myBook = new Book "myBook" just name the object you are creating 3. you then use t he new operator which returns a reference to a new instance of the Book class.

Book myBook = new Book

The purpose of calling the constructor on the right side of the new operator is to initialize the newly created object.

2. Classes - Public, private, static 3. Methods - headers, constructors, accessors, mutators, static, overloading - **Constructor:** A method that is run when an object is instantiated, usually to initialize the objects instance variables; name always same as class; no return type. - **Accessor:** A method used to examine an attribute of an object without changing it; aka returns info with no changes. - **Mutator:** A method used to change the value of an attribute of an object; aka returns info with changes. - **Static:** static methods are for the whole class - **Overloading:** The process of using the same operator symbol or identifier to refer to many different methods.

4. Scope - this keyword 5. References - vs Primitive data types, null reference 6. Parameters - formal vs actual, passing primitive types as parameters, passing objects as parameters - **Formal parameter**s are introduced in a method definition but **actual parameters** are contained in a method call and passed to that method.