Chapter 9 Objects and classes Flashcards
Programming using objects is known as?
Object-oriented programming (OOP) involves programming using objects.
What does an object represent?
An object represents an entity in the real world that can be distinctly identified.
An object has an
- identity
- state,
- behaviours.
The state of an object consists of what?
The state of an object consists of a set of data fields (also known as properties) with their current values.
The behaviour of an object is defined by what?
A set of methods.
The state(also known as its properties or attributes) of an object is determined by:
methods
Define classes
Classes are constructs that define objects of the same type.
A class is a template, blueprint, or contract that defines what an object’s data fields and methods will be.
An object is an instance of a class. You can create many instances of a class. Creating an object or an instance is referred to as instantiation.
Constructors definition
A class uses variables to define data fields and methods to define actions.
A class also provides methods of a special type, known as constructors, which are invoked to create a new object.
A constructor has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type.
Constructors syntax
Syntax:
class ClassName {
ClassName(parameter list) {
//constructor body
}
}
The illustration of class templates and objects can be standardized using
Unified Modeling Language (UML) notation.
This notation is called a UML class diagram, or simply a class diagram.
In the UML class diagram, the data field is denoted as (syntax)
dataFieldName: dataFieldType
In the UML class diagram, the constructor is denoted as:
ClassName(parameterName: parameterType)
In the UML class diagram, the method is denoted as (syntax):
methodName(parameterName: parameterType): returnType
A UML Class Design consists of
class name
data fields
constructors and methods
A constructor with no parameters is referred to as a
no-arg constructor
Constructors must have the same name as
the class itself
Constructors do not have a
return type—not even void
Constructors are invoked using the
new operator when an object is created
ex. you have to use the new keyword when invoking a new array.
A constructor can perform any action, but constructors are designed to perform
initializing actions, such as initializing the data fields of objects
T or F. Constructors can be overloaded
T
Constructors are used to construct objects. To construct an object from a class, invoke a constructor of the class using the new operator, as follows:
new ClassName(arguments);
or new Circle();
or new Circle(5.0);
Define: a default constructor
A class may be defined without constructors.
In this case, a no-arg constructor with an empty body is implicitly defined in the class.
This constructor, called a default constructor, is provided automatically only if no constructors are explicitly defined in the class.
A class is essentially a programmer-defined type. A class is a reference type, which means that a variable of the class type can reference an instance of the class.
Newly created objects are allocated in _____ memory and objects are accessed via the object’s _____.
heap
reference variables
After an object is created, its data can be accessed and its methods can be invoked using the ___ ________, also known as the object member access operator:
dot operator (.)
Referencing the object’s dataField:
objectRefVar.dataField
e.g., myCircle.radius
Invoking the object’s method:
ObjectRefVar.methodName(arguments)
e.g., myCircle.getArea()
The data fields can be of _____ types
reference