Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.

Slides:



Advertisements
Similar presentations
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Advertisements

Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Written by: Dr. JJ Shepherd
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Class design. int month; int year class Month Defining Classes A class contains data declarations (state) and method declarations (behaviors) Data declarations.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Java Programming Review (Part I) Enterprise Systems Programming.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Class and Object. Class vs Object Let us consider the following scenario… Class defines a set of attributes/fields and a set of methods/services. Object.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
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.
Comp1004: Building Better Objects II Encapsulation and Constructors.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
Defining Classes and Methods
Topic: Classes and Objects
Lecture 3 John Woodward.
Examples of Classes & Objects
AKA the birth, life, and death of variables.
Exceptions, Interfaces & Generics
Implementing Classes Yonglei Tao.
Object-Oriented Programming: Classes and Objects
Classes & Objects: Examples
Encapsulation and Constructors
More on Classes and Objects
AKA the birth, life, and death of variables.
Object Oriented Programming Review
JAVA CLASSES.
Defining Classes and Methods
Defining Classes and Methods
Chap 2. Identifiers, Keywords, and Types
Review for Midterm 3.
Topics to cover Instance variables vs. local variables.
ITE “A” GROUP 2 ENCAPSULATION.
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

Basic Class Structure

Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance methods to access that data object – a particular instance of a class –purpose is to encapsulate data –created via the new operator –many objects of a given class may exist –each object will have the same instance data, with different values –all objects will share the same instance methods –an object is required to invoke instance methods MyClass mc = new MyClass(); mc.someMethod();

Class –variables (data) –methods (behavior) variables (local to the method) statements –Declarations –assignments –method invocations –change of control »tests »loops »return »break »continue »try/catch blocks

Variables Must be declared with a type: –primitive type: int x; –class type: String s; Declaration of a variable does not create an object of that type: Rectangle r; r = new Rectangle (10, 10, 50, 100); Scope: variables have meaning within the statement block {} where they are declared (including inner blocks) public class-level variables have meaning outside the class: –System.out –Dimension dim = this.getSize() int width = dim.width;

Methods Method Definition public class BankAccount { private double balance; // makes deposit & returns the new balance public double deposit (double amount) { balance = balance + amount; return balance; } Method Invocation public class Test { public static void main (String[] args) { BankAccount account = new BankAccount (1000); double value = account.deposit (100.); }

Constructor Sets up the object: initializes instance variables Similar to a method, but –no return type –same name as class Definition public class BankAccount { private double balance; public BankAccount (double amount) { balance = amount; } Invocation BankAccount account = new BankAccount (1000.);

Static Elements a class may have variables and/or methods that are not associated with an object these are called class or static members only one copy of a static variable exists, no matter how many objects are created they are identified by the static keyword they are referenced by using the class name instead of an object instance –setBackground (Color.red); // red is a static variable –double d = Double.parseDouble (“2.5”); main is a static method which is called by the java interpreter –public static void main (String[] args)