Lecture 10 - 27-Feb-2025
Lecture 10 - 27-Feb-2025
Writing Classes
Writing Classes
• We've been using predefined classes. Now we will learn to write our
own classes to define objects
2
Outline
Anatomy of a Class
Encapsulation
Anatomy of a Method
Relationship between an object and a class
• Class
• Blueprint of an object
• The blueprint defines the important characteristics of object
• Blueprint of a house defines (walls, windows, doors, etc)
• Once blueprint created => use it to build objects (houses)
• represents the concept of an object
• Any object created is a realization of the concept
• Example
• String class (concept) => String object (specific characters)
Another example
• Suppose a class
• Called student represents a particular student
• Student is the general concept of a student
• who has (name, address, Major, GPA)
• To whom you need (to set address, major, compute GPA)
• Every object created => an actual student
• In a system
• that helps manage the business of a university
• one student class and 1000s of student objects
Example of Classes
Object
• An object
• has a state defined by the attributes associated with that object
• Attributes of student => Student’s name, address, major, etc..
• stores the values of attributes for a particular student
• whose attributes defined by variables declared within a class
Object
• An object
• has behaviors defined by the operations associated with that object
• Operations of a student => update student’s address, GPA etc
• executes the operations defined by the class
• whose operations defined by methods declared within a class
Classes
• A class can contain data declarations and method declarations
Method declarations
Classes and objects
• Consider a six-sided die (singular of dice)
• its state can be defined as which face is showing (integer called FaceValue)
• its primary behaviour (method) is that it can be rolled: setting its value to a
random number between one and six
• See Die.java
The Die Class
• The Die class contains two data values
• a constant MAX that represents the maximum face value
• an integer faceValue that represents the current face value
• The roll method uses the random method of the Math class to
determine a new face value
• There are also methods to explicitly set and retrieve the current face
value at any time
The toString Method
• All classes that represent objects should define a toString method
• The toString method returns a character string that represents the
object in some way
• It is called automatically when an object is concatenated to a string or
when it is passed to the println method
Constructors
• A constructor is a special method that is used to set up an object
when it is initially created
• A constructor has the same name as the class
• The Die constructor is used to set the initial face value of each new
die object to one
Data Scope
• The location at which a variable is declared defines its scope , which is
the area within a program in which that variable can be referenced.
• Data declared within a method can be used only in that method
• Data declared within a method is called local data
• In the Die class, the variable result is declared inside the toString
method -- it is local to that method and cannot be referenced anywhere
else
Instance Data
• A variable declared at the class level (such as faceValue) is called instance data
• Each instance (object) has its own instance variable
• A class declares the type of the data, but it does not reserve any memory space for it
• Every time a Die object is created, a new faceValue variable is created as well
• The objects of a class share the methods but each object has its own data space
• That's the only way two objects can have different states
Instance Data
• We can depict the two Die objects from the RollingDice program as
follows
die1 faceValue 5
die2 faceValue 2
Some methods may provide accessor and/or mutator capabilities as a side effect of their primary
purpose. For example, the roll method of the Die class changes the faceValue of the die and returns
that new value as well. Therefore, roll is also a mutator method.
Outline
Anatomy of a Class
Encapsulation
Anatomy of a Method
Method
• A method is a group of programming language statements that is given a
name
• A method declaration specifies the code that is executed when the method
is invoked.
• Every method in a Java program is part of a particular class.
• When a method is called:
• One by one, the statements of that method are executed
• The header of a method includes the type of the return value, the method
name, and a list of parameters that the method accepts.
• The statements that make up the body of the method are defined in a
block delimited by braces.
Method Declaration
Method Control flow
method
parameter list
name
• A method should have one return statement as the last line of the
method body
The return statement is used to exit a method and optionally send a value back to the calling method.
40
Return Statement
• Return type specified in method header
• Primitive type or class name
• When the method returns a value
• In this case, the method must have a return statement
• Return statement : return + value to be returned
• Driver programs are often used to test other parts of the software
• The Transactions class contains a main method that drives the
use of the Account class, exercising its services