Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science and Object-Oriented Programming

Similar presentations


Presentation on theme: "Introduction to Computer Science and Object-Oriented Programming"— Presentation transcript:

1 Introduction to Computer Science and Object-Oriented Programming
Week 4

2 Examples What is a car? What is a truck?
Abstraction Taking away inessential features until only the essence of some concept remains Examples What is a car? What is a truck? What is a mammal? Week 4

3 We say a black box provides
Related to abstraction A black box Does something (often useful) Provides an interface for controlling it But its inner workings are hidden Examples Cell phone Video game Car We say a black box provides encapsulation Week 4

4 Depends How You Look At It
Week 4

5 Who Cares? Abstraction and black boxes help humans deal with complexity Make things simpler and more efficient You don’t care how things work inside You care only the interface is well-defined Leads to a separation of concerns User of the black box Designer and implementer of inner workings Week 4

6 Let’s Try Abstraction Week 4

7 What’s It Mean to Software?
Our black boxes are objects OOP focuses on Use Interfaces Design of objects Week 4

8 Encapsulation in OOP The use of abstraction to design programs in terms of classes/objects The definition of the class Focused on the interface On one outside - what users know and use On the inside - what programmers design & code Week 4

9 Encapsulation in OOP In COMP 111 we learn: To use
To define (the interface) To design and implement (the inner workings) Week 4

10 Example of Use We don’t know how the String class is implemented
But we do know the purpose and how to use public methods of String e.g. length, toUpperCase, replace But we know What parameters (if any) we need to pass in Their sequence and type of each The type of the return value (if any) Week 4

11 Example of Defining We need to define an abstraction Then translate
With attributes With behavior Then translate Attributes to data Behavior to methods Week 4

12 Public Interface of a Class
Behavior (abstraction) for a Bank Account: deposit money, withdraw money, get balance Methods of a BankAccount class might be: deposit, withdraw, getBalance harrysChecking.deposit(2000); harrysChecking.withdraw(500); System.out.println( harrysChecking.getBalance()); Week 4

13 Method Definitions access specifier (such as public or private)
return type (such as String or void) method name (such as deposit) list of parameters (such as double amount) method body in { } Week 4

14 Specifying Methods accessSpecifier returnType methodName(parameters) { method body } Where parameters is a comma-separated list of parameterType parameterName Week 4

15 Examples public void deposit(double amount) { . . . }
public void withdraw(double amount) public double getBalance() Week 4

16 Constructors are similar to methods an access specifier, parameter list and body but their job is not to define a behavior but to properly initialize an object for later use Week 4

17 Constructors . Named the same as the class, Never have a return value
All constructors of a class have the same name Compiler can tell constructors apart because they take different parameters Week 4

18 Constructors Constructor body is executed when a new object is created
Statements in constructor body will set the internal data of the object Week 4

19 Commenting the Public Interface
Use documentation comments to describe the classes and public methods Use the standard documentation notation so program called javadoc can automatically generate a set of HTML pages Week 4

20 Commenting the Public Interface
Provide documentation comments for every class every method every parameter every return value Javadoc comment are within /** and */ Best to write the method comments first, before writing code in the body Week 4

21 Example /** Withdraws money from the bank account.
@param amount the amount to withdraw */ public void withdraw(double amount) { // implementation filled in later } Week 4

22 Example /** Gets the current balance of the bank account.
@return the current balance */ public double getBalance() { // implementation filled in later } Week 4

23 Generated by Javadoc Week 4

24 Instance Fields A class is the pattern for its instances (objects)
Each object stores its data in instance fields (or instance variables) A field is a term for a storage location in memory An instance of a class is an object of that class Thus, an instance field is a storage location present in each object of the class Week 4

25 Defining Instance Fields
To define an instance field: An access specifier (usually private) The type of the instance field (such as double) The name of the instance field (such as balance) Week 4

26 Example public class BankAccount { . . . private double balance; }
Week 4

27 Instance Fields Each object instance of a class has its own set of instance fields Instance fields are generally declared with the access specifier private The private specifier means cannot be accessed directly other than by methods defined in the class must use methods Week 4

28 More on Access The balance instance variable can be accessed
by the BankAccount deposit method but not by the main method of another class Since instance variables are private all access to their values must be through public methods This is how we implement encapsulation Week 4

29 Next Week Read Module 5 introduction and key points In Big Java
3-7 to 3.8 4.1 to 4.5 Week 4

30 Next Week Submit homework Assignment 4-2 Work on Assignment 5-1
Submit on or before May 29 Into Course Drop Box Worth 15 points Work on Assignment 5-1 Week 5 Guided Learning Activities Nothing to submit Week 4


Download ppt "Introduction to Computer Science and Object-Oriented Programming"

Similar presentations


Ads by Google