More About Objects and Methods

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

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.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
Chapter 51 More About Objects and Methods Chapter 5.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
More About Objects and Methods
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
Static Class Members Wrapper Classes Autoboxing Unboxing.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
10-Nov-15 Java Object Oriented Programming What is it?
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
CSC 142 Computer Science II Zhen Jiang West Chester University
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Polymorphism l Constructors.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
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.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
More About Objects and Methods
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Haidong Xue Summer 2011, at GSU
Java Programming: Guided Learning with Early Objects
Methods Chapter 6.
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
CMSC 202 Static Methods.
CSC240 Computer Science III
Object Based Programming
Inheritance Basics Programming with Inheritance
Chapter 6 Methods: A Deeper Look
Inheritance, Polymorphism, and Interfaces. Oh My
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 4 Writing Classes.
More About Objects and Methods
Computer Programming with JAVA
More About Objects and Methods
Constructors, GUI’s(Using Swing) and ActionListner
Outline Anatomy of a Class Encapsulation Anatomy of a Method
More About Objects and Methods Chapter 5 Programming with Methods
Creating and Using Classes
Presentation transcript:

More About Objects and Methods Chapter 6

Objectives Define and use constructors Write and use static variables and methods Use methods from class Math Use predefined wrapper classes Use stubs, drivers to test classes and programs

Objectives Write and use overloaded methods Define and use enumeration methods Define and use packages and import statements Add buttons and icons to applets Use event-driven programming in an applet

Constructors! Cat Example

Constructors: Outline Defining Constructors Calling Methods from Constructors Calling a Constructor from Other Constructors

Defining Constructors A special method called when instance of an object created with new Create objects Initialize values of instance variables Can have parameters To specify initial values if desired May have multiple definitions Each with different numbers or types of parameters

Defining Constructors Example class to represent pets Figure 6.1 Class Diagram for Class Pet

Defining Constructors Constructor without parameters is the default constructor Java will define this automatically if the class designer does not define any constructors If you do define a constructor, Java will not automatically define a default constructor Usually default constructors not included in class diagram

Defining Constructors Figure 6.2 A constructor returning a reference

Calling Methods from Other Constructors Constructor can call other class methods

Static! Cat Example

Static Variables & Methods: Outline Static Methods Dividing the Task of a main Method into Subtasks Adding a main Method to a class The Math Class Wrapper Classes

Static Variables Static variables are shared by all objects of a class Variables declared static final are considered constants – value cannot be changed Variables declared static (without final) can be changed Only one instance of the variable exists It can be accessed by all instances of the class

Static Variables Static variables also called class variables Contrast with instance variables Do not confuse class variables with variables of a class type Both static variables and instance variables are sometimes called fields or data members

Static Methods Some methods may have no relation to any type of object Example Compute max of two integers Convert character from upper- to lower case Static method declared in a class Can be invoked without using an object Instead use the class name

Example! Cat Example