Download Java Object-Oriented Programming: Classes, Objects, and Methods and more Study notes Computer Science in PDF only on Docsity! Chapter 3: Objects as Models: Java is a purely object-oriented language, thus everything in Java is modeled via classes (Objects). An object has two basic properties: 1. State: an object contains one or more items of information. 2. Behavior: an object has behavior – it responds to operations that are performed on it. Some of these operations may change the state of it. Representing Objects within a program: The state of an object is stored as instance variable and the behavior is represented as instance methods. The instance variables keep track of the state of the object. They could be variables that stores a single value for example and integer value or variables that store an entire object. Operations would be carried out on each object. To do this we use the instance methods. To invoke an instance method we use the name of the object followed be a period and then the name of the method. For example if the name of the object was account and a method inside this object was deposit, then to use it we would write something similar to the following: account.deposit() Classes: Both the instance variables and instance methods are grouped into a class. Declaring a class is as follows: public class class-name { variable declarations constructor declarations method declaration } Using the example in the notes for account: public class Account { variable declarations constructor declarations method declarations } These declarations are usually preceded by an access modifier – public or private . 1 These would indicate whether if the associated declarations are for use privately only (i.e. to be used within this class only – private) or could be accessed by other classes (i.e. available publicly). It is advisable to declare all instance variable to be private. Example private double balance; This is to facilitate information hiding. The out side cannot see what is stored in balance, as well as by not having access to it, it cannot be changed by the out side world. Declaring Instance methods: public void deposit (double amount) { ……… } Access modifier: The word public means that the method could be used (called) anywhere in the program, not only the class in which it is defined. Result type: indicate what type of answer the method will output (return) when the method is called. Method name: The name can be any legal identifier. Parameters: A method can have any number of parameters (form 0 to …) of any data type including user defined type. When a method is declared, these are called parameters. When the method is called, the values passed are regarded as arguments. Let us declare a class name Sword: public class Sword { private String description; //used to give the sword a descriptive name private int weight; //used to store the weight of the sword private int damage; //used to store the amount damage the sword can do private int magicDamage; //used to store the magical damage the sword can do // now put here the class methods } Method Overloading and Method Overriding: 2 { damage = newDamage; } Declaring constructors: Whenever a new object is created, the instance variables are initialized by a constructor. 1. Constructors may or may not have parameters. 2. A constructor has no return type. 3. The name of the constructor must be the same name of the class in which it is declared/defined. 4. A class may have more than one constructor, provided that they have different amount of parameters and/or data type. 5. Having more than one constructor can be advantageous – one can initialize the instance variable to zero. Constructors are called with the new operator. This is used to dynamically create space (memory) for a new object. Let us define constructors for the Item class and the Sword class: public class Item { String description; //note the absence of the accessor word private int weight; //note the absence of the accessor word private Item() //this is a default constructor or paramaterless { description = “General item”; weight = 0; } Item (String initDescr, int initWeight) //this will allow the user to initialize the values { description = initDescr; weight = initWeight; } public String getDescription() { return description; } public void setDescription(String newDescription) { description = newDescription; 5 } public class Sword extends Item //note the use of extends Item { private int damage; private int magicDamage; Sword (String initDescr, int initWeight, int initDamage, int initMagicDamage) { super(initDescr, initWeight); damage = initDamage; magicDamage = initMagicDamage); } public int getDamage() { return damage; } public void setDamage(int newDamage) { damage = newDamage; } Because Sword is a derived class, the constructor from Item is called to initialize the two common variables – description and weight. The constructor for Sword now only has to initialize the other two variables that are locale to it. Note: If no explicit call (as in the above case) is made to super(), then an implicit call to the parameterless constructor of the base call would be made as the first statement in the derived class constructor. If the base class has a user-defined constructor with arguments, but no argumentless constructor, then a compiler error will occur. Now we can create objects for both the Item and Sword classes as follows: Item defaultItem = new Item(); Item specifyItem = new Item(“My new item”, 50); Sword mySword = new (Excalibur”, 40, 10, 15); 6 Java String Class The string class package in java is probably one of the most important packages available. To use the methods found in this package, you will use the java.lang package. Note: this package is automatically imported into your program when you import the java.io.* package. So on creating string variables, we declare them similar to creating integer or double, example: String last_Name, first_Name, middle_Name; OR String last_Name = “ “, first_Name, middle_Name; In this last case, we have initialize last_Name to the blank or empty. We could assign a value to a string variable: first_Name = “John” Note the use of the quote around john. Also a string object could be created without the use of the word new. A string could be visualized as an array holding the individual characters example, first_Name could be visualized as the following array: J o h n 0 1 2 3 These are the index position starting from 0 We can now extract individual characters from the name JOHN example: char x; x = first_Name.charAt(2); now x will hold the value found at position 2 – “h”. We use the charAt(int) function to access the value at the position supplied (in this case 2). Other functions of immediate importance are listed on page 110. Note also when you call a function of the string class you must follow the following format: 7