Chapter 4. Defining Classes I

Similar documents
Chapter 4. Defining Classes I

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

Chapter 4 Defining Classes I

EECS168 Exam 3 Review

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Defining Classes and Methods

Methods and Data (Savitch, Chapter 5)

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Objects as a programming concept

Chapter 6 Introduction to Defining Classes

Programming II (CS300)

Defining Classes and Methods

5. Defining Classes and Methods

Programming II (CS300)

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

5. Defining Classes and Methods

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

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

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Defining Classes and Methods

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Methods. Methods. Mysteries Revealed

Defining Classes and Methods

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

More About Objects and Methods

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

TaxiBot New attributes Variables Math! TaxiBot

A A B U n i v e r s i t y

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

STUDENT LESSON A5 Designing and Using Classes

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

Introduction to Programming (Java) 4/12

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

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

Chapter 7. Inheritance

Java and OOP. Part 2 Classes and objects

Arrays Classes & Methods, Inheritance

Dot and Scope Resolution Operator

Chapter 5: Classes and Objects in Depth. Information Hiding

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

CLASSES AND OBJECTS IN JAVA

Classes and Objects. CGS 3416 Spring 2018

CHAPTER 7 OBJECTS AND CLASSES

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Lecture 5: Methods CS2301

Notes on Chapter Three

Chapter 10 Introduction to Classes

Introduction to Classes and Objects. David Greenstein Monta Vista High School

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

CS 335 Java Programming Controls. Fall 2007

Data Structures (list, dictionary, tuples, sets, strings)

Defining Classes I. 4.1 Class Definitions Information Hiding and Encapsulation Overloading

ENCAPSULATION AND POLYMORPHISM

Java Primer 1: Types, Classes and Operators

Chapter 15: Object Oriented Programming

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

APCS Semester #1 Final Exam Practice Problems

Object-Oriented Programming Concepts

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

1.00 Lecture 8. Using An Existing Class, cont.

Object Oriented Programming

Introduction to Programming Using Java (98-388)

Principles of Object Oriented Programming. Lecture 4

Cpt S 122 Data Structures. Introduction to C++ Part II

Introduction to Java. Handout-1d. cs402 - Spring

Object-Oriented Programming in Java

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

User Defined Classes. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

What will this print?

Class, Variable, Constructor, Object, Method Questions

Object-Oriented Programming in Java. Topic : Objects and Classes (cont) Object Oriented Design

CS 2530 INTERMEDIATE COMPUTING

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

More About Objects and Methods

Chapter 5: Classes and Objects in Depth. Introduction to methods

ECOM 2324 COMPUTER PROGRAMMING II

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.

Computer Science II (20073) Week 1: Review and Inheritance

Module 5. Arrays. Adapted from Absolute Java, Rose Williams, Binghamton University

Lecture 7: Classes and Objects CS2301

CS112 Lecture: Defining Instantiable Classes

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

M.CS201 Programming language

CHAPTER 7 OBJECTS AND CLASSES

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

Encapsulation in Java

Object Oriented Programming

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

CS-201 Introduction to Programming with Java

CS-201 Introduction to Programming with Java

Transcription:

Chapter 4 Defining Classes I

Introduction Classes are the most important language feature that make object-oriented programming (OOP) possible Programming in Java consists of defining a number of classes Every program is a class All helping software consists of classes All programmer-defined types are classes Classes are central to Java 4-2

A Class Is a Type A class is a special kind of programmer-defined type, and variables can be declared of a class type A value of a class type is called an object or an instance of the class If A is a class, then the phrases "bla is of type A," "bla is an object of the class A," and "bla is an instance of the class A" mean the same thing A class determines the types of data that an object can contain, as well as the actions it can perform 4-3

Primitive Type Values vs. Class Type Values A primitive type value is a single piece of data A class type value or object can have multiple pieces of data, as well as actions called methods All objects of a class have the same methods All objects of a class have the same pieces of data (i.e., name, type, and number) For a given object, each piece of data can hold a different value 4-4

Defining Classes 4-5

Example of Class 4-6

The new Operator An object of a class is named or declared by a variable of the class type: ClassName classvar; The new operator must then be used to create the object and associate it with its variable name: classvar = new ClassName(); These can be combined as follows: ClassName classvar = new ClassName(); 4-7

Example of using a Class 4-8

Instance Variables and Methods Instance variables can be defined as in the following two examples Note the public modifier (for now): public String instancevar1; public int instancevar2; In order to refer to a particular instance variable, preface it with its object name as follows: objectname.instancevar1 objectname.instancevar2 4-9

