Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

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.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
Chapter 41 Defining Classes and Methods Chapter 4.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.
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.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Java Classes Introduction. Contents Introduction Objects and Classes Using the Methods in a Java Class – References and Aliases Defining a Java Class.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Re-Intro to Object Oriented Programming
Objects as a programming concept
Chapter 7 User-Defined Methods.
3 Introduction to Classes and Objects.
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Object Oriented Systems Lecture 03 Method
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Unit-2 Objects and Classes
Defining Classes and Methods
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Programs and Classes A program is made up from classes
Chapter 6 Objects and Classes
Object Oriented Programming in java
Java Programming Language
Defining Classes and Methods
Defining Classes and Methods
Classes, Objects and Methods
Presentation transcript:

Java Classes Chapter 1

2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining a Java Class Passing Arguments Constructors The toString Method Static Fields and Methods Packages – The Java Class Library

3 Objects An object is a program construct that Contains data Performs certain actions The actions are called methods The actions interact to form the solution to a given problem

4 Classes A class is a type or kind of object Objects of the same class have The same kinds of data The same methods A class definition is a general description of What the object is What it can do

5 An Example of a Class Fig. 1-1 An outline of a class

6 Instantiation of the Class Fig. 1-1 Three instances of the class Automobile

7 Using Methods in a Java Class Given: jName joe = new Name(); The new operator creates an instance of the class Invokes the constructor method Valued methods return a single value void methods do not return a value

8 Using Methods in a Java Class Fig. 1-2 A variable that references an object.

9 References and Aliases Primitive types: byte, short, int, long float, double, char, boolean All other types are reference or class types String greeting = "Howdy"; greeting is a reference variable When two variables reference the same instance, they are considered aliases

10 References and Aliases Fig. 1-3 Aliases of an object

11 Arguments and Parameters Given Name joe = new Name(); joe.setFirst ("Joseph"); joe.setLast ("Brown"); "Joseph" and "Brown" are arguments sent to the methods Invocation of method must have same number of arguments as there are formal parameters in the declaration

12 Defining a Java Class Given These are the data fields (instance variables) Note they are private They will require accessor and mutator methods public class Name {private String first; // first name private String last; // last name } // end Name

13 Method Definitions Given This is a valued method Returns a String Given This is a void method public void setFirst(String firstName) {first = firstName; } // end setFirst public String getFirst() {return first; }// end getFirst

14 Naming Methods Start method name with lowercase letter Use verb or action phrase Start class name with uppercase Use noun or descriptive phrase Local variables A variable declared within a method

15 Passing Arguments Call by value For primitive type, parameter initialized to value of argument in call Call by reference For a class type, formal parameter is initialized to the address of the object in the call

16 Passing Arguments Fig.1-4 a & b The method giveLastNameTo modifies the object passed to it as an argument.

17 Passing Arguments Fig.1-4 c & d The method giveLastNameTo modifies the object passed to it as an argument.

18 Passing Arguments Figure 1-5 a & b A method cannot replace an object passed to it as an argument.

19 Passing Arguments Figure 1-5 c & d A method cannot replace an object passed to it as an argument.

20 Constructors A method that Allocates memory for the object Initializes the data fields Properties Same name as the class No return type, not even void Any number of formal parameters

21 Constructors Fig. 1-6 An object (a) after its initial creation; (b) after its reference is lost

22 Static Fields and Methods A data field that does not belong to any one object One instance of that data item exists to be shared by all the instances of the class Also called: static field, static variable, class variable

23 Static Fields and Methods Fig. 1-7 A static PI versus a non static field

24 Packages Multiple related can be conveniently grouped into a package Begin each file that contains a class within the package package myStuff; Place all files within a directory Give folder same name as the package To use the package, begin the program with import myStuff.*;

25 The Java Class Library Many useful classes have already been declared Collection exists in Java Class Library Example The class Math is part of the package java.lang