Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 4 Defining Classes I Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Road Map Introduction to object oriented programming. Classes
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
CS102--Object Oriented Programming Lecture 3: – Defining classes (10 questions) – The StringTokenizer class – The Math class Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
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.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
SWE 510: Object Oriented Programming in Java1 Defining Classes 1 This slide set was compiled from the Absolute Java textbook and the instructor’s own class.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
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)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Slides prepared by Rose Williams, Binghamton University Chapter 4 Defining Classes I.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
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.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 4 Defining Classes I Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Comp 249 Programming Methodology
Comp 249 Programming Methodology
CMSC 202 Static Methods.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Comp 249 Programming Methodology
Chapter 6 Methods: A Deeper Look
Comp 249 Programming Methodology
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Comp 249 Programming Methodology
Chapter 4 Defining Classes I
Classes and Objects Static Methods
Object Oriented Programming in java
Chapter 4 Constructors Section 4.4
Information Hiding and Encapsulation Section 4.2
Presentation transcript:

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of Absolute Java 3 rd Edition by Savitch; which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is published by Pearson Education / Addison-Wesley. Copyright © 2007 Pearson Addison-Wesley Copyright © Aiman Hanna All rights reserved

Overloading Overloading is when two or more methods in the same class have the same method name Overloading is when two or more methods in the same class have the same method name To be valid, any two definitions of the method name must have different signatures To be valid, any two definitions of the method name must have different signatures A signature consists of the name of a method together with its parameter list A signature consists of the name of a method together with its parameter list Differing signatures must have different numbers and/or types of parameters Differing signatures must have different numbers and/or types of parameters 4-2 Overloading1.java (MS-Word file) Overloading1.java (MS-Word file) Overloading1.javaMS-Word file Overloading1.javaMS-Word file

Overloading and Automatic Type Conversion If Java cannot find a method signature that exactly matches a method invocation, it will try to use automatic type conversion If Java cannot find a method signature that exactly matches a method invocation, it will try to use automatic type conversion The interaction of overloading and automatic type conversion can have unintended results The interaction of overloading and automatic type conversion can have unintended results In some cases of overloading, because of automatic type conversion, a single method invocation can be resolved in multiple ways In some cases of overloading, because of automatic type conversion, a single method invocation can be resolved in multiple ways Ambiguous method invocations will produce an error in Java Ambiguous method invocations will produce an error in Java 4-3

You Can Not Overload Operators in Java Although many programming languages, such as C++, allow you to overload operators (+, -, etc.), Java does not permit this Although many programming languages, such as C++, allow you to overload operators (+, -, etc.), Java does not permit this You may only use a method name and ordinary method syntax to carry out the operations you desire You may only use a method name and ordinary method syntax to carry out the operations you desire 4-4

Default Variable Initializations Instance class variables are automatically initialized in Java Instance class variables are automatically initialized in Java boolean types are initialized to false boolean types are initialized to false Other primitives are initialized to the zero of their type Other primitives are initialized to the zero of their type Class types are initialized to null Class types are initialized to null However, it is a better practice to explicitly initialize instance variables in a constructor However, it is a better practice to explicitly initialize instance variables in a constructor Note: Local variables are not automatically initialized Note: Local variables are not automatically initialized 4-5

The this Parameter All instance variables are understood to have. in front of them All instance variables are understood to have. in front of them If an explicit name for the calling object is needed, the keyword this can be used If an explicit name for the calling object is needed, the keyword this can be used myInstanceVariable always means and is always interchangeable with this.myInstanceVariable myInstanceVariable always means and is always interchangeable with this.myInstanceVariable 4-6 VehicleCompare5.java (MS-Word file) VehicleCompare5.java (MS-Word file) VehicleCompare5.javaMS-Word file VehicleCompare5.javaMS-Word file

The this Parameter this must be used if a parameter or other local variable with the same name is used in the method this must be used if a parameter or other local variable with the same name is used in the method Otherwise, all instances of the variable name will be interpreted as local Otherwise, all instances of the variable name will be interpreted as local int someVariable = this.someVariable int someVariable = this.someVariable localinstance 4-7

Information Hiding and Encapsulation Information hiding is the practice of separating how to use a class from the details of its implementation Information hiding is the practice of separating how to use a class from the details of its implementation Abstraction is another term used to express the concept of discarding details in order to avoid information overload Abstraction is another term used to express the concept of discarding details in order to avoid information overload Encapsulation means that the data and methods of a class are combined into a single unit (i.e., a class object), which hides the implementation details Encapsulation means that the data and methods of a class are combined into a single unit (i.e., a class object), which hides the implementation details Knowing the details is unnecessary because interaction with the object occurs via a well-defined and simple interface Knowing the details is unnecessary because interaction with the object occurs via a well-defined and simple interface In Java, hiding details is done by marking them private In Java, hiding details is done by marking them private 4-8