Instance Variables and Methods Method definitions are divided into two parts: a heading and a method body: public void mymethod() { code to perform some action and/or compute a value Body Heading } Methods are invoked using the name of the calling object and the method name as follows: classvar.mymethod(); Invoking a method is equivalent to executing the method body 4-10

More About Methods There are two kinds of methods: Methods that compute and return a value Methods that perform an action This type of method does not return a value, and is called a void method Each type of method differs slightly in how it is defined as well as how it is (usually) invoked 4-11

More About Methods A method that returns a value must specify the type of that value in its heading: public typereturned methodname(paramlist) A void method uses the keyword void in its heading to show that it does not return a value : public void methodname(paramlist) 4-12

main is a void Method A program in Java is just a class that has a main method When you give a command to run a Java program, the run-time system invokes the method main Note that main is a void method, as indicated by its heading: public static void main(string[] args) 4-13

return Statements The body of both types of methods contains a list of declarations and statements enclosed in a pair of braces public <void or typereturned> mymethod() { } declarations statements Body 4-14

return Statements The body of a method that returns a value must also contain one or more return statements A return statement specifies the value returned and ends the method invocation: return Expression; Expression can be any expression that evaluates to something of the type returned listed in the method heading 4-15

return Statements A void method need not contain a return statement, unless there is a situation that requires the method to end before all its code is executed In this context, since it does not return a value, a return statement is used without an expression: return; 4-16

Method Definitions An invocation of a method that returns a value can be used as an expression anyplace that a value of the typereturned can be used: typereturned trvariable; trvariable = objectname.methodname(); An invocation of a void method is simply a statement: objectname.methodname(); 4-17

Local Variables A variable declared within a method definition is called a local variable All variables declared in the main method are local variables All method parameters are local variables If two methods each have a local variable of the same name, they are still two entirely different variables 4-18

Parameters of a Primitive Type A parameter list provides a description of the data required by a method It indicates the number and types of data pieces needed, the order in which they must be given, and the local name for these pieces as used in the method public double mymethod(int p1, int p2, double p3) 4-19

Parameters of a Primitive Type When a method is invoked, the appropriate values must be passed to the method in the form of arguments Arguments are also called actual parameters The number and order of the arguments must exactly match that of the parameter list The type of each argument must be compatible with the type of the corresponding parameter int a=1,b=2,c=3; double result = mymethod(a,b,c); 4-20

Parameters of a Primitive Type In the preceding example, the value of each argument (not the variable name) is plugged into the corresponding method parameter This method of plugging in arguments for formal parameters is known as the call-by-value mechanism 4-21

A Formal Parameter Used as a Local Variable (Part 1 of 5) 4-22

A Formal Parameter Used as a Local Variable (Part 2 of 5) 4-23

A Formal Parameter Used as a Local Variable (Part 3 of 5) 4-24

A Formal Parameter Used as a Local Variable (Part 4 of 5) 4-25

A Formal Parameter Used as a Local Variable (Part 5 of 5) 4-26

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-27

public and private Modifiers The modifier public means that there are no restrictions on where an instance variable or method can be used The modifier private means that an instance variable or method cannot be accessed by name outside of the class It is considered good programming practice to make all instance variables private Most methods are public, and thus provide controlled access to the object Usually, methods are private only if used as helping methods for other methods in the class 4-28

Accessor and Mutator Methods Accessor methods allow the programmer to obtain the value of an object's instance variables The data can be accessed but not changed The name of an accessor method typically starts with the word get Mutator methods allow the programmer to change the value of an object's instance variables in a controlled manner Incoming data is typically tested and/or filtered The name of a mutator method typically starts with the word set 4-29

Encapsulation 4-30

A Class Has Access to Private Members of All Objects of the Class Within the definition of a class, private members of any object of the class can be accessed, not just private members of the calling object 4-31

Constructors A constructor is a special kind of method that is designed to initialize the instance variables for an object: public ClassName(anyParameters){code} A constructor must have the same name as the class A constructor has no type returned, not even void Constructors are typically overloaded 4-32

Constructors A constructor is called when an object of the class is created using new ClassName objectname = new ClassName(anyArgs); The name of the constructor and its parenthesized list of arguments (if any) must follow the new operator This is the only valid way to invoke a constructor: a constructor cannot be invoked like an ordinary method If a constructor is invoked again (using new), the first object is discarded and an entirely new object is created If you need to change the values of instance variables of the object, use mutator methods instead 4-33

Example of Constructor 4-34

Example of Constructor (cont.) 4-35

Example of Mutator methods 4-36

Example of Accessor methods 4-37