java - user defined classes
TRANSCRIPT
User-Defined Classes
ITC 210 ® 2015
Classes and Object
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.
ITC 210 ® 2015
Classes and Object
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.
ITC 210 ® 2015
Classification of Member of Classprivate – you cannot access it outside the classpublic – you can access it outside the classprotected
ITC 210 ® 2015
Classes and ObjectClass 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.
ITC 210 ® 2015
Instance MethodsThe (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.
ITC 210 ® 2015
Constructor PropertiesConstructor 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.
ITC 210 ® 2015
ConstructorsA 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 constructor1. Those with parameters2. Those without parameters (default constructor)
ITC 210 ® 2015
Creating your own classpublic 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;}
}
Unified Modeling Language Class
Diagrams
ITC 210 ® 2015
Unified Modeling Lang.A class and its members can be described
graphically using Unified Modeling Language (UML) notation.
ITC 210 ® 2015
UML Class DiagramClock
-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.
ITC 210 ® 2015
Next Topic – Static Member of a Class