#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Java - User Defined classes
1.
2. ITC 210 ® 2015
Java does not provide all the classes that
you will ever need, so it permits you to design
and implement your own classes. Therefor, you
must learn how to create your own classes.
3. ITC 210 ® 2015
An object of a class has both data and
operations that can be performed on that
data.
The mechanism in Java that allows you to
combine data operations on the data in a
single units is called a class.
Combining data and operations on the data is
called encapsulation.
4. ITC 210 ® 2015
private – you cannot access it outside the class
public – you can access it outside the class
protected
5. ITC 210 ® 2015
Class is a reserved word. It only defines a data
type and it announces that declaration of a
class.
The data members of a class are also called
fields.
6. ITC 210 ® 2015
The (non-static) methods of a class are called
the instance method of the class.
When the variable of an instance can be
changed such methods are called mutator
method.
When a instance variable does not change its
value such method are called accessor.
7. ITC 210 ® 2015
Constructor has the following properties:
The name of a constructor is the same name of
the class.
A constructor has not return type nor void.
A class can have more than one constructor.
If a class has more than one constructor, the
constructor must have different signatures.
Constructors execute automatically when class
objects are instantiated.
8. ITC 210 ® 2015
A Constructor has the same name as the
class, and it executes automatically when a
object of that class is created.
Constructors are used to guarantee that
the instance variables of the class are initialized.
Two types of constructor
1. Those with parameters
2. Those without parameters (default
constructor)
9. ITC 210 ® 2015
public class Person {
private String firstname;
private String lastname;
public Person()
{
firstname="";
lastname="";
}
public Person(String first, String last)
{
setName(first,last);
}
public String toString()
{
return (firstname+ " " + lastname);
}
public void setName(String first, String last)
{
firstname=first;
lastname=last;
}
public String getFirstName()
{
return firstname;
}
public String getLastName()
{
return lastname;
}
}
10.
11. ITC 210 ® 2015
A class and its members can be described
graphically using Unified Modeling Language
(UML) notation.
12. ITC 210 ® 2015
Clock
-hr: int
-min: int
-sec: int
+Clock()
+Clock(int, int, int)
+setTime(int, int, int): void
+getHouts(); int
+getMinutes(): int
+getSeconds(); int
+printTime(): void
+incrementSecond(): int
+incrementMinutes(): int
+incrementHours(): int
+equals(Clock): boolean
+makeCopy(Copy): void
+getCopy(): Copy
Contains the name of the class
Contains the data members and their
data types
Contains the method names, parameter
list, and return types.
The + (plus) sign in front of a member
indicates that it is a public member;
The – (minus) sign indicates that it
is a private member.
The # symbol before a member name
indicates that it is a protected
member.