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
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Java Software Solutions
Lecture 2: Object Oriented Programming I
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
Enhancing classes Visibility modifiers and encapsulation revisited
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)
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
The Java Programming Language
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.
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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
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.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java Classes Introduction. Contents Introduction Objects and Classes Using the Methods in a Java Class – References and Aliases Defining a Java Class.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
More About Objects and Methods
Chapter 5: Enhancing Classes
Chapter 7 User-Defined Methods.
3 Introduction to Classes and Objects.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Selenium WebDriver Web Test Tool Training
University of Central Florida COP 3330 Object Oriented Programming
CMSC 202 Static Methods.
CSC240 Computer Science III
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Classes and Methods
Chapter 6 Methods: A Deeper Look
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
More About Objects and Methods
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Classes and Objects Static Methods
Object Oriented Programming in java
Java Programming Language
Defining Classes and Methods
Classes, Objects and Methods
Corresponds with Chapter 5
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 Static Fields and Methods Packages – The Java Class Library

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

4 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

5 An Example of a Class An outline of a class

6 Instantiation of the Class Three instances of the class Automobile

7 Create a object for a Java Class Declare: Name joe; Instantiate: joe = new Name (); Combine: Name joe = new Name(); The new operator creates an instance of the class Invokes the constructor method

8 Create a object for a Java Class A variable that references an object.

9 References and Aliases Primitive types:(Built-in to Java) A chunk of memory holding the data itself byte, short, int, long float, double, char, boolean int number1; All other types are reference or class types All objects defined from classes The object “refers to” or “points to” the chunk of memory that actually holds the data String greeting = "Howdy"; greeting is a reference variable instead of the actual string Set to null value for an reference-variable When two variables reference the same instance, they are considered aliases

10 References and Aliases Aliases of an object Name jamie = new Name(); jamie.setFirst(“Jamie”); jamie.setLast(“Jones”); Name friend = jamie; Two references, one chunk of data Is friend == jamie?

11 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

12 Arguments and Parameters Given statements: Name joe = new Name(); joe.setFirst ("Joseph"); joe.setLast ("Brown"); "Joseph" and "Brown" are arguments sent to the methods Arguments must correspond to the formal parameters in the method declaration public void setFirst(String firstName) { First = firstName; }

13 Defining a Java Class Given Store class definition in a file whose name is the name of the class followed by.java. public, private: access modifier class Name is public, any other Java class can use this class private data fields (instance variables) Note data fields are private: only the methods inside this class can access them. All data fields should be private They will require accessor and mutator methods public class Name {private String first; // first name private String last; // last name } // end Name

14 Coding Conventions Naming conventions conventions are things we suggest to make code more consistent and easier to understand this is purely aesthetic; not enforced by the compiler We use capitalization to distinguish an identifier’s purpose class names begin with upper case letters: Use noun or descriptive phrase method, variable names begin with lower case: Use verb or action phrase instance variables start with an underscore Good Name Student takeClass _cs220Student Poor Name Thing (no role, purpose) doStuff (not specific) c (too cryptic) Class Method object Class Method object

15 Passing Arguments Call by value For primitive type, formal parameter initialized to value of corresponding argument in call. Method cannot change the value of the argument. Call by reference For a reference type, formal parameter is initialized to the memory address of the object in the call. Method can change the data in the object.

16 Example 1 public void giveLastNameTo(Name child) { child.setLast(last); } public static void main(String[] args) { Name jamie = new Name(); jamie.setFirst(“Jamie”); jamie.setLast(“Jones”); Name jane = new Name(); jane.setFirst(“Jane”); jane.setLast(“Doe”); jamie.giveLastNameTo(jane); }

17 Passing Arguments The method giveLastNameTo modifies the object passed to it as an argument.

18 Passing Arguments The method giveLastNameTo modifies the object passed to it as an argument.

19 Example 2 Public void giveLastNameTo(Name Child) { String firstName = child.getFirst(); child = new Name(); child.setLast(last); child.setFirst(firstName); } Public static void main(String[] args) { Name jamie = new Name(); jamie.setFirst(“Jamie”); jamie.setLast(“Jones”); Name jane = new Name(); jane.setFirst(“Jane”); jane.setLast(“Doe”); jamie.giveLastNameTo(jane); }

20 Passing Arguments A method cannot replace an object passed to it as an argument.

21 Passing Arguments A method cannot replace an object passed to it as an argument.

22 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 including no parameters Default constructor has no parameters

23 Constructors An object (a) after its initial creation; (b) after its reference is lost memory leak public Name() public Name(String firstName, String lastName) { setFirst(firstName); setLast(lastName); } Name jill = new Name(“Jill”, “Jones”); jill = new Name(“Jill”, “Smith”);

24 Static Fields and Methods A data field that does not belong to any one object Adding reserved word static: private static int numOfInvocation = 0; Also called: static field, static variable, class variable One copy of that data item exists to be shared by all the instances of the class

25 Static Fields A static PI versus a non static field Static does not mean constant, shared by all objects of its class, but value can change. Objects can use a static field to communicate with each other to perform some joint action. private static int numberOfInvocations = 0;

26 Static Methods A method that does not belong to an object of any kind. You use the class name instead of an object name to invoke the method. Java predefined class math contains standard mathematical methods. int maximum = Math.max(2,3); double root = Math.sqrt(4.2); Static method cannot access non-static fields, nor non-static methods. Constructors cannot be static.

27 Packages Multiple related classes 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 Package can contain packages To use the package, begin the program with import myStuff.graphics.*; package names class name myStuff.graphics.FramemyStuff.graphics.Frame

28 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