Static Methods Sometimes, it is desired to use a function of a class without creating objects from this class. In such case, the method can be created as static Sometimes, it is desired to use a function of a class without creating objects from this class. In such case, the method can be created as static When a static method is defined, the keyword static is placed in the method header When a static method is defined, the keyword static is placed in the method header public static returnedType myMethod(parameters) {... } Static methods are invoked using the class name in place of a calling object Static methods are invoked using the class name in place of a calling object maxMiles = MetricConverter.kmToMile(maxSpeed); 5-9 VehicleSearch5.java (MS-Word file) VehicleSearch5.java (MS-Word file) VehicleSearch5.javaMS-Word file VehicleSearch5.javaMS-Word file

Pitfall: Invoking a Non-static Method Within a Static Method A static method cannot refer to an instance variable of the class, and it cannot invoke a non-static method of the class A static method cannot refer to an instance variable of the class, and it cannot invoke a non-static method of the class A static method has no this, so it cannot use an instance variable or method that has an implicit or explicit this for a calling object A static method has no this, so it cannot use an instance variable or method that has an implicit or explicit this for a calling object A static method can invoke another static method, however A static method can invoke another static method, however 5-10

Static Variables A static variable is a variable that belongs to the class as a whole, and not just to one object A static variable is a variable that belongs to the class as a whole, and not just to one object There is only one copy of a static variable per class, unlike instance variables where each object has its own copy There is only one copy of a static variable per class, unlike instance variables where each object has its own copy All objects of the class can read and change a static variable All objects of the class can read and change a static variable Although a static method cannot access an instance variable, a static method can access a static variable Although a static method cannot access an instance variable, a static method can access a static variable A static variable is declared like an instance variable, with the addition of the modifier static A static variable is declared like an instance variable, with the addition of the modifier static private static double bestPrice; 5-11 VehicleCompare6.java (MS-Word file) VehicleCompare6.java (MS-Word file) VehicleCompare6.javaMS-Word file VehicleCompare6.javaMS-Word file

Overloading Constructors Constructors can also be overloaded to provide different object creation options Constructors can also be overloaded to provide different object creation options 4-12 VehicleSearch6.java (MS-Word file) VehicleSearch6.java (MS-Word file) VehicleSearch6.javaMS-Word file VehicleSearch6.javaMS-Word file

Copy Constructors A copy constructor is a constructor with a single argument of the same type as the class A copy constructor is a constructor with a single argument of the same type as the class The copy constructor should create an object that is a separate, independent object, but with the instance variables set so that it is an exact copy of the argument object The copy constructor should create an object that is a separate, independent object, but with the instance variables set so that it is an exact copy of the argument object 5-13 VehicleSearch7.java (MS-Word file) VehicleSearch7.java (MS-Word file) VehicleSearch7.javaMS-Word file VehicleSearch7.javaMS-Word file VehicleSearch8.java (MS-Word file) VehicleSearch8.java (MS-Word file) VehicleSearch8.javaMS-Word file VehicleSearch8.javaMS-Word file

The StringTokenizer Class The StringTokenizer class is used to recover the words or tokens in a multi-word String The StringTokenizer class is used to recover the words or tokens in a multi-word String You can use whitespace characters to separate each token, or you can specify the characters you wish to use as separators You can use whitespace characters to separate each token, or you can specify the characters you wish to use as separators In order to use the StringTokenizer class, be sure to include the following at the start of the file: In order to use the StringTokenizer class, be sure to include the following at the start of the file: import java.util.StringTokenizer; import java.util.StringTokenizer; 4-14 Strings2.java (MS-Word file) Strings2.java (MS-Word file) Strings2.javaMS-Word file Strings2.javaMS-Word file

Some Methods in the StringTokenizer Class (Part 1 of 2) 4-15

Some Methods in the StringTokenizer Class (Part 2 of 2) 4-16

The Math Class The Math class provides a number of standard mathematical methods The Math class provides a number of standard mathematical methods It is found in the java.lang package, so it does not require an import statement It is found in the java.lang package, so it does not require an import statement All of its methods and data are static, therefore they are invoked with the class name Math instead of a calling object All of its methods and data are static, therefore they are invoked with the class name Math instead of a calling object The Math class has two predefined constants, E ( e, the base of the natural logarithm system) and PI ( , ) The Math class has two predefined constants, E ( e, the base of the natural logarithm system) and PI ( , ) area = Math.PI * radius * radius; 5-17

Some Methods in the Class Math (Part 1 of 5) 5-18

Some Methods in the Class Math (Part 2 of 5) 5-19

Some Methods in the Class Math (Part 3 of 5) 5-20

Some Methods in the Class Math (Part 4 of 5) 5-21

Some Methods in the Class Math (Part 5 of 5) 5-22