Lecture Content Flashcards
Skeleton (hierarchy) of a Class
/* Package declaration */
/* Import declarations */
class House {
/* Field declarations */
/* Constructors */
/* Methods */
Why should an object should hide its implementation details?
- To prevent misuse
- To simplify code (hide stuff you dont need to see)
- To insolute users from implementation changes
Classes should present their users with an interface that
- gives them access to well-specified behaviours
- hides internal data representation
- hides algorithmic details
- preserves data integrity
Encapsulation: Classes and Users
Classes should allow you to change the internal representation without affecting users
A good problem solving strategy: Divide and Conquer
Understand problem, find tools, break into small problems.
- Try to understand or define the problem
- Choose the right tools for the job (and read the documentation)
- Break complex problems into smaller sub-problems
- Sub-problems can be broken again into yet smaller problems
- Simple enough problems can be solved directly
- Small solutions combine to solve bigger problems
Java Strategy: Imaginary Methods
Typically, a solution to a problem of reading, manipulating or writing information is a method.
When writing one method, if you need to do a sub-task that is too complex to solve straight-away.. .
- Imagine there is a method that does that sub-task for you
- Give it a name, inputs, return type and meaning
- Then just use it as if it exists
Compile after each method
- The compiler will tell you what new methods need defining.
What to keep in mind when designing methods (even for imaginary):
-
Meaning of inputs (if any):
- How to interpret the information passed in
-
Preconditions (if any):
- Things which must be true as method is called
-
Postconditions (if object’s state changes):
- Things guaranteed by end of method execution
-
Meaning of return value (if any):
- How to interpret information returned
!! variables constrained within some local scope.
• method input arguments…. . . within method definition
• locally defined variables…. . . until the end of method
• this object . . .
. . . within an instance method
• variables initialised in a for-loop. . .. . . within the for-body
due to local scope, within a method block can only directly access local parts of the stack, but….
. . . but ‘this’ allows you to access the owning object
. . .‘return’ variables allow information to escape the local scope
instance methods
a method that is invoked on a specific object, requesting the object to do something, and maybe changing the object’s state on the way
x = myCircle.area();
instance attributes/fields
myCircle.diameter;
static methods
aka class methods
static fields
aka class variables
Only one static object is created per class– not one per object
Static Fields as Global Variables: Global object references
System.out is a global object reference
static fields usually declared final or not?
yes, they don’t change
how to make methods global
static can be used to make methods global
(static methods, like static fields, are underlined in UML)
5 features of Arrays
- Arrays are objects
- Arrays can contain objects
- Arrays can contain other arrays
- All arrays have a length
- Arrays of arrays needn’t be ’rectangular’
- Not all rows need be the same size