Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.

Slides:



Advertisements
Similar presentations
This is Java Jeopardy Writing Methods…chapter 4…
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
INHERITANCE BASICS Reusability is achieved by INHERITANCE
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.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Enhancing classes Visibility modifiers and encapsulation revisited
1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.
Classes, Encapsulation, Methods and Constructors
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
COMP 14: decomposition, overloading, relationships June 7, 2000 Nick Vallidis.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Hello AP Computer Science!. What are some of the things that you have used computers for?
Programming Languages and Paradigms Object-Oriented Programming.
Writing Classes (Chapter 4)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
A Revamping Of our skills so far. Our Tools so Far Variable - a placeholder for a value Method - a set of code which accomplishes a task Class - a mold.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Working With Objects Tonga Institute of Higher Education.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Classes - Intermediate
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Defining Your Own Classes II
Functions + Overloading + Scope
Topic: Classes and Objects
Examples of Classes & Objects
Introduction to Modular Programming
Methods.
Interface.
Classes & Objects: Examples
Building Java Programs
Encapsulation and Constructors
Building Java Programs
More on Classes and Objects
CS2011 Introduction to Programming I Objects and Classes
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Building Java Programs
Scope of variables class scopeofvars {
ITE “A” GROUP 2 ENCAPSULATION.
Chapter 4 Test Review First day
Presentation transcript:

Java 5 Class Anatomy

User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating classes provides us with more flexibility. See book example, p. 214

Visualizing Class Files 1 public class account { private int acctNumber; private double balance; public account(int a, double b) { acctNumber = a; balance = b; } public void printBalance() { System.out.println(“Bal: “ + balance); } Instance variables or the data fields (properties) of the object. These are private and can only be accessed by local methods.

Visualizing Class Files 2 public class account { private int acctNumber; private double balance; public account(int a, double b) { acctNumber = a; balance = b; } public void printBalance() { System.out.println(“Bal: “ + balance); } Constructor builds the object and initializes any default values. Objects do not require a constructor.

Visualizing Class Files 3 public class account { private int acctNumber; private double balance; public account(int a, double b) { acctNumber = a; balance = b; } public void printBalance() { System.out.println(“Bal: “ + balance); } Methods are what an object can do. This method prints out the balance of our account.

Using a Class (driver) public class accountRun { public static void main(String[] args) { account a = new account(325, ); a.printBalance(); } Notice the driver file has a main method and creates a new object using the constructor and rules defined in the account class.

Instance Data An object is an instance of a class. Variables defined in classes are governed by the concept of “variable scope” See code example on web site. Java automatically initializes class variables but it is good practice to do it anyway.

Encapsulation At the object level we think of how to design the class. At the project level we think of how to design objects and how those objects will interact with each other. Objects should be “self-governing” Only the class that declares a variable should be able to change that variable!

Encapsulation Pt. 2 Keywords help us set up this protection. public&private private int total = 5; public void setTotal(int a) { total = a; }

Basic Methods Modifier (public or private) Return type (void, int, float, string, etc.) Identifier (its name) Parameters (arguments to method) public void sayHello() { System.out.println(“Hello”); }

Constructors Methods with the same name as the class called when new objects are initialized. Return nothing. Not required, Java has a built-in constructor if a class doesn’t define one. Jason myObj = new Jason();

Return Values Methods can return values to the calling code. public int even(int a) { if(a % 2 == 0) return 1; else return 0; }

Method Parameters Parameters must be passed into a method in the proper order. The parameter along with the method’s name is called its “signature”. Signature does not include return type. Method parameters are local to the method in scope.

Method Overloading A class can contain several methods with the same name that differ only by their parameters. This is called method overloading. Methods are distinguished by their signature. An example of an overloaded method we’ve used frequently is println().

Method Decomposition Sometimes we want to write a method that is too big and cumbersome and so we break it down into multiple, smaller methods. Always try to break your code down into the smallest sensible blocks to make maintenance easier. Too many small methods may actually complicate things so be careful.