Objects - Page 1 of 10 A first look at Classes and Objects:...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Page 1 of 10 A first look at Classes and Objects: In Java classes are the single most important language features that facilitate object- oriented programming (OOP). A class is the blueprint for an object . From the class, one or more objects may be created. Before an object can be created, it must be carefully designed by a programmer. The programmer must determine the attributes and methods that are necessary, and then write a class, which is a collection of programming statements specifying the attributes and methods that a particular object has. Our text refers to attributes as fields , or instance variables . Together the fields and methods of a class are referred to as members of the class. A class is not an object, but description of an object. All of the objects that are created from the same class will have the attributes and methods described by the class. Once a class has been written, you can use the class to create as many objects of that class as needed. Each object is considered as an instance of the class. Therefore, an object is a value of the class (a class type) and differs from a value of a primitive type in that it has methods (actions), as well as attributes (data). The following demonstrate a variable of String type that references a String object. String person = “Jenny”; A String object person variable holds the address of a String object As discussed before OOP addresses the problem of code/data separation through encapsulation and data hiding Encapsulation refers to combining of data and code into a single object. In order to accomplish encapsulation, Java restricts access to the instance variables or fields of the class and does not allow it to be accessed outside of the class definition. Here, we will use the private access modifier when declaring instance variables of a class. When the private access specifier is applied to a class member, the member cannot be accessed by code outside the class. The class member can be accessed only by methods that are members of the same class. When the public access modifier is applied to a class member, the member can be accessed by code inside and outside the class definition. The public access modifier is used by class methods, indicating that the class instance methods can be accessed inside and outside of the class. In java, typical layout of a class and class members is as follows; public class ClassName { Filed declarations Method definitions “Jenny” address
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 2 of 10 } Constructors: W e often want to initialize the instance variables for an object when you create the object. A constructor is a special variety of method that is designed to perform such initialization. A constructor in Java must have the same name as the class. If you add no constructor definition to your class, Java automatically creates a constructor that takes no argument. The constructor that Java class is known as default constructor. However, the only time that Java provides a default constructor is when you do not write
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/05/2011 for the course CS 38 taught by Professor Stevehouseman during the Summer '09 term at Irvine Valley College.

Page1 / 10

Objects - Page 1 of 10 A first look at Classes and Objects:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online