Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
C++ Classes & Data Abstraction
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Objects, Methods, Parameters, Input Lecture 5, Thu Jan
Road Map Introduction to object oriented programming. Classes
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
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.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
Saravanan.G.
Writing Classes (Chapter 4)
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Objects and Classes. Problem Solving The key to designing a solution is breaking it down into manageable pieces When writing software, we design separate.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Objects & Classes Weiss ch. 3. So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Access Control to Class Members tMyn1 Access Control to Class Members In its support for encapsulation, the class provides two major benefits. First, it.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
Classes - Intermediate
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.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Class Fundamentals BCIS 3680 Enterprise Programming.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
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.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Variables in Java A variable holds either
3 Introduction to Classes and Objects.
Examples of Classes & Objects
Classes A class is a blueprint of an object
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Classes Variables That Are Not of a Built-in Type Are Objects
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Classes & Objects: Examples
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Classes, Encapsulation, Methods and Constructors (Continued)
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Packages & Random and Math Classes
Chapter 9 Objects and Classes
ITE “A” GROUP 2 ENCAPSULATION.
CSG2H3 Object Oriented Programming
Presentation transcript:

Classes/Objects

Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values –can be initialized to null Use new to instantiate new objects/initialize object variables –String name = new String(“Mickey”); –invokes Constructor of String class

Example int num = 5; String name = new String(“Mickey”); String name2 = “Minnie”; //String shortcut 5 num name Mickey name2 Minnie

Dot Operator Use dot operator to access methods of an object –name.length()

Aliases Two object variables can point to the same object With primitive types, a copy is made int a = 5; int b = 12; a = b; 12 a b

Aliases String name = “Mickey”; String name2 = “Mickey”; name Mickey name2 Mickey

Aliases name = name2; name name2 Mickey

Strings Strings are immutable String methods –char charAt(int index) –String replace(char oldChar, char newChar)

Strings String s = “Hello” String t = s.replace('l', 'p'); s Hello t Heppo

Packages Classes in the standard Java library are grouped into packages –java.util –java.math –java.text Using the API

import To use a class from the library you must either use the fully-qualified name or import the class java.util.Scanner s = new java.util.Scanner(System.in) import java.util.Scanner; import java.util.*; //import all classes in java.util.

Java Classes Contain data and methods Methods enable user to access and modify data

Encapsulation Data should be hidden from the user –Access to/modification of data is controlled by methods Modifiers –Enable programmer to specify which data and methods are private and which are public private are accessible within the class public are accessible from anywhere

Name.java public class Name { private String first; private String last; //Constructor public Name(String thefirst, String thelast) { first = thefirst; last = thelast; } public void printName() { System.out.println("First: " + first + " Last: " + last); }

Driver Classes The driver typically contains the main method –this is the starting point for the program The driver creates instances of other classes

NameTest.java public class NameTest { public static void main(String[] args) { Name n = new Name("Sami", "Rollins"); n.printName(); }

Methods public void printName() {... } modifier return_type name(type name,...) You must specify the return type and the type of each parameter

More Method Headers For each data member, consider whether you should create setter and getter methods public String getFirstName() public void setFirstName(String newname) public String getLastName() public void setLastName(String newname)

Constructors Called when a new object is created Like a method with no return type Should initialize all relevant data Should take as input initial values for any variables that do not have a logical default value A default constructor takes no parameters public Name(String thefirst, String thelast) { first = thefirst; last = thelast; }

Scope public Name(String first, String last) { this.first = first; this.last = last; } this provide access to class variable first/last are local to the constructor