80 likes | 96 Views
Classes. we can write our own classes to represent and define our objects e.g. class for Complex objects represents a complex number defines its properties real part imaginary part defines its behaviors constructor(s) for a new number math functions drawing
E N D
Classes • we can write our own classes • to represent and define our objects • e.g. class for Complex objects • represents a complex number • defines its properties • real part • imaginary part • defines its behaviors • constructor(s) for a new number • math functions • drawing • we can then create new objects such class and use their functionality in an arbitrary context
Why Classes? • to represent a logical entity • what makes sense as an object • functionality from same context • to structure software • divide and conquer • smaller pieces are easier to test and debug • software architecture • software engineering • to define a library • Java APIs • our own module that we may reuse • because Java and Eclipse support them
Class Types • a class can be defined in 3 ways: • in a separate .java file • top-level class • can be public • it can be used everywhere • within the same file as a top-level class • local • can be used only by classes defined in the same file • nested within another class • local • can be used within the same scope • within the declaration where it's defined • within nested declarations
Anatomy of a Class • every class has a name and a body • e.g. public class Complex {} • body is enclosed in braces { and } • body contains • instance variables, also fields • constructors • methods • possibly other nested classes • e.g. Complex contains • fields realPart and imaginaryPart • constructors • Complex(), Complex(double real, double imaginary) • methods • read(), write(), add (Complex number), etc. • e.g. ComplexMath contains • nested class Complex
Fields • represent properties of an object • every object has the same fields • but the fields typically have different values • when an objectceases to exist its fields don't exist anymore, too • their values may exist it they are referenced elsewhere • can be used directly by all methods in the class • their scope is the entire class • are typically declared as private • visible only within the class • outside, accessible only through accessor methods • tip: initialize a field in its declaration • e.g. String name = "N/A"; • this prevents errors • NullPainterException because of method call on uninitialized field • such initialization occurs before a constructor is executed • be a good citizen • give a field good name • document it in preceding /** */ Javadoc comment
Constructors • called when a new object is created • typically used to initialize the new object • considered as a special type of a method • same syntax in declaration • except name and return type • constructors can be overloaded • class can define more than one constructor • their parameter list must be different • a constructor can call another constructor • using this(); • e.g. public Complex() {this(0,0);} • calls public Complex(double real, double imaginary) {…} • use this for a field that has the same name as a parameter • e.g. this.real = real;
Methods • used to define what an object can do • every object has the same methods • we call a method on an object • e.g. number.write() calls method write() on object number • within a method we can use this as reference to the object on which the method was called, i.e. within write(), this means number • a method can be public or private • public methods can be called on any object • private methods can be called only within the class • they are called on this object • publicmethods are meant to be used by other objects • privatemethods are auxiliary, used only within the class
Accessor and Mutator Methods • it is dangerous to declare a field as public • because anyone has access to it • they can modify it (assign it another value) • we may want to prevent snoopers to read it's value • we may want to know who and when its accessed • e.g. for debugging purposes • instead, we declare the field as private, and supply • a publicaccessor method to access the field's value • a publicmutator method to assign the field's value • accessor method typically • is named getXyz for a field xyz • returns the field's value, e.g. • public double getReal() {return real;} • mutator method typically • is named setXyz for a field xyz • assigns the field's value, e.g. • public void setReal(double real) {this.real = real;}