Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Similar documents
Chapter 4. Defining Classes I

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Comp 249 Programming Methodology

Comp 249 Programming Methodology Chapter 8 - Polymorphism

Copyright 2007 Pearson Addison-Wesley Copyright 2018 Aiman Hanna All rights reserved

Comp 249 Programming Methodology

Comp 249 Programming Methodology Chapter 9 Exception Handling

Copyright 2007 Pearson Addison-Wesley Copyright 2019 Aiman Hanna All rights reserved

Comp 249 Programming Methodology Chapter 11 Recursion

Chapter 4 Defining Classes I

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Chapter 5. Defining Classes II. Slides prepared by Rose Williams, Binghamton University

Objects and Classes -- Introduction

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods

More About Objects and Methods

More About Objects and Methods

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 4. Defining Classes I

Introduction to Programming Using Java (98-388)

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Object Oriented Programming in C#

Part 3. Why do we need both of them? The object-oriented programming paradigm (OOP) Two kinds of object. Important Special Kinds of Member Function

More About Objects and Methods

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

EECS168 Exam 3 Review

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Lecture 5: Methods CS2301

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

More About Objects and Methods

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Chapter 2: Using Data

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

CH. 2 OBJECT-ORIENTED PROGRAMMING

Chapter 5. Defining Classes II. Copyright 2016 Pearson Inc. All rights reserved.

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Programming II (CS300)

Programming II (CS300)

CS110D: PROGRAMMING LANGUAGE I

Java Primer 1: Types, Classes and Operators

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Fundamental Concepts and Definitions

6/4/12. Recursive void Methods. Chapter 11. Vertical Numbers. Vertical Numbers. Vertical Numbers. Algorithm for Vertical Numbers

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Full file at

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Programming 2. Object Oriented Programming. Daniel POP

Implementing Object Equivalence in Java Using the Template Method Design Pattern

PIC 20A The Basics of Java

Lecture 10 Declarations and Scope

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Anatomy of a Class Encapsulation Anatomy of a Method

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Using Classes and Objects. Chapter

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Full file at

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

CSC Java Programming, Fall Java Data Types and Control Constructs

9/10/2018 Programming Data Structures Inheritance

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

Chapter 4.!Data Abstraction: The Walls! 2011 Pearson Addison-Wesley. All rights reserved 4-1

Function Overloading

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Chapter 5 Object-Oriented Programming

What does it mean by information hiding? What are the advantages of it? {5 Marks}

COMP 250 Winter 2011 Reading: Java background January 5, 2011

In Java there are three types of data values:

Java Overview An introduction to the Java Programming Language

5/24/12. Introduction to Polymorphism. Chapter 8. Late Binding. Introduction to Polymorphism. Late Binding. Polymorphism and Abstract Classes

STUDENT LESSON A5 Designing and Using Classes

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Hello, world! 206 TUGboat, Volume 31 (2010), No. 2. resulting in: Drawing structured diagrams with SDDL Mathieu Bourgeois and Roger Villemaire

25. Interfaces. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Conversions and Overloading : Overloading

Bitwise Operators Objects and Methods

OBJECT ORIENTED SIMULATION LANGUAGE. OOSimL Reference Manual - Part 1

Data Abstraction: The Walls

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

Chapter 3 Function Overloading

Pace University. Fundamental Concepts of CS121 1

Java Programming Tutorial 1

Class, Variable, Constructor, Object, Method Questions

Objectives. Introduce static keyword examine syntax describe common uses

After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady.

PROGRAMMING LANGUAGE 2

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

CPSC 427: Object-Oriented Programming

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Transcription:

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 2007-2016 Aiman Hanna All rights reserved

Overloading 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 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 Overloading1.java (MS-Word file) 4-2

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 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 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 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 boolean types are initialized to false Other primitives are initialized to the zero of their type Class types are initialized to null However, it is a better practice to explicitly initialize instance variables in a constructor Note: Local variables are not automatically initialized 4-5

The this Parameter All instance variables are understood to have <the calling object>. in front of them 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 VehicleCompare5.java (MS-Word file) 4-6

The this Parameter 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 int somevariable = this.somevariable local instance 4-7

Information Hiding and Encapsulation 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 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 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 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 maxmiles = MetricConverter.kmToMile(maxSpeed); VehicleSearch5.java (MS-Word file) 5-9

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 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 5-10

Static Variables 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 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 A static variable is declared like an instance variable, with the addition of the modifier static private static double bestprice; VehicleSearch8.java (MS-Word file) 5-11

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

Copy Constructors 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 VehicleSearch7.java (MS-Word file) Revisit VehicleSearch8.java (MS-Word file) 5-13

The StringTokenizer Class 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 In order to use the StringTokenizer class, be sure to include the following at the start of the file: import java.util.stringtokenizer; Strings2.java (MS-Word file) 4-14

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 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 The Math class has two predefined constants, E (e, the base of the natural logarithm system) and PI (, 3.1415...) 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