Chapter 03
Chapter 03
Class members:
• Variables represent the state of an object
• Methods constitute its behavior
The general syntax:
<modifier> class <className> { }
<className> specifies the name of the class
class is the keyword
<modifier> specifies some characteristics of
the class:
• Access modifiers: private, protected, default and
public
• Other modifiers: abstract, final, and strictfp
<variableName>: the name of the object
reference that will refer to the object that you
want to create
<className>: the name of an existing class
<classConstructor>: a constructor of the
class
The right side of the equation creates the object
of the class specified by <className> with the
new operator, and assigns it to <variableName>
(i.e. <variableName> points to it)
allowsyou to define a class (like a variable or a
method) inside a top-level class (outer class or
enclosing class)
aninstance of an inner class can only exist
within an instance of its outer class
Using Methods
Working with Classes and Objects
Understanding Enums
Inheritance
Writing and Invoking Constructors
Writing and Using Interfaces
useful when you want a variable to hold only a
predetermined set of values
define an enum variable in two steps:
1. Define the enum type with a set of named values
2. Define a variable to hold one of those values
subclass of the Java class Enum
Using Methods
Working with Classes and Objects
Understanding Enums
Inheritance
Writing and Invoking Constructors
Writing and Using Interfaces
enable the programmer to write a class based
on an already existing class - the parent class /
super class
The subclass inherits (reuses) the non-private
members of the super class
The keyword to derive a class from another
class is extends
Using Methods
Working with Classes and Objects
Understanding Enums
Inheritance
Writing and Invoking Constructors
Writing and Using Interfaces
The constructor of a class has the same name
as the class and has no explicit return type
The new operator allocates memory for the
instance, and executes the constructor to
initialize the memory