Classes ad Objects jvo@ualg.pt José Valete de Oliveira 4-1 Agai: Distace betwee poits withi the first quadrat Sample iput Sample output 1 1 3 4 2 jvo@ualg.pt José Valete de Oliveira 4-2 1
The simplest class implemetatio /** @versio 0.0 */ // Bad code! WHY? class Poit { it x, y; double dist(poit p) { jvo@ualg.pt José Valete de Oliveira 3-3 Cotrol of visibility /** @versio 1.0 */ class Poit { private it x, y; public double dist(poit p) { Three keywords: - public - private - protected jvo@ualg.pt José Valete de Oliveira 3-4 2
Sitaxe jvo@ualg.pt José Valete de Oliveira 3-5 Sitaxe jvo@ualg.pt José Valete de Oliveira 3-6 3
Sitaxe for attributes jvo@ualg.pt José Valete de Oliveira 3-7 Sitaxe for attributes, illustrated /** @versio 1.0 */ class Poit { private it x, y; public double dist(poit p) { jvo@ualg.pt José Valete de Oliveira 3-8 4
Sitaxe for methods jvo@ualg.pt José Valete de Oliveira 3-9 Sitaxe for methods, illustrated @versio 1.0 class Poit { private it x, y; public double dist(poit p) { it dx = x p.getx(); it dy = y p.gety(); retur Math.sqrt(dx*dx+dy*dy); //. jvo@ualg.pt José Valete de Oliveira 3-10 5
A cliet of Poit public class Mai { public static void mai(strig[] args) { Scaer sc = ew Scaer (System.i); Poit A = ew Poit(sc.extIt(), sc.extit()); Poit B = ew Poit(sc.extIt(), sc.extit()); System.out.pritl( (it) A.dist(B) ); sc.close(); jvo@ualg.pt José Valete de Oliveira 3-11 Agai: a very bad solutio a la C // A very poor solutio, usefull for motivatio oly public class Bad { public static void mai (Strig [] args) { it xa, ya, xb, yb; Scaer sc = ew Scaer (System.i); xa = sc.extit(); //... Other vars readig omitted for brevity xa = -5; // Clear violatio ad o easy way to cotrol it L System.out.prit ( (it) dist(xa, ya, xb, yb) ); jvo@ualg.pt José Valete de Oliveira 3-12 6
A cliet of Poit public class Mai { public static void mai(strig[] args) { Scaer sc = ew Scaer (System.i); // Violatio Poit A = ew Poit(-5, sc.extit()); // easily detectable here! Poit B = ew Poit(sc.extIt(), sc.extit()); System.out.pritl( (it) A.dist(B) ); jvo@ualg.pt José Valete de Oliveira 3-13 Costructors class Perso { private Strig ame; public Perso(Strig s) { ame = s; class X { public X() { class Poit { private it x, y; public Poit(it x, it y) { setx(x); sety(y); 7
Costructors class Poit { private it x, y; public Poit(it x, it y) { setx(x); sety(y); A costructor: - iitializes a object -Reserves memory to object data (istace variables) -Returs a referece to that memory area // Cliet code Poit p; // variable declaratio p = ew Poit(1, 2); // calls the costructor Costructor A costructor is called wheever a object is created usig the keyword ew A costructor has the followig characteristics: - It has the same ame as the class - It does ot have a retur type (ot eve void!) // Cliet code Poit p; // variable declaratio p = ew Poit(1, 2); // calls the costructor 8
Default costructor If the class does ot defie ay costructor, the compiler will provide a default (or oargumet) costructor. Default costructor allows for istace variable default iitializatio If the class defie a cotructor (ay costructor) the compiler will ot provide a default costructor aymore. Default variable iitializatios Local variables are ot automatically iitialized Istace variables are automatically iitialized: q boolea types are iitialized to false q Other primitives are iitialized to the zero of their type q Class types are iitialized to ull It is a good programmig practice to explicitly iitialize istace variables withi a costructor 9
Default costructor example // AVOID: Used for motivatio oly class Poit { private it x, y=1024; public it getx() {retur x; public it gety() {retur y; public class Mai { public static void mai(strig[] args) { Poit p = ew Poit(); System.out.pritl(p.getX()); System.out.pritl(p.getY()); Default costructor example /** @versio 1.0 */ class Poit { private it pix, piy; public Poit(it x, it y) { setx(x); sety(y); public it getx() { retur pix; public it gety() { retur piy; public void setx(it x) { if (x<0) System.exit(1); pix = x; public void sety(it y) { Compile time error: o default costructor available! public class Mai { public static void mai(strig[] args) { Poit p = ew Poit(); System.out.pritl(p.getX()); System.out.pritl(p.getY()); 10
Explicit default costructor /** @versio 1.1 */ class Poit { private it pix, piy; public Poit() {; public Poit(it x, it y) { setx(x); sety(y); // Explicit default costructor a improved versio /** @versio 2.0 */ class Poit { private it pix, piy; public Poit() { pix = 0; piy = 0; public Poit(it x, it y) { setx(x); sety(y); // 11
Refereces I Java a variable cotais either a value of primitive data type or a referece. For primitive data types, assigig meas copyig the value resultig from the right expressio to the cotets of the variable o the left. I the example, it i, j =10; i=j; both i ad j have idepedet storage spaces, that after assigmet will have the same value jvo@ualg.pt José Valete de Oliveira 3-23 Refereces Should a variable hold a referece, assigig meas that the variable o the left will ow refer to the object resultig from the evaluatio of right expressio. I the example: Poit a, b = ew Poit(1, 2); a = b; a.setx(0); System.out.pritl(b.getX()); Both variables a, ad b will refer both to the same object of the class Poit jvo@ualg.pt José Valete de Oliveira 3-24 12
Argumet passig I Java, argumet passig is allways doe by value. The values of primitive data types are copied to the fuctio formal parameters; for other types, what are copied are the refereces (ot the objects themselves). This is equivalet to referece argumet passig. jvo@ualg.pt José Valete de Oliveira 3-25 Check poit: what s the output? class Poit { private it x,y; public void setx(it x) { x = x; public it getx() { retur x; public class Check{ public static void mai(strig[] args) { Poit origi = ew Poit(); Poit B = origi; B.setX(1); System.out.pritl(origi.getX()); jvo@ualg.pt José Valete de Oliveira 3-26 13
Method overloadig Method overloadig occurs whe two or more methods withi the same class have the same ame To be valid, ay two defiitios of the method ame must have differet sigatures q A sigature cosists of the ame of a method together with its parameterlist q Differig sigatures must have differet umber ad/or types of parameters Method overloadig Whe a ame of a method is declared twice withi the same class, the compiler views the secod declaratio as follows: - If the retur type ad the sigature of both methods agree, methods are viewedas a duplicate; it mult (it a, float b) { ; It mult (it a, floatb) { // ; //Error duplicate method - If the sigature of both methods are equal but the retur type are differet, the secod declaratio is viewed as a error; float mult (it a, float b) { // ; It mult (it a, float b) { ; //Error - If sigatures are differet i the umber ad/or type of parameters, both methods are viewed as overloaded. float mult (it a, float b) { // ; float mult ( float a, float b) { // ; //OK 14
Scope Each program elemet has a scope the cotext where it ca be referred to usig its simple ame. Elemets ca have the same ame whe their scope does ot overlap. { it k = 0; //... for (it k = 0; k< 3; k++) { // OK! No problem! Scope Each program elemet has a scope the cotext where it ca be referred to usig its simple ame. If the scopes of two variables overlap, the variable defied i the ier scope takes precedece over the variable with the same ame i the outer scope. { it k = 0; //... for (it k = 0; k< 3; k++) { // loop k hides outer k 15
Scope The Scope of a elemet is determied by the place where the elemet is defied. Variables with the same ame but differet scope are differet variables. Possible scopes i Java: q Local q Fuctio q File q Class q Prototype q Package class Poit { private it x, y; public Poit(it x, it y) { // x, y: the same ame // i a differet scope // Where are we so far? Java q q q q q A little bit of history Goals ad characteristics First programs Cotrol of visibility Classes Sitaxe Iitializatio of objects: costructors Refereces Method overloadig (to be cotiued.) jvo@ualg.pt José Valete de Oliveira 3-32 16