Classes and Objects Diagram of program structure Program
Classes and Objects
Diagram of program structure Program File Class File Variables Constructors Variables File Statements Methods Variables Statements • A program consists of one or more classes • Typically, each class is in a separate. java file 2 2
Solution: Objects • Group together related variables into an object • Like creating your own data structure out of Java building blocks public class <object name> { <field(s)>; } • Syntax to use this data structure: <object> <variable> = new <object>();
Instance Variables • • Recall that a class must define the state of an object and its behavior. We declare state variables (variables that hold the state of the object) in a similar way to that of regular variables, only they appear outside methods, inside the class. State variables are also called instance variables or fields. Roughly speaking, the private modifier means that the variables are not part of the object’s interface. public class Clock { private int hours, minutes, seconds; // … }
Clock constructor invoked Clock c; c = new Clock(10, 45, 0); public class Clock { private int hours, minutes, seconds; public Clock(int h, int m, int s){ hours = h; minutes = m; seconds = s; } // … C: hours: 10 Minutes: 45 Seconds: 0
Constructor Overloading example public Clock(int h, int m, int s){ hours = h; minutes = m; seconds = s; } public Clock(int h) { this(h, 0 , 0); } public Clock() { this(12); }
Visibility Modifiers - Members • • A member is a field, a method or a constructor of the class. Members of a class can be declared as private, protected, public or without a visibility modifier: private int hours; public int hours;
Accessibility Options Four accessibility options: – – public – protected * (default) = “package” ** – private * ** protected is also accessible by package called also “package-private” or “package-friendly” Example: public class Person { private String name; protected java. util. Date birth. Date; String id; // default accessibility = package public Person() {} } 8
The ‘this’ keyword In Java ‘this’ is a reference to myself Example: public class Point { private int x, y; public Point(int x, int y) { this. x = x; this. y = y; } } The ‘this’ keyword is also used to call another constructor of the same class – we will see that later 9
Defining constants Though const is a reserved word in Java it's actually not in use! However the final keyword let's you define constants and const variables Example: public class Thingy { public final static doodad = 6; // constant public final id; // constant variable public Thingy(int id) {this. id = id; } // OK // public set(int id) {this. id = id; } // error! } 10
API Documentation • Your classes are often intended to be used by other programmers • Programmers that use your class are not interested in the way it is implemented. They want to use it as a whole and are only interested in what it does and how to use it. • API (Application Programmer Interface) documentation is a description of the interface of the class intended for the application programmer who wants to use it. • To use the class, we need not (and should not) look at the code. All that is needed is the class API.
- Slides: 11