1B1b. Classes in Java Part III. Review. Static. Static (2) Example. Static (3) 1B1b Lecture Slides. Copyright 2004, Graham Roberts 1

Similar documents
1B1b Classes in Java Part I

Constructors for classes

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Introduction to Programming Using Java (98-388)

Example: Fibonacci Numbers

An introduction to Java II

Recursion 1. Recursion is the process of defining something in terms of itself.

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

Chapter 4. Defining Classes I

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Practice Questions for Chapter 9

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

You must declare all variables before they can be used. Following is the basic form of a variable declaration:

Object Oriented Software Design II

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

CSC324 Principles of Programming Languages

University of Palestine. Mid Exam Total Grade: 100

2. The object-oriented paradigm!

Class, Variable, Constructor, Object, Method Questions

CS113: Lecture 4. Topics: Functions. Function Activation Records

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

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CS110D: PROGRAMMING LANGUAGE I

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

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

Notes on Chapter Three

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Tell compiler ValueType is a generic type. public: void push_back(const ValueType& elem); // rest of implementation }

A First Object. We still have another problem. How can we actually make use of the class s data?

C++ 8. Constructors and Destructors

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

Array. Prepared By - Rifat Shahriyar

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Informatik II Tutorial 6. Subho Shankar Basu

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

Instance Members and Static Members

CS 351 Design of Large Programs Singleton Pattern

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Example: Count of Points

Programming Languages and Techniques (CIS120)

Conversions and Overloading : Overloading

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Primitive Data and Objects

CS180 Recitation. More about Objects and Methods

CS 2530 INTERMEDIATE COMPUTING

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

Java Identifiers, Data Types & Variables

Class 9: Static Methods and Data Members

The return Statement

Object-oriented programming in Java. Sept 19, 2016 Sprenkle - CSCI Object References. weight = height = Chicken. weight = height = name =

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Fundamental Concepts and Definitions

CS260 Intro to Java & Android 03.Java Language Basics

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

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

OBJECT ORIENTED PROGRAMMING

Objects and Iterators

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here:

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

More on Objects and Classes

COMPUTER APPLICATIONS

COMP-202 More Complex OOP

(a) Assume that in a certain country, tax is payable at the following rates:

Defining Classes and Methods

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

COE318 Lecture Notes Week 3 (Sept 19, 2011)

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

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

Lecture 10 Declarations and Scope

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

Final Exam CS 251, Intermediate Programming December 13, 2017

Informatik II (D-ITET) Tutorial 6

ENCAPSULATION AND POLYMORPHISM

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

And Even More and More C++ Fundamentals of Computer Science

Association, Aggregation and Composition. Software Engineering CITS1220

9 Working with the Java Class Library

Remedial classes. G51PRG: Introduction to Programming Second semester Lecture 2. Plan of the lecture. Classes and Objects. Example: Point class

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

Transcription:

1B1b Classes in Java Part III Review We have seen that classes: declare instance variables. declare methods. may have constructors. Now want to start filling in further details. 1 2 Static Why are some methods and variables declared as static? Static (2) It depends on whether variables or methods belong to the class or to instance objects of the class. 3 4 Static (3) Non-static variables are instance variables. Each object gets its own copy of each variable. Static variables are class variables. A single copy of each variable exists and can be accessed by any other method in the class. class Test private int instancevar; private static int classvar; Example Count number of times a method is called for all instance objects of a class. private static int count = 0; public void f() count++; // Rest of method 5 6 1

final Public static variables are often used to create symbolic constants. E.g., Math.PI Such variables are additionally declared final: public static final double PI = 3.141 The value of a final variable cannot be changed by assignment. Static (4) Non-static methods are instance methods. An instance method must be called for an object of the class. x.method(args); Static methods are class methods. A static method can be called by any method declared by the class, or any method at all if public. 7 8 Example Singleton Static (5) private static MyClass instance; private MyClass() public static MyClass getinstance() if (instance == null) instance = new MyClass(); return instance; public class X private int i; public void f() X xa = new X(); X xb = new X(); xa.f(); xb.f(); xa: int i f() xb: int i f() 9 10 Static (6) Questions public class X private static int i; public static void f() X: xa: X xa = new X(); X xb = new X(); xa.f(); f() xb: xb.f(); X.f(); Can think of this as a class object. int i 11 12 2

Classes and Program Structure A program consists of a collection of classes. Those classes define the abstract structure of the program in terms of the relationships between the classes. When the program is run, the relationships are realised by object references. Representing Associations An association between two classes typically means that an object of one class has a reference to an object of another class. Country Has-Capital City 13 14 Association (2) class Country private City capital;... Association (3) The type used to represent the association needs to be determined correctly. For example: Board 1 64 Square 15 16 Association (4) class Board private Square[] squares = new Square[64]; // or // ArrayList squares = new ArrayList();... Association (5) Aggregation/Compositions are treated in the same way but we may need to be careful about sharing objects. Message List Manages 0..* Message 17 18 3

Association (6) class MessageList private List messages = new ArrayList();... Associations (7) Employee name : String age : int department : String getname() getage() getdepartment() WorksFor 0..* 1 Employer companyname : String getemployee(name) addemployee(name, age, department) 19 20 Questions Initialisation We have seen that constructors can be used to initialise instance variables. Both class and instance variables can also be directly initialised by initialisation expressions. private int x = 2; 21 22 Initialisation (2) And also by an initialiser block: private Stack x; x = new Stack(); x.push(1); x.push(2); A static initialiser block can be used for static variables. private static Stack x; static x = new Stack(); x.push(1); x.push(2); Choosing 3 ways to initialise - how do you choose? No single answer but: aim to initialise a variable as close to the point of declaration as possible. or group all initialisation into the constructor, so it is all in the same place. 23 24 4

More than one constructor A class can have more than one constructor. Each can be used to initialise objects in a specific way. But won t all the constructors have the same name? Yes. Overloading Two or more methods or constructors can have the same name. But must have different arguments. String() String(byte[]) String(char[]) String(String) String(byte[], int) 25 26 Overloading (2) Return types are not considered: int f(int) float f(int) ) // Error int f(int,int) ) // OK float f(int,, float) // OK int f() // OK The compiler determines which method to call by matching the argument types. this thisis special variable that is automatically declared in an instance method. It is a reference to the object the method was called for. Allows you to refer directly to the current object. 27 28 this (2) this (3) class T private Thing t; public int f(int x) t.dosomething(this); class T private int x; public int f(int x) this.x = x; 29 30 5

this (4) Can also be used to call a different overloaded constructor: // This constructor does the real work T(int x, int y, String z)... T() // Supply default values this(0,0, Hello ); // no duplication of // init code Summary Looked at various details of the construction and use of classes. Overloading is a new variety of abstraction. Lots of details for the programmer to know about and use carefully. 31 32 6