Objects and Classes F Creating Objects and Object
Objects and Classes F Creating Objects and Object Reference Variables – Differences between primitive data type and object type Constructors F Modifiers (public, private and static) F Instance and Class Variables and Methods F Scope of Variables F Use this Keyword F
Class Concept
UML: Unified Modeling Language
Class Circle Declaration class Circle { double x 0 = 0. 0; double y 0 = 0. 0; double radius = 1. 0; double find. Area() { return radius*3. 14159; } }
Declaring Object Reference Variables Class. Name object. Name; Example: Circle my. Circle;
Creating Objects object. Name = new Class. Name(); Example: my. Circle = new Circle(); The object reference is assigned to the object reference variable.
Declaring/Creating Objects in a Single Step Class. Name object. Name = new Class. Name(); Example: Circle my. Circle = new Circle();
Test Class Circle public class Test. Circle { public static main(String args[]) { double area 1, area 2; Circle my. Circle 1 = new Circle(); Circle my. Circle 2 = new Circle(); area 1 = My. Circle 1. find. Area(); area 2 = My. Circle 2. find. Area(); System. out. println (”S 1=”+area 1); System. out. println (”S 2=”+area 2); } }
Differences between variables of primitive Data types and Object types
Object reference The object reference is a handle to the location where the object resides in memory. When a primitive value is passed into a method, a copy of the primitive is made. The copy is what is actually manipulated in the method. So, the value of the copy can be changed within the method, but the original value remains unchanged. When an object reference or array reference is passed into a method, a copy of the reference is actually manipulated by the method. So, the method can change the attributes of the object.
Accessing Objects F Referencing the object’s data: object. Name. data Example: r= my. Circle. radius; my. Circle. radius=10. 0; F Invoking the object’s method: object. Name. method() Example: S = my. Circle. find. Area();
Constructors Circle() { x 0 = 0. 0; y 0 = 0. 0; radius = 1. 0; } Constructors are a special kind of methods that are invoked to construct objects. Circle(double x, double y, double r) { x 0 = x; y 0 = y; radius = r; }
Constructors, cont. A constructor with no parameters is referred to as a default constructor. · Constructors must have the same name as the class itself. · Constructors do not have a return type—not even void. · Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.
Constructors and method for Class Circle public class Circle {. . . Circle() // Default constructor { x 0 = 0. 0; y 0 = 0. 0; radius = 1. 0; } Circle(double x, double y, double r) { x 0 = x; y 0 = y; radius = r; } find. Area() { return radius*3. 14159; } }
Default Constructor If a class does not define any constructor explicitly, a default constructor is defined implicitly. F F If a class defines constructor implicitly, a default constructor does not exist unless it is defined explicitly.
Using Constructors F Objective: Demonstrate the role of constructors and use them to create objects. my. Circle = new Circle(); my. Circle = new Circle(2, . 0, 3. 0, 5. 0);
Visibility Modifiers and Accessor Methods By default, the class, variable, or data can be accessed by any class in the same package (will be explained later ). F public The class, data, or method is visible to any class in any package. F private The data or methods can be accessed only by the declaring class.
Visibility modifiers and accessor methods public class Circle { private double x 0 = 0. 0; private double y 0 = 0. 0; private double radius = 1. 0; public double get. Radius() { return radius; } public void set. Radius(double new. Radius) { radius = new. Radius; } public void set. Radius(int new. Radius) { radius = new. Radius; } … }
Instance Variables and Methods Instance variables belong to a specific instance. Instance methods are invoked by an instance of the class.
Class Variables, Constants, and Methods Class variables are shared by all the instances of the class. Class methods are not tied to a specific object. Class constants are final variables shared by all the instances of the class.
Class Variables, Constants, and Methods, cont. To declare class variables, constants, and methods, use the static modifier. private static int number. Of. Objects=0; public final static double PI = 3. 1415926;
Class Variables, Constants, and Methods, example. public class Circle { private static int number. Of. Objects=0; public final static double PI = 3. 1415926; Circle(double x, double y, double r) { x 0=x; y 0=y; radius=r; number. Of. Objects++; } public static get. Number. Of. Objects() { return number. Of. Objects; } … }
Class Variables, Constants, and Methods, cont.
Math is class with private constructor F Class Math contains only public static methods: Math. sin() Math. asin () Math. ceil() Math. round() Math. abs() Math. min() Math. pow() Math. exp() Math. sqrt(). . .
Scope of Variables F The scope of instance and class variables is the entire class. They can be declared anywhere inside a class. F The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used.
The Keyword this F Use this to refer to the current object. F Use this to invoke other constructors of the object. public void set. Radius(double radius) { this. radius = radius; } public Circle() { this(0. 0, 1. 0); }
- Slides: 26