JAVA CLASSES.

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Written by: Dr. JJ Shepherd
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Function Overloading Having more than one function with the same name. list of argument of a function are called signature of a function. We can have more.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
Writing Classes (Chapter 4)
Introduction to Object-Oriented Programming
1 OOP in C#: Object Interaction. Inheritance and Polymorphism OOP in C# - Del 1.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Objects and Classes Mostafa Abdallah
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Written by: Dr. JJ Shepherd
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Non-Static Classes What is the Object of this lecture?
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Topics Instance variables, set and get methods Encapsulation
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Chapter 3 Implementing Classes
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Written by: Dr. JJ Shepherd
Lecture 3 John Woodward.
Data Structures and Algorithms revision
Chapter 3 – Implementing Classes
Implementing Classes Yonglei Tao.
Packages, Interfaces & Exception Handling
suggested reading: Java Ch. 6
Chapter Goals To become familiar with the process of implementing classes To be able to implement and test simple methods To understand the purpose and.
Chapter Three - Implementing Classes
Chapter 3 Implementing Classes
An Introduction to Java – Part II
More on Classes and Objects
Implementing Classes Chapter 3.
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Dr. R Z Khan Handout-3 Classes
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

JAVA CLASSES

Class Definition: accessSpecifier class ClassName fields constructors { fields constructors methods } Attributes of the object Initialization of the object Actions on the object

Choose a name for a program that simulates a bank account: public class BankAccount { fields constructors methods } We will be able to create objects of type BankAccount

Instance Fields An object stores its data in instance fields Field: a technical term for a storage location inside a block of memory Instance of a class: an object of the class The class declaration specifies the instance fields:

access specifier nameOfVariable Instance Field Syntax An instance field declaration consists of the following parts: access specifier (such as private, public or protected) type of variable (such as double, int, String) name of variable (such as balance) Each object of a class has its own set of instance fields You should declare all instance fields as private access specifier nameOfVariable

Create a field to store a bank account holder’s balance public class BankAccount { private double balance; constructors methods }

private double balance; private int accountNumber; constructors Suppose we modify the BankAccount class so that each bank account has an account number. How does this change/affect the instance fields? public class BankAccount { private double balance; private int accountNumber; constructors methods }

Static Fields private double balance; private int accountNumber; A static field belongs to the class, not to any object of the class. Also called class field. public class BankAccount { private double balance; private int accountNumber; private static String bankName = “McLees Money”; } If bankName was not static, each instance of BankAccount would have its own bank name

Static Fields Static fields should always be declared as private Exception: Static constants, which may be either private or public public class BankAccount { . . . public static final double OVERDRAFT_FEE = 5; /* Refer to it as BankAccount.OVERDRAFT_FEE */ }

Fields for the BankAccount class public class BankAccount { private double balance; private int accountNumber; private static String bankName = “McLees Money”; public static final double OVERDRAFT_FEE = 5; constructors methods }

Methods Three Types: Accessor – gets information to the user Mutator- Behaviors or actions taken on an object of a class Three Types: Accessor – gets information to the user Mutator- changes or alters one of the fields of an object Helper- aids the program in solving problems… usually done behind the scenes

What are the behaviors of a BankAccount object? mutator deposit money This method will add an amount to the balance in account withdraw money This method will add an amount to the balance in account mutator Accessor get balance This method will get the balance in account and return it to the user

Method Syntax: a method header consists of the following parts: access specifier (such as private, public) return type of variable (such as double, int, String, void) method name of variable (such as deposit) list of incoming variables (parameters ) including their type method body in { } access specifier return type nameOfMethod (dataType paramterName) { method body; }

Methods for the BankAccount class public class BankAccount { fields constructors public void deposit (double amount){ … } public void withdraw(double amount){ … } public double getBalance() {…} }

Methods for the BankAccount class let’s add another Helper method to compute the overdraft fee public class BankAccount { fields constructors public void deposit (double amount){ … } public void withdraw(double amount){ … } public double getBalance() {…} private double overdraftFee() {…} }

Constructors: A constructor initializes an instance field Three types of constructors Default-sets all the instance fields to their default values Numbers = 0, Strings = null, booleans = false Overloaded - sets the instance fields to user input Copy-uses a prewritten constructor to initialize other objects Constructor name = class name Constructor body is executed when a new object is created All the constructors will have the same name-only the parameters are different (computer can tell the difference) Constructors for us are always public

Constructor Syntax: public NameOfClass (dataType paramterName) { access specifier (public) name of Class (such as BankAccount) list of parameters, including their data type for overloaded constructors constructor body in { } public NameOfClass (dataType paramterName) { constructor body; }

Constructors for the BankAccount class public class BankAccount { private double balance; private int accountNumber; private static String bankName = “McLees Money”; public static final double OVERDRAFT_FEE = 5; public BankAccount ( ) balance = 0; accountNumber = 0; } public BankAccount (double myBalance, int myAcctNum) balance = myBalance; accountNumber = myAcctNum; methods

Public Interface for the BankAccount class public class BankAccount { private double balance; private int accountNumber; private static String bankName = “McLees Money”; public static final double OVERDRAFT_FEE = 5; public BankAccount ( ) balance = 0; accountNumber = 0; } public BankAccount (double myBalance, int myAcctNum) balance = myBalance; accountNumber = myAcctNum; public void deposit (double amount){ … } public void withdraw(double amount){ … } public double getBalance() {…} private double overdraftFee() {…}