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

Slides:



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

ICS 201 Inheritance Introduction to Computer Science
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.
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
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS102--Object Oriented Programming Lecture 3: – Defining classes (10 questions) – The StringTokenizer class – The Math class Copyright © 2008 Xiaoyan Li.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
AP Computer Science A – Healdsburg High School 1 Unit 3 - Objects and Classes Lab: First Steps - Example.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Slides prepared by Rose Williams, Binghamton University Chapter 4 Defining Classes I.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
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
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Defining Classes and Methods
Implementing Non-Static Features
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Defining Classes and Methods
Chapter 4 Defining Classes I
Classes and Objects Static Methods
Object Oriented Programming in java
Defining Classes and Methods
Defining Classes and Methods
Classes, Objects and Methods
Chapter 4 Constructors Section 4.4
Presentation transcript:

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

A Java Program A Java program consists of one or more classes A Java class consists of one or more methods A Java method consists of one or more statements A Java Program Java classes Java Methods

3 Diagram of program structure A program consists of one or more classes each class is in a separate.java file Program File Class Variables Constructors Methods Variables Statements

4 Classes and Source Files Each class is stored in a separate file The name of the file must be the same as the name of the class, with the extension.java public class Car {... } Car.java By convention, the name of a class (and its source file) starts with a capital letter. (In Java, all names are case-sensitive.)

5 public class SomeClass Fields Constructors Methods } Attributes / variables that define the object’s state; can hold numbers, characters, strings, other objects Procedures for constructing a new object of this class and initializing its fields Actions that an object of this class can take (behaviors) { Class header SomeClass.java import... import statements

Object-Oriented Programming (OOP) 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

Object Declaration It is possible to declare several objects from a class: Class Employee e2 e1 e3 e4 Note: classes Employee and TestClass should be saved in the same directory String s2 s1 s3 s4 The same principle as:

- 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 The following sentences are equivalent: “ e1 is of type Employee," “ e1 is an object of the class Employee," and “ e1 is an instance of the class Employee" Employee e2 e1 e3 e4

Main Class Employee Class 3. Manage object 1. Create objects from class e1 e2 e3 e4 2. objects 9

- Object Creation - new Operator The instruction : Employee e1; only declares e1. But the object is still not created. To create the object the operator new must be used: e1 = new Employee(); These can be combined as follows: Employee e1 = new Employee();

- Object Creation DeclarationCreation Question: What is the name of Employee e1? How to change the name of Employee e1? … next slide..

- Instance Variables and Methods … In order to refer to a particular instance variable, preface it with its object name as follows: objectName.instanceVar1 objectName.instanceVar2 Example: e1.name e1.age e1.salary To change the name of e1: e1.name = " Mohamed " ;

- Instance Variables and Methods … In order to invoke a method of a class, you need an object of that class: objectName.method1() objectName.method2(argument1) Example: e1.outputDetails();

Encapsulation Data Hiding Abstraction Security ؟ 14

Encapsulation allows the programmer to group data and the methods that operate on them together in one place, and to hide details from the user. In Java, hiding details is done by marking them private Encapsulation (data hiding) withDraw Deposit Transfer Print AccountNo AccountValue NameAddress Change Address withDraw any value 15

Encapsulation Person Vending Machine Buy Pepsi Sell (1 SR, Pepsi) Sell 16

Access Modifiers: public and private Instance variables and methods of a class can be declared public, or private Example: public String name; private int age; public void outputDetails{..} 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

- public and private Modifiers … Illegal because we try to access a private member (age) from outside the class Employee

Problem.. It is considered good programming practice to make all instance variables private Question: how to access and modify the instance variables of Employee objects e1, e2, e3 and e4?.. answer.. Use accessor and mutaor methods …. next slide..

- Accessor and Mutator Methods … The methods that retrieve the data of fields are called accessors. The data can be accessed but not changed The name of an accessor method starts with the word get Example: public String getName() { return name; } The methods that modify the data of fields are called mutators. The name of a mutator method starts with the word set Example: public void setName(String n) { name = n; }

- Accessor and Mutator Methods (Example) Accessor method for instance variable name Mutator method for instance variable name Modifying the name of e1 using a mutator method

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

Constructor Example Constructor

How Constructors are called A constructor is called when an object of the class is created using new ClassName objectName = new ClassName(anyArgs); Example: Employee e1 = new Employee( " Mohamed ", 20, 5000);

Constructors Constructor Calling the constructor

Include a No-Argument Constructor If you do not include any constructors in your class, Java will automatically create a default or no-argument constructor that takes no arguments, performs no initializations, but allows the object to be created If you include even one constructor in your class, Java will not provide this default constructor If you include any constructors in your class, be sure to provide your own no-argument constructor as well

No-argument constructor

The methods equals and toString The purpose of equals, a boolean valued method, is to compare two objects of the class to see if they satisfy the notion of "being equal “ Note: You cannot use == to compare objects public boolean equals(ClassName objectName) The purpose of the toString method is to return a String value that represents the data in the object public String toString()

equals example

Invoking equals method: equals invocation

toString example toString invocation

32 Class variables Vs Primitive type variables int i = 15; Employee e1 = new Employee("Mohamed", 20, 5000); i is a variable of type integer that contain the value 15. e1 is a variable of type Employee that contains the address where the object is located. The object named by the variable is stored in some other location in memory i 15  e1 “Mohamed”

33 References What happens if we do: Employee e1 = new Employee("Mohamed", 20, 5000); Employee e2 = e1; e1 and e2 will refer to the same object! The assignment operator sets the reference (memory address) of one class type variable equal to that of another Any change to the object named by one of theses variables will produce a change to the object named by the other variable, since they are the same object !!

Example What will be printed here?

35 - Class Parameters … All parameters in Java are call-by-value parameters Class type parameters appear to behave differently from primitive type parameters They appear to behave in a way similar to parameters in languages that have the call-by-reference parameter passing mechanism

Call-by-Value

Call-by-Reference What salary will printed here?

Static Methods A static method is one that can be used without a calling object. A static method still belongs to a class, and its definition is given inside the class definition. 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 returnedValue = MyClass.myMethod(arguments);

Static Variables Static variables can be declared and initialized at the same time private static int myStaticVariable = 0; If not explicitly initialized, a static variable will be automatically initialized to a default value boolean static variables are initialized to false Other primitive types static variables are initialized to the zero of their type Class type static variables are initialized to null It is always preferable to explicitly initialize static variables rather than rely on the default initialization

Static Variables A static variable should always be defined private, unless it is also a defined constant The value of a static defined constant cannot be altered, therefore it is safe to make it public In addition to static, the declaration for a static defined constant must include the modifier final, which indicates that its value cannot be changed public static final int BIRTH_YEAR = 1954; When referring to such a defined constant outside its class, use the name of its class in place of a calling object int year = MyClass.BIRTH_YEAR;

Simple Example static field static method

Example (Part 1 of 4)

Example (Part 2 of 4)

Example (Part 3 of 4)

Example (Part 4 of 4)