Presentation is loading. Please wait.

Presentation is loading. Please wait.

 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,

Similar presentations


Presentation on theme: " An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,"— Presentation transcript:

1

2  An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account, bus, etc,etc,etc…  An object is an idea that represents a real- world object  Objects are characterized by state and behavior

3  A person has a state described by their weight, height, eye color, hair color, profession, race, ethnicity, etc, etc, etc  People also have behaviors, of which there are probably millions. Speaking, thinking, reading, running, jumping, etc, etc, etc  A variable that represents an object is called an object reference

4  A class is a software blueprint (a template, or outline) for implementing objects  An object is considered an instance of a class ◦ Usually have many different instances of one class in a program  The current state of an object is maintained in it’s data fields or instance variables

5  A class essentially defines a new data type. We have used classes before ◦ Scanner class ◦ JButton, JFrame, Jpanel ◦ Random class  A class as a blueprint never has a ‘main’ method

6  From now on, we need to differentiate between the classes we have coded in the past with main methods, and the classes that we’ll be using now to define objects  A class that holds a blueprint for an object will be referred to as simply a class. These classes don’t ‘run’ or ‘compile’  A class that has a main method is what we will use to test our classes. These will be called either Driver programs or Client programs.

7  3 types of methods: ◦ Constructors ◦ Accessors ◦ Mutators  All method headers, with the exception of constructors, look like this: (access specifier) (return type) (name) (parameter list)

8  The access specifier tells which other methods can call the method  A return type of void signals that the method does not return a value  Items in the parameter list are separated by commas

9  Creates/initializes an object of the class  Easily recognizable and distinguishable from other methods: ◦ Same name as the class ◦ No return type (not even the word void)  Different constructors provide different ways initializing class objects  Default constructors & value constructors

10  Has no arguments. It provides reasonable initial values for an object  Bank Account default constructor implementation  In a client program: BankAccount b = new BankAccount();  Constructs a BankAccount object with a balance of 0 and a password equal to the empty string.

11  Constructor with parameters that actually initializes the instance variables of the object with values  Bank Account value constructor implementation  In a client program: ◦ BankAccount c = new BankAccount(“Mr. K”, 1000);  Creates a BankAccount object with Mr. K as the password, and 1000 as the balance

12  An accessor method accesses a class object without altering the object  Used to return some information about the object into the client program  BankAccount accessor implementation  In a client program: ◦ BankAccount b1 = new BankAccount(“PW1”,500.0); ◦ BankAccount b2 = new BankAccount(“PW2”,600,0); ◦ if (b1.getBalance() > b2.getBalance()) ◦ ……

13  The. operator (dot operator) indicates the following method (getBalance()) is a method of the class to which b1 and b2 belong, that is, the BankAccount class.  You know where to look to find the method if you know what type of object called it

14  A mutator method changes the state of an object by modifying at least one of its instance variables  BankAccount mutator implementation  In a client program: ◦ b1.withdraw(“PW1”, 200.0); ◦ b2.deposit(“PW2”,37.89);

15  Outputting your object normally will give you the class name followed by a memory location of that object: BankAccount@fea485c4  You need to write your own toString() method for each of your classes that will format your object appropriately

16  Header: ◦ public String toString()  For Bank account, maybe: ◦ return “Bank Account: balance = $” +myBalance;  Now: ◦ BankAccount b = new BankAccount(600); ◦ System.out.print(b); will produce: ◦ Bank Account: balance = $600

17  A static variable contains a value that is shared by all instances of the class  Does not pertain/refer to any particular instance of the class  Typical uses: ◦ Keep track of statistics for objects of the class ◦ Accumulate a total ◦ Keep track of total number of objects created

18  A static final variable cannot be changed.  Capitalized, often declared public  Keyword static indicates there is a single value that applies to whole class, instead of a new instance for each object of the class  In a client class: ClassName.VARIABLE ◦ Math.PI ◦ BankAccount.OVERDRAWN_PENALTY


Download ppt " An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,"

Similar presentations


Ads by Google