ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design
ACM/JETT Workshop - August 4-5, Topics In this Lecture, we will discuss the some of the fundamental notions in Object-Oriented systems: –Encapsulation of data and functions as objects. –Class vs object –Information hiding using public and private –Creating instances /objects from classes. –Calling methods on objects –Instance vs class-level data members. –Java code segments to illustrate these concepts.
ACM/JETT Workshop - August 4-5, What to expect After this lecture, you should be able to use the given code in BankAccount.java file and do the following: –Add a new method to class BankAccount. –Create instances from class BankAccount. –Use the methods defined in BankAccount –Compile and test your code.
ACM/JETT Workshop - August 4-5, 2005 An Object-Oriented System An object-oriented system consists of a network of objects which collaborate to solve a problem. Objects store data and perform actions. bankaccount bank customer showBalance (acctId) getBalance()
ACM/JETT Workshop - August 4-5, 2005 An Object-Oriented (OO) System To understand the object-oriented concepts and OO design, we will study a small part of an application area that we are all familiar with: banking. A simple specification of banking: A bank maintains a collection of bank accounts, where a bank account can be a checking or a savings account. Each bank account is associated with a customer. A customer queries,deposits and/or withdraws money into a bank account.
ACM/JETT Workshop - August 4-5, 2005 An Object-Oriented System The figure below shows some of the objects in the specification and a scenario where a customer calls a showBalance() function on a bank object; in turn, a bank object calls getBalance() function on a bank account object. bankaccount bank customer showBalance (acctId) getBalance()
ACM/JETT Workshop - August 4-5, 2005 What are Objects? Objects are “ things” that encapsulate data and behavior (actions called methods). Objects can be “physical” things (eg: teacher, book, fish). Objects can be “conceptual” things (eg: registration, payment, bank account). Objects correspond to “nouns” in problem description.
ACM/JETT Workshop - August 4-5, What are objects? Objects encapsulate data and behavior. A Bank Account object has –acctId –balance –calcInterest () –deposit () –withdraw () –getBalance() data methods (functions) (behavior)
ACM/JETT Workshop - August 4-5, What are objects? Objects may have an internal structure (private). –Eg: A bank account may store an acctId, balance etc. Objects reveal themselves by their external behavior (public methods). –Eg: A back account object may be used with its public methods, like deposit and withdraw.
ACM/JETT Workshop - August 4-5, What are objects? Objects reveal their “public behavior” but keep their "inside (or implementation)" hidden. The “public behavior” of objects is through a set of methods (functions) that are offered to clients of objects. The methods (functions) can be invoked by clients. A client is an object that invokes the methods of an object.
ACM/JETT Workshop - August 4-5, Objects = data + methods An object of type BankAccount acctId deposit() withdraw() privatepublic calcInterest() balance getBalance() Client A client can call any public methods data methods
ACM/JETT Workshop - August 4-5, What is a class A class is a software blueprint that defines the variables and the methods common to all objects of a certain kind. A class can be used and reused many times to create many objects. A class is one mechanism to define new types in Java. Objects are created as instances of a class.
ACM/JETT Workshop - August 4-5, Information hiding In a class, public features can be seen and manipulated by anybody -- they are the external (interface) view. private features can be manipulated by only the other members of that class. They are the internal (implementation) view. Good OO design recommends that –methods are public and –data fields are private.
ACM/JETT Workshop - August 4-5, Class vs Object acctId deposit() withdraw() privatepublic calcInterest() balance getBalance() acctId= A12 deposit() withdraw() privatepublic calcInterest() Balance= getBalance() BankAccount – a class myAccount – an object of type BankAccount
ACM/JETT Workshop - August 4-5, Class vs Object How do you define a class in Java? Show class BankAccount Example1_3 Show class BankAccount Example1_3
ACM/JETT Workshop - August 4-5, Creating objects from a class Objects are created by instantiating a class. An object is also called an instance. In Java, you create an object from a class using new().
ACM/JETT Workshop - August 4-5, Creating objects from a class // Declare a variable // of type BankAccount BankAccount myAcct; myAcct // Create an instance of type BankAccount // Store the reference in the variable // myAcct myAcct = new BankAccount(); acctId = “Not set” Balance = 0.0 myAcct
ACM/JETT Workshop - August 4-5, Calling Methods of an objects The public methods of an object can be invoked with a. (dot) operator. Example: BankAccount myAcct; myAcct = new BankAccount(); // deposit $ by invoking the // deposit() method on myAcct myAcct.deposit(100.00);
ACM/JETT Workshop - August 4-5, Instance data members of an object // Declare a variable of type BankAccount BankAccount myAcct; myAcct = new BankAccount(); BankAccount acct2 = new BankAccount(); // deposit into myAcct. myAcct.deposit(100.00); // deposit into acct2. acct2.deposit(500.00); myAcct acctId = “Not set” balance = 0.0 acct2 acctId = “Not set” balance = 0.0 The instances myAcct and acct2 have their own copy of the data members, balance and acctId (instance data members)
ACM/JETT Workshop - August 4-5, Class-level Data members Assume that we want to include the fact that the current interest rate for all bank accounts is 5%. If the rate changes, all bank account instances should have the same rate. In this case, we can define the interest rate as a class level data member. In Java, class-level members are called static members. Both data and methods can be static.
ACM/JETT Workshop - August 4-5, Class-level Data members Methods that are static can only access static data members. Static methods can be called on a class without creating an instance. Example: BankAccount myAcct = new BankAccount(); // Calling an instance method on an instance myAcct.deposit (100.00); // Calling a static method on a class double rate = BankAccount.getRate();
ACM/JETT Workshop - August 4-5, Class-level Data members Show class BankAccount Example2_3 Show class BankAccount Example2_3
ACM/JETT Workshop - August 4-5, State of an object The state of an object is described by the values of (some or all) of its data members. Example: The state of a bank account object would include the current balance.
ACM/JETT Workshop - August 4-5, When do objects change their state? Objects change their state as a result of executing a method. Example: The state of a BankAccount object (with a current balance of $0) will change when a deposit() or withdraw() method is invoked on that object.
ACM/JETT Workshop - August 4-5, The topics that you should be comfortable with at this time Class vs object How to define a class in Java private vs public members of a class Creating instances/objects from classes in Java. Calling instance methods on objects and Calling class methods on class/object using the Java code segments given.
ACM/JETT Workshop - August 4-5, To test your understanding Do Lab 1