Java Reference
In-Depth Information
6.9 Summary
The chapter describes how to create classes and objects. While a class is a general
template that describes the type of data that we are interested in modeling, an object is
an instance of a class that has a concrete value for each data item. The pillar of every class
is its data. Following the data encapsulation and abstraction principles, all non-constant
variables of a class should be made private . This means that the outside world does not
have direct access to the data and needs to interact with it through the available methods.
These methods make the interface of the class. As a general rule, every method inside a
class should be simple and perform a single task.
Instances of a class are created by invoking the class constructor, where a default empty
constructor is provided when no constructors are defined. The job of the constructor is
to initialize the local variables of the class, where they can be set to default values when
the constructor method does not receive their initial values. There are two types of class
members: static and instance. Static methods and variables are defined on the whole class
(e.g., for every class, there is a single instance of a static variable). Conversely, instance
methods and variables are associated with an object of the class (e.g., every object has its
own copies of the instance variables).
The chapter also presents multi-class program solutions. Class interaction principles
are demonstrated through examples. As a general rule, inter-class interaction should be
minimized in order to keep the design simple and extendable.
6.10 Syntax
Refers to the current instance of the class.
this( ... )
Calls a different constructor of the class.
A method that converts an object to a String . It is automatically
called in the printing methods and when we use the operator “+” to concatenate a
String to an object.
equals( ... other)
Create this method when you want to check for object equality.
The method should return true exactly when the objects this and other are equal.
￿ new Foo(...)
Creates a new instance of the Foo class by calling the appropriate
￿ public vs. private
Public members are visible everywhere, while private members
are only visible from within the class.
￿ static
Static members belong to the whole class, while every object has its own
set of instance variables. A variable/method is instance when it does not have the
static identifier in front of it.
￿ null
The value of a reference that does not reference any object.
The m instance method of the C class is called ( C is the class of the o object).
Search WWH ::

Custom Search