1 / 21

Classes & Objects Revisited

Classes & Objects Revisited. Fall 2013. UML Notation. ClassName. + varName1 : type -varName2: type #varName3: type varName4: type. Variables and methods modifiers +  public #  protected  private <space>  default ~  package ____  static. +Constructor()

edythe
Download Presentation

Classes & Objects Revisited

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes & Objects Revisited Fall 2013

  2. UML Notation ClassName +varName1: type -varName2: type #varName3: type varName4: type • Variables and methods modifiers +  public #  protected •  private • <space>  default ~  package ____  static +Constructor() +Constructor(varName: type) +method(): type -method2(varName: type): type #method3((): type

  3. Classes Classes are constructs that define objects of the same type. A Java class uses • instance variables to define data fields • methods to define behaviors. Additionally, a class provides special types of methods, known as constructors, which are invoked to construct objects from the class.

  4. Classes

  5. Class Definition public class ClassName { // instance variables for holding object attributes // instance variables // constructor: build the object // default constructor: instantiates class and initializes public ClassName() { // Define the default constructor, i.e., no parameters } // other constructors public ClassName(parameter list) { // Define constructor with parameters list } // mutators: manage requests for changing attributes // accessors: supply the values of the attributes // facilitators: uses attributes to calculate responses } ClassName Instance variables +ClassName() +ClassName(varName: type) +method(): type -method2(varName: type): type #method3((): type

  6. Default Constructor A class may be declared without constructors. In this case, a no-arg constructor with an empty body is implicitly declared in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly declared in the class.

  7. Instance Variables vs Local Variables • Instance variable: defined at the class level, i.e., not in a method definition but in the class definition • Used to store data specific to an object created in the class • Known to all methods in the class • Default initialization • 0 for integers (byte, short, int, long), 0.0 for double, 0.0F for float, ‘\u000’ for char, false for boolean, null for reference type • Local variables: defined in methods in the class • Used to store information specific to that method • Known only to that specific method • No default initialization, so unknown initial value when created

  8. Creating Classes • A java class is a program that is used as a template to create objects • Choose class name • Identify data items in that class • These become instance variables • Identify how to create an object from the class • These become the constructors • Identify actions you can take in the class • These become methods

  9. Class Definition ClassName public class ClassName { public int var1; //instance variable, will be initialized to 0 /* * Default no parameter constructor is automatically created * by java if no other constructor is defined. * If a constructor is defined, java does not create a no * parameter constructor. */ public ClassName () { var1 = 1; // sets var1 to 1 when the object is created } /* Constructor with a parameter */ public ClassName (int inVal) { var1 = inVal; //sets var1 to the value of inVal } /* method with void return will not return a value. * Must not use the keyword return */ public void setVar1 (int inVal) { var1 = inVal; } /* method with int return, will return a value of type int. * If a method has a return type, you must return a value of that type */ public int getVar1 () { return var1; } } +var1: int +ClassName() +ClassName(inVar: int) +setVar1(inVal: int): void +getVal1(): iint

  10. Create an Object from Class ClassName public class TestClassName { public static void main(String[] args) { int a = 3; //this is a local method, local to method main() ClassName cn1 = new ClassName(); //create object cn1 of type ClassName int b = cn1.getVar1(); //this is a local method, local to method main() System.out.println("cn1.getVar1() = " + b); System.out.println("cn1.getVar2() = " + cn1.getVar2()); ClassName cn2 = new ClassName(a); //create object cn2 of type ClassName initializing var1 to 3 int c = cn2.getVar1(); //this is a local method, local to method main() System.out.println("cn2.getVar1() = " + c); } }

  11. Instance Variables and Methods • Instance variables belong to a specific instance. • Instance methods are invoked by an instance of the class. • An instance of a class is an object created from that class • ClassName cn1 = new ClassName(); • cn1 is an object created from that class, cn1 is an instance of that class ClassName. • Instance variables in that object are assigned specific values for that object.

  12. Static Variables, Constants, and Methods • Static variables are shared by all the instances of the class. • Static methods are not tied to a specific object. • Static constants are final variables shared by all the instances of the class. • To declare static variables, constants, and methods, use the static modifier. • public static int getVar2() { return var2; } • Cannot reference a non-static variable in a static method

  13. Static vs Non-Static instance Variables • Static instance variables • The value stored in a static instance variable is known to all objects created from that class • If one object changes the value of a static instance variable, all the objects will see that change. • Non-static instance variables • Each object has it’s own set of instance variables that were created from the instance variables defined in the class. • Instance variables created by one object are not visible to other objects created from that class.

  14. Adding Static Variables and Methods public class ClassName { public int var1; public static int var2; //instance variable declared as static public ClassName () { var1 = 1; var2++; //add 1 to the static variable each time a new object is created } public ClassName (int inVal) { var1 = inVal; var2++; //add 1 to the static variable each time a new object is created } public void setVar1 (int inVal) { var1 = inVal; } public int getVar1 () { return var1; } public int getVar2() { return var2; } //static method, cannot work with non-static instance variables public static int equation(int inVal) { return var2 * inVal; } } ClassName +var1: int +var2: int +ClassName() +ClassName(inVar: int) +setVar1(inVal: int): void +getVal1(): int +getVar2(): int +equation(inVal: int): int

  15. Using Static Variables and Methods public class TestClassName { public static void main(String[] args) { int a = 3; //this is a local method, local to method main() ClassName cn1 = new ClassName(); //create object cn1 of type ClassName int b = cn1.getVar1(); //this is a local method, local to method main() System.out.println("cn1.getVar1() = " + b); System.out.println("cn1.getVar2() = " + cn1.getVar2()); //create object cn2 of type ClassName initializing var1 to 3 ClassName cn2 = new ClassName(a); int c = cn2.getVar1(); //this is a local method, local to method main() System.out.println("cn2.getVar1() = " + c); //static variables use System.out.println("cn1.getVar2() = " + cn1.getVar2()); System.out.println("cn2.getVar2() = " + cn2.getVar2()); //static method use System.out.println("cn2.equation(5) = " + cn2.equation(5)); } }

  16. Visibility Modifiers and Accessor/Mutator Methods • No modifier • By default, the class, variable, or method can be accessed by any class in the same package. • public The class, data, or method is visible to any class in any package. • private The data or methods can be accessed only by the declaring class. The get and set methods are used to read and modify private properties.

  17. Adding Visibility Modifiers public class ClassName { public int var1; public static int var2; //instance variable declared as static private int var3; public ClassName () { var1 = 1; var2++; //add 1 to the static variable each time a new object is created } public ClassName (int inVal) { var1 = inVal; var2++; } public void setVar1 (int inVal) { var1 = inVal; } public int getVar1 () { return var1; } ClassName +var1: int +var2: int -var3: int +ClassName() +ClassName(inVar: int) +setVar1(inVal: int): void +getVal1(): int +getVar2(): int +equation(inVal: int): int -getVar3(): int -setVar3(inVar: int): void +display(): void

  18. Adding Visibility Modifiers public int getVar2() { return var2; } public static int equation(int inVal) { return var2 * inVal; } private void setVar3(int inVal) { var3 = inVal; } private int getVar3() { return var3; } public void display() { setVar3(getVar1() + getVar2()); System.out.println(getVar3()); } } ClassName +var1: int +var2: int -var3: int +ClassName() +ClassName(inVar: int) +setVar1(inVal: int): void +getVal1(): int +getVar2(): int +equation(inVal: int): int -getVar3(): int -setVar3(inVar: int): void +display(): void

  19. Using Private Variables and Methods public class TestClassName { public static void main(String[] args) { int a = 3; //this is a local method, local to method main() ClassName cn1 = new ClassName(); //create object cn1 of type ClassName int b = cn1.getVar1(); //this is a local method, local to method main() System.out.println("cn1.getVar1() = " + b); System.out.println("cn1.getVar2() = " + cn1.getVar2()); //create object cn2 of type ClassName initializing var1 to 3 ClassName cn2 = new ClassName(a); int c = cn2.getVar1(); //this is a local method, local to method main() System.out.println("cn2.getVar1() = " + c); System.out.println("cn1.getVar2() = " + cn1.getVar2()); System.out.println("cn2.getVar2() = " + cn2.getVar2()); System.out.println("cn2.equation(5) = " + cn2.equation(5)); System.out.print("cn2.var1 = " + cn2.getVar1() + " and cn2.var2 is " + cn2.getVar2() + " so cn2.display() is: "); cn2.display(); //public access to private variable and private method System.out.print("cn1.var1 = " + cn1.getVar1() + " and cn1.var2 is " + cn1.getVar2() + " so cn1.display() is: "); cn1.display(); // int d = cn1.getVar3(); //cannot access var3 outside class } }

  20. Visibility Modifiers +C1 ~p1 +x: int y: int -z: int +C2 +C3 ~p2 • class C2 • Can access C1 • Class C3 • Cannot access C1 • Can access C2 +m1(): void -m3(): void aMetjhod(): void aMetjhod(): void package p1; public class C1 { public int x; int y; private int z; public void m1() { } void m2() { } private void m3() { } } package p1; public class C2 { void aMethod() { C1 o = new C1(); can access o.x; can access o.y; cannot access o.z; can invoke o.m1(); can invoke o.m2(); cannot invoke o.m3(); } } package p2; public class C3 { void aMethod() { C1 o = new C1(); can access o.x; cannot access o.y; cannot access o.z; can invoke o.m1(); cannot invoke o.m2(); cannot invoke o.m3(); } }

  21. Package One Beta Alpha • Package One • getVar1 +v1: int -v2: double #v3: boolean +var1: int -var2: String var3: int +Beta() +Beta(NewV3: boolean) +getV1(): int -getV2(): double #getV3(): boolean +setV1(newV1: int): void -setV2(newV2 double): void -setV3(newV3: boolean): void +Alpha() +Alpha(newVar1: int) +getVar1(): int -getVar2(): String -getVar3(): int +setVar1(newVar1: int) -setVar2(newVar2: String setVar2(newVar3: int) Package Two Delta Charlie +vD1: int -vD2: double #Dv3: boolean +vC1: String -vC2: int +Alpha(newVC1: String) +getVC1(): String -getVC2(): int +setVC1(newVC1: String) -setV2(newVC2: int) +Delta() +setVD1(newV1: int): void -setVD2(newV2 double): void -setVD3(newV3: boolean): void

More Related