Download presentation
Presentation is loading. Please wait.
1
Classes and Objects Introduced
Java Programming Classes and Objects Introduced
2
© Matt B. Pedersen (matt@cs.unlv.edu)
Introduction A class is a named collection of Fields, that hold data values. Methods, that operate on the fields. Classes are the most important reference type (4 others exist) Classes define a new datatype CSC 140 Java Programming © Matt B. Pedersen
3
© Matt B. Pedersen (matt@cs.unlv.edu)
Simple Class Example Example: class Point { double x; // x coordinate double y; // y coordinate } Defines a 2 dimensional point in Cartesian coordinates. (No methods yet) CSC 140 Java Programming © Matt B. Pedersen
4
Data types versus data values
It is important to distinguish between data types and data values: int is a data type, 42 is a data value of type int. char is a data type, is a character value (of char type). CSC 140 Java Programming © Matt B. Pedersen
5
If classes are data types ….
A class is a data type, and data types define a range of values. So what are the values of class type? Objects An object is an instance of a class. E.g., where Point is a class representing all possible points a Point object represents a specific point in 2 dims. CSC 140 Java Programming © Matt B. Pedersen
6
© Matt B. Pedersen (matt@cs.unlv.edu)
Just like blueprints Classes are like blueprints of a ‘building’. You cannot live in a ‘building’ blueprint. You can use the blueprint to make a specific ‘building’ (or instantiate a ‘building’) Every ‘building’ instantiated is its own new ‘building’ and is physically not the same as the other ‘buildings’ ‘not the same’ does not mean they do not look the same, they are separate buildings. CSC 140 Java Programming © Matt B. Pedersen
7
© Matt B. Pedersen (matt@cs.unlv.edu)
A Bigger Example public class Point { public double x, y; public Point (double x, double y) { this.x = x; this.y = y; } public double distanceFromOrigin() { return Math.sqrt (x*x + y*y); CSC 140 Java Programming © Matt B. Pedersen
8
© Matt B. Pedersen (matt@cs.unlv.edu)
A Bigger Example public class Point { public double x, y; public Point (double x, double y) { this.x = x; this.y = y; } public double distanceFromOrigin() { return Math.sqrt (x*x + y*y); Name of the class is Point CSC 140 Java Programming © Matt B. Pedersen
9
© Matt B. Pedersen (matt@cs.unlv.edu)
A Bigger Example public class Point { public double x, y; public Point (double x, double y) { this.x = x; this.y = y; } public double distanceFromOrigin() { return Math.sqrt (x*x + y*y); It has 2 fields named x and y, both of double type. They are public, i.e., directly available to anyone holding a reference to an object of type Point. CSC 140 Java Programming © Matt B. Pedersen
10
© Matt B. Pedersen (matt@cs.unlv.edu)
A Bigger Example public class Point { public double x, y; public Point (double x, double y) { this.x = x; this.y = y; } public double distanceFromOrigin() { return Math.sqrt (x*x + y*y); It has a constructor that takes in 2 parameters (also called x and y) CSC 140 Java Programming © Matt B. Pedersen
11
© Matt B. Pedersen (matt@cs.unlv.edu)
A Bigger Example public class Point { public double x, y; public Point (double x, double y) { this.x = x; this.y = y; } public double distanceFromOrigin() { return Math.sqrt (x*x + y*y); this.x refers to the field x, where as x (on the right hand side of the assignment) is the x from the parameter list). this is a reference to the object being instantiated (almost true ;-), but good enough for now). We only need this because the field and the parameter is called the same. CSC 140 Java Programming © Matt B. Pedersen
12
© Matt B. Pedersen (matt@cs.unlv.edu)
A Bigger Example public class Point { public double x, y; public Point (double x, double y) { this.x = x; this.y = y; } public double distanceFromOrigin() { return Math.sqrt (x*x + y*y); A method called distanceFromOrigin, which takes in no parameters and returns _____ √x2+y2 Math.sqrt is the square root function. CSC 140 Java Programming © Matt B. Pedersen
13
What should I call my file?
All classes with the modifier public must be in separate files that are named the same as the class. For the example, it must be saved in a file called Point.java When compiled it generates Point.class CSC 140 Java Programming © Matt B. Pedersen
14
© Matt B. Pedersen (matt@cs.unlv.edu)
Creating Objects Recall a declaration of an integer variable: int myInt; What value does myInt hold? None, it must be assigned: myInt = 42; We can declare variable of class type in the same way: Point p; but p do not hold a value until one has been assigned to it. CSC 140 Java Programming © Matt B. Pedersen
15
© Matt B. Pedersen (matt@cs.unlv.edu)
Creating Objects How do we get an object value? By instantiating a class! Use the new keyword. Point p; p = new Point (3.14, 2.72); CSC 140 Java Programming © Matt B. Pedersen
16
Instantiating an Object
A number of important things happen when instantiating an object using new. An actual object is created based on the class being instantiated The object has the fields that the class lists. The constructor is executed with the parameters passed to it (if any). An object reference value is returned. CSC 140 Java Programming © Matt B. Pedersen
17
Types of Fields and Methods
There are 4 types of fields and methods: Class Fields Class Methods Instance Fields Instance Methods CSC 140 Java Programming © Matt B. Pedersen
18
© Matt B. Pedersen (matt@cs.unlv.edu)
Instance Fields An instance field is a field associated with an object: public class Point { int x; int y; Every instance of Point has its own set of fields x and y. CSC 140 Java Programming © Matt B. Pedersen
19
Class Fields (or static fields)
Where instance of a class (I.e., object) has instances of instance fields, a class field is shared between instances: public class Point { static int counter = 0; … } We use the word static to denote this. CSC 140 Java Programming © Matt B. Pedersen
20
This class field lives here Public class Point { int x, y;
static int count = 0; Point (int x, int y) { this.x = x; this.y = y; count += 1; } } A CLASS This class field lives here CSC 140 Java Programming © Matt B. Pedersen
21
© Matt B. Pedersen (matt@cs.unlv.edu)
x = 4 y = 3 new Point(4,3); Public class Point { int x, y; static int count = 1; Point (int x, int y) { this.x = x; this.y = y; count += 1; } } A CLASS AN OBJECT this.x = x initializes the instance field x to the value of the parameter x (4) this.y = y initializes the instance field y to the value of the parameter y (3) count += 1 increments the class field by one. CSC 140 Java Programming © Matt B. Pedersen
22
© Matt B. Pedersen (matt@cs.unlv.edu)
x = 4 y = 3 new Point(4,3); Public class Point { int x, y; static int count = 2; Point (int x, int y) { this.x = x; this.y = y; count += 1; } } A CLASS AN OBJECT x = 10 y = 98 new Point(10,98); ANOTHER OBJECT CSC 140 Java Programming © Matt B. Pedersen
23
© Matt B. Pedersen (matt@cs.unlv.edu)
Static fields A static field can be initialized at declaration time: static int count = 0; It should never be initialized in a constructor: Point (int x, int y) { … count = 0; } CSC 140 Java Programming © Matt B. Pedersen
24
© Matt B. Pedersen (matt@cs.unlv.edu)
Static Fields Within the class in which a static field is declared, it may be referenced by its name: public class Point { static int counter = 0; int getCouner() { return counter; } } CSC 140 Java Programming © Matt B. Pedersen
25
© Matt B. Pedersen (matt@cs.unlv.edu)
Field Modifiers The static modifier makes fields into class fields. Other important modifiers: public, private, protected Determines if a field can be seen/used outside of the class in which it is defined. final Once initialized it can never be written to again. transient, volatile Don’t worry about these. CSC 140 Java Programming © Matt B. Pedersen
26
© Matt B. Pedersen (matt@cs.unlv.edu)
Public Static Fields Public static fields like counter: public class Point { public static int counter = 0 … } may be referenced like this: Inside the class: counter (by its name) Outside the class: Point.counter (by <class>.<fieldname>) p.counter, where p is an instance of Point (<object>.<fieldname>) (This one is not advisable) CSC 140 Java Programming © Matt B. Pedersen
27
© Matt B. Pedersen (matt@cs.unlv.edu)
Private Static Field The counter might be private: public class Point { private static int counter = 0 … } Now, Point.counter is no longer legal, neither is p.counter. Only within Point can counter be accessed. CSC 140 Java Programming © Matt B. Pedersen
28
© Matt B. Pedersen (matt@cs.unlv.edu)
Example public class Point { public int x; public int y; private static int counter = 0; Point(int x, int y) { this.x = x; this.y = y; counter = counter + 1; } public static int getInstanceCount() { return counter; CSC 140 Java Programming © Matt B. Pedersen
29
© Matt B. Pedersen (matt@cs.unlv.edu)
Example public class Point { public int x; public int y; private static int counter = 0; Point(int x, int y) { this.x = x; this.y = y; counter = counter + 1; } public static int getInstanceCount() { return counter; Instance Fields Each object of class Point instantiated by new Point(..,..) as a copy of int x and int y. CSC 140 Java Programming © Matt B. Pedersen
30
© Matt B. Pedersen (matt@cs.unlv.edu)
Example public class Point { public int x; public int y; private static int counter = 0; Point(int x, int y) { this.x = x; this.y = y; counter = counter + 1; } public static int getInstanceCount() { return counter; Class Field Each instance of Point shares the counter field. Class fields or static fields are shared between instances. CSC 140 Java Programming © Matt B. Pedersen
31
© Matt B. Pedersen (matt@cs.unlv.edu)
Example public class Point { public int x; public int y; private static int counter = 0; Point(int x, int y) { this.x = x; this.y = y; counter = counter + 1; } public static int getInstanceCount() { return counter; Constructor A constructor is invoked as the first thing that happens at the new Point(…,…) call. It is used to set up fields in the object. new Point(…,…) does not return a reference to the new object until the constructor has run. CSC 140 Java Programming © Matt B. Pedersen
32
© Matt B. Pedersen (matt@cs.unlv.edu)
Example public class Point { public int x; public int y; private static int counter = 0; Point(int x, int y) { this.x = x; this.y = y; counter = counter + 1; } public static int getInstanceCount() { return counter; this is a special reference value that always referes to the object that we are currently operating inside. Here we use it to get to the fields as they are overshadowed by the parameters. Object Context CSC 140 Java Programming © Matt B. Pedersen
33
© Matt B. Pedersen (matt@cs.unlv.edu)
Example public class Point { public int x; public int y; private static int counter = 0; Point(int x, int y) { this.x = x; this.y = y; counter = counter + 1; } public static int getInstanceCount() { return counter; Since counter is private in Point, we cannot access it through Point.counter or p.counter for any object p of class Point, so we need a way to get to the value of counter. This is called an accessor method.. Accessor Method CSC 140 Java Programming © Matt B. Pedersen
34
© Matt B. Pedersen (matt@cs.unlv.edu)
Example public class Point { public int x; public int y; private int counter = 0; Point(int x, int y) { this.x = x; this.y = y; counter = counter + 1; } public static int getInstanceCount() { return counter; Point p1 = new Point(1,2); Point p2 = new Point(3,4); … System.out.println( “# of times Point was instantiated:” + Point.getInstanceCount()); CSC 140 Java Programming © Matt B. Pedersen
35
© Matt B. Pedersen (matt@cs.unlv.edu)
Example public class Point { public int x; public int y; private static int counter = 0; Point(int x, int y) { this.x = x; this.y = y; counter = counter + 1; } public static int getInstanceCount() { return counter; public String toString() { return “(“+x+“,”+y+“)”; Point p1 = new Point(1,2); Point p2 = new Point(3,4); System.out.println( “# of times Point was instantiated: ” + Point.getInstanceCount()); System.out.println(p1); System.out.println(p2); Would produce: # of times Point was instantiated: 2 (1,2) (3,4) Special method called toString; takes no arguments and returns a String. Automatically called when an object is used in a print statement. CSC 140 Java Programming © Matt B. Pedersen
36
© Matt B. Pedersen (matt@cs.unlv.edu)
Example public class Point { private int x; private int y; private static int counter = 0; Point(int x, int y) { this.x = x; this.y = y; counter = counter + 1; } public static int getInstanceCount() { return counter; String toString() { return “(“+x+“,”+y+“)”; int getX() { return x; // or this.x } int getY() { return y; // or this.y void setX(int x) { this.x = x; void setY(int y) { this.y = y; Might be nice to make x and y private as well, but then we need accessors (also called ‘getters’) for them, and if we want to change them we need mutators (also called ‘setters’) as well. CSC 140 Java Programming © Matt B. Pedersen
37
© Matt B. Pedersen (matt@cs.unlv.edu)
Methods Like fields, we can have Instance methods (class methods) Again, we use the static keyword to denote this. Object methods (regular methods) CSC 140 Java Programming © Matt B. Pedersen
38
© Matt B. Pedersen (matt@cs.unlv.edu)
Non-static methods A regular method (non-static) can be thought of as ‘belonging to the object’ It may reference both static and non-static fields. Can be public, or private, and final A final method cannot be re-implemented in subclasses. CSC 140 Java Programming © Matt B. Pedersen
39
© Matt B. Pedersen (matt@cs.unlv.edu)
Example public class Circle { // a class field public static final double π = ; // an instance field public double r; // the radius of the circle public Circle(double radius) { r = radius; } public double area() { return π * r * r; public double circumference () { return 2 * π * r; CSC 140 Java Programming © Matt B. Pedersen
40
© Matt B. Pedersen (matt@cs.unlv.edu)
Example public class Circle { // a class field public static final double π = ; // an instance field public double r; // the radius of the circle public Circle(double radius) { r = radius; } public double area() { return π * r * r; public double circumference () { return 2 * π * r; Can I add this method: public static getRadius() { return r; } CSC 140 Java Programming © Matt B. Pedersen
41
© Matt B. Pedersen (matt@cs.unlv.edu)
class class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } } // Create a circle with radius 5 Circle c1 = new Circle(5); CSC 140 Java Programming © Matt B. Pedersen
42
© Matt B. Pedersen (matt@cs.unlv.edu)
class class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } } // Create a circle with radius 5 Circle c1 = new Circle(5); c1 is a reference to an object of class Circle. A new Circle object is created with the new keyword, and the value 5 is passed to the constructor. CSC 140 Java Programming © Matt B. Pedersen
43
© Matt B. Pedersen (matt@cs.unlv.edu)
class object r = 5 class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } } // Create a circle with radius 5 Circle c1 = new Circle(5); C1 is a reference to an object of class Circle. A new Circle object is created with the new keyword, and the value 5 is passed to the constructor. CSC 140 Java Programming © Matt B. Pedersen
44
© Matt B. Pedersen (matt@cs.unlv.edu)
class object r = 5 class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } } r = 100 object // Create a circle with radius 5 Circle c1 = new Circle(5); // Create a circle with radius 20 Circle c2 = new Circle(20); CSC 140 Java Programming © Matt B. Pedersen
45
© Matt B. Pedersen (matt@cs.unlv.edu)
class object r = 5 class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } } r = 100 object // Create a circle with radius 5 Circle c1 = new Circle(5); // Create a circle with radius 20 Circle c2 = new Circle(20); CSC 140 Java Programming © Matt B. Pedersen
46
© Matt B. Pedersen (matt@cs.unlv.edu)
class object r = 5 class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } } // Create a circle with radius 5 Circle c1 = new Circle(5); // Create a circle with radius 20 Circle c2 = new Circle(20); r = 100 object System.out.println(c1.area()); double cir = c2.circumference(); double pi = Circle.π; CSC 140 Java Programming © Matt B. Pedersen
47
© Matt B. Pedersen (matt@cs.unlv.edu)
class class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } } object r = 5 public double area() { return π * r * r; } c1.area(): Value of r from object CSC 140 Java Programming © Matt B. Pedersen
48
© Matt B. Pedersen (matt@cs.unlv.edu)
class class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } } r = 100 object c2.circumference(): public double circumference () { return 2 * π * r; } CSC 140 Java Programming © Matt B. Pedersen
49
© Matt B. Pedersen (matt@cs.unlv.edu)
class class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } } double pi = Circle.π; CSC 140 Java Programming © Matt B. Pedersen
50
© Matt B. Pedersen (matt@cs.unlv.edu)
class class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } public static radToDeg(double rads) { return rads * 180 / π; } Let us add a class method (a static method) radsToDeg cannot reference ANY non-static fields!! CSC 140 Java Programming © Matt B. Pedersen
51
© Matt B. Pedersen (matt@cs.unlv.edu)
class class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } public static radToDeg(double rads) { return rads * 180 / π; } radsToDeg cannot reference ANY non-static fields!! Circle.radToDeg(35); There is no object context for radToDeg to access non-static fields in. CSC 140 Java Programming © Matt B. Pedersen
52
© Matt B. Pedersen (matt@cs.unlv.edu)
class object r = 5 class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } public static radToDeg(double rads) { return rads * 180 / π; } r = 100 object // Create a circle with radius 5 Circle c1 = new Circle(5); // Create a circle with radius 20 Circle c2 = new Circle(20); How do I get the radius of c1 or c2? CSC 140 Java Programming © Matt B. Pedersen
53
© Matt B. Pedersen (matt@cs.unlv.edu)
class object r = 5 class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } public static radToDeg(double rads) { return rads * 180 / π; } r = 100 object // Create a circle with radius 5 Circle c1 = new Circle(5); // Create a circle with radius 20 Circle c2 = new Circle(20); raidus in Circle is public, so it can be accessed Like this: c1.r (= 5) or c2.r (=100) but NOT Circle.r CSC 140 Java Programming © Matt B. Pedersen
54
© Matt B. Pedersen (matt@cs.unlv.edu)
class object r = 5 class Circle { public static final π = ; public double r; public double area() { … } public double circumference() { … } public static radToDeg(double rads) { return rads * 180 / π; } BECAUSE: r lives in an object; it is not static, and only static fields live in classes and can be accessed through class.field r = 100 object // Create a circle with radius 5 Circle c1 = new Circle(5); // Create a circle with radius 20 Circle c2 = new Circle(20); raidus in Circle is public, so it can be accessed Like this: c1.r (= 5) or c2.r (=100) but NOT Circle.r CSC 140 Java Programming © Matt B. Pedersen
55
© Matt B. Pedersen (matt@cs.unlv.edu)
Non-static methods can reference Non-static fields Non-static methods Static fields Static methods Static methods can reference CSC 140 Java Programming © Matt B. Pedersen
56
© Matt B. Pedersen (matt@cs.unlv.edu)
The this keyword A special keyword ‘this’ can be referenced inside objects. It refers to the current context, that is, the object in which the code is located (NOT class, but object) CSC 140 Java Programming © Matt B. Pedersen
57
© Matt B. Pedersen (matt@cs.unlv.edu)
The this keyword public class Circle { public static final double π = ; private double radius; public Circle(double radius) { // set the radius field equal to the radius parameter } public double getRadius() { return radius; } public double area() { return π * radius * radius; } public double circumference() return 2 * π * radius; } public static double radToDeg(double rads) { return rads * 180 / π; CSC 140 Java Programming © Matt B. Pedersen
58
© Matt B. Pedersen (matt@cs.unlv.edu)
The this keyword public class Circle { public static final double π = ; private double radius; public Circle(double radius) { // set the radius field equal to the radius parameter } public double getRadius() { return radius; } public double area() { return π * radius * radius; } public double circumference() return 2 * π * radius; } public static double radToDeg(double rads) { return rads * 180 / π; Problem: both the field and the parameter is called radius. CSC 140 Java Programming © Matt B. Pedersen
59
© Matt B. Pedersen (matt@cs.unlv.edu)
The this keyword public class Circle { public static final double π = ; private double radius; public Circle(double radius) { // set the radius field equal to the radius parameter } public double getRadius() { return radius; } public double area() { return π * radius * radius; } public double circumference() return 2 * π * radius; } public static double radToDeg(double rads) { return rads * 180 / π; Problem: both the field and the parameter is called radius. Solution: The field can be referenced like any other public field by: objectRef.fieldName But we don’t have an object reference do we? Yes we do, it is called ‘this’ CSC 140 Java Programming © Matt B. Pedersen
60
© Matt B. Pedersen (matt@cs.unlv.edu)
The this keyword public class Circle { public static final double π = ; private double radius; public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; } public double area() { return π * radius * radius; } public double circumference() return 2 * π * radius; } public static double radToDeg(double rads) { return rads * 180 / π; Problem: both the field and the parameter is called radius. Solution: The field can be referenced like any other public field by: objectRef.fieldName But we don’t have an object reference do we? Yes we do, it is called ‘this’ CSC 140 Java Programming © Matt B. Pedersen
61
© Matt B. Pedersen (matt@cs.unlv.edu)
Copying an Object Consider this code: Circle c1 = new Circle(20); Circle c2 = c1; c1 and c2 are references to the same object! There is no built-in way to clone an object, so we need to make one: public Circle clone() { return new Circle(radius); } CSC 140 Java Programming © Matt B. Pedersen
62
Multiple Constructors
With the Circle class we must provide an initial radius when instantiating the class: Circle c1 = new Circle(4); What about Circle c2 = new Circle(); That is illegal, there is no constructor with no arguments. CSC 140 Java Programming © Matt B. Pedersen
63
Multiple Constructors
Lets make one then, but what value should radius have? public Circle() { this.radius = 1; // we pick 1 } This does the same as calling the other constructor with the value 1! Public Circle() { this(1); } This is called an explicit constructor invocation. (Must appear on line 1) CSC 140 Java Programming © Matt B. Pedersen
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.