Classes Introduce to classes and objects in Java. Classes and Objects in Java Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes. 1 2 Classes A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Circle centre radius circumference() area() Classes A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Bare bone class no fields, no methods // my circle class 3 4 Class Circle with fields Class Circle with methods Add fields public double x, y; // centre coordinate public double r; // radius of the circle Add methods to Circle class public double x, y; // centre of the circle public double r; // radius of circle The fields (data) are also called the instance varaibles. 5 public double circumference() { return 2*3.14*r; public double area() { return 3.14 * r * r; Method Body 6
Class of Circles Class of Circle cont. Defining the Circle class, have created a new data type Data Abstraction acircle, bcircle simply refers to a Circle object, not an object itself. Can declare variables of that type: acircle bcircle Circle acircle; Circle bcircle; Points to nothing (Null Reference) Points to nothing (Null Reference) 7 8 Creating objects of a class Objects are created dynamically using the new keyword. acircle and bcircle refer to Circle objects Creating objects of a class acircle = new Circle(); bcircle = new Circle() ; bcircle = acircle; acircle = new Circle() ; bcircle = new Circle() ; 9 10 Creating objects of a class Automatic garbage collection acircle = new Circle(); bcircle = new Circle() ; bcircle = acircle; Before Assignment acircle P bcircle Q Before Assignment acircle bcircle P Q Q The object does not have a reference and cannot be used in future. The object becomes a candidate for automatic garbage collection. Java automatically collects garbage periodically and releases the memory used to be used in the future. 11 12
Accessing Object/ Circle Data Executing Methods in Object/ Circle Similar to C syntax for accessing data defined in a structure. acircle.x = 2.0 // initialize center and radius acircle.y = 2.0 acircle.r = 1.0 Using Object Methods: double area; acircle.r = 1.0; area = acircle.area(); sent message to acircle 13 14 Constructors Constructors Take another look at Circle object creation: Parentheses? Calling a function? Yes, creating a new object automatically begins with an initialization function called the constructor. Constructors have the same name as the class. Constructor is a method that gets invoked at object creation time. Constructors have the same name as the class. Constructors cannot return values. Constructors are normally used for initializing objects. A class can have more than one constructor with different input arguments. 15 16 Defining a Constructor Like any other method public double x,y,r; // Constructor public Circle(double centrex, double centrey, double radius) { x = centrex; y = centrey; r = radius; Public double circumference() { return 2*3.14*r; Public double area() { return 3.14 * r * r; Constructors initialise Objects Using the default constructor acircle.x = 2.0 // initialize center and radius acircle.y = 2.0 acircle.r = 1.0 acircle = new Circle() ; At creation time the center and radius are not defined. These values are explicitly set later. 17 18
Constructors initialise Objects Multiple Constructors With defined constructor Circle acircle = new Circle(1.414,-1.0,.25); Sometimes want to initialize in a number of different ways, depending on circumstance. acircle = new Circle(1.414, -1.0, 0.25) ; acircle is created with center (1.414, -1.0) and radius 0.25 This can be supported by having multiple constructors having different input arguments. 19 20 Multiple Constructors Initializing with constructors public double x,y,r; //instance variables // Constructors public Circle(double centrex, double cenrey, double radius) { x = centrex; y = centrey; r = radius; public Circle(double radius) { x=0; y=0; r = radius; public Circle() { x=0; y=0; r=1.0; public class TestCircles { public static void main(string args[]){ Circle circlea = new Circle( 10, 12, 20); Circle circleb = new Circle(10); Circle circlec = new Circle(); Public double circumference() { return 2*3.14*r; Public double area() { return 3.14 * r * r; circlea = new Circle(10, 12, 20) circleb = new Circle(10) circlec = new Circle() 21 Centre = (10,12) Radius = 20 Centre = (0,0) Radius= 10 Centre = (0,0) Radius = 1 22 Method Overloading Polymorphism Constructors all have the same name? In Java, methods are distinguished by: name number of arguments type of position of arguments Not method overriding (coming up), method overloading: Allows a single object, method or operator associated with different meaning depending on the type of data passed to it. Defining the same method with different argument types (method overloading) - polymorphism. 23 24
Use of this keyword Data Hiding and Encapsulation this keyword can be used to refer to the object itself. public double x,y,r; // Constructor public Circle (double x, double y, double r) { this.x = x; this.y = y; this.r = r; Java provides control over the visibility of variables and methods, encapsulation, safely sealing data within the capsule of the class Prevents programmers from relying on details of class implementation, so you can update without worry Protects against accidental or willful stupidity Keeps code elegant and clean (easier to maintain) 25 26 Public, Private, Protected Public keyword applied to a class, makes it available/visible everywhere. Applied to a method or variable, completely visible. Private fields or methods for a class only visible within that class. Private members are not visible within subclasses, and are not inherited. Protected members of a class are visible within the class, subclasses and also within all classes that are in the same package as that class. private double x,y,r; Visibility // Constructor public Circle (double x, double y, double r) { this.x = x; this.y = y; this.r = r; public double circumference() { return 2*3.14*r; public double area() { return 3.14 * r * r; 27 28 Visibility Accessors Getters/ Setters Circle message area Construction time message Circle Center (x,y) Radius r circumference message private double x,y,r; public double getx() { return x; public double gety() { return y; public double getr() { return r; public double setx(double x) { this.x = x; public double sery(double y) { this.y = y; public double setr(double r) { this.r = r; 29 30
Class Variables Java doesn t allow for global variables Instead have class variables that apply to the class as a whole, and not to just a single instance Define a class variable by marking with the static keyword Define using static: Class Variables // class variable, one for the Circle class, how many circles public static int numcircles; //instance variables,one for each instance of a Circle public double x,y,r; // Constructors... Access with the class name: ncircles = Circle.numCircles; 31 32 Class Variables - Example Using static variables: // class variable, one for the Circle class, how many circles private static int numcircles = 0; private double x,y,r; // Constructors... Circle (double x, double y, double r){ this.x = x; this.y = y; this.r = r; numcircles++; Class Variables - Example Using static variables: public class CountCircles { public static void main(string args[]){ Circle circlea = new Circle( 10, 12, 20); // numcircles = 1 Circle circlea = new Circle( 5, 3, 10); // numcircles = 2 circlea = new Circle(10, 12, 20) circleb = new Circle(5, 3, 10) numcircles 33 34 Class vs Instance Variables Instance variables : One copy per object. Every object has its own instance variable. E.g. x, y, r (centre and radius in the circle) Class (static) variables : One copy per class. E.g. numcircles (total number of circle objects created) 35