1B1b Classes in Java Part I

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

Array. Prepared By - Rifat Shahriyar

Notes on Chapter Three

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Introduction to Programming Using Java (98-388)

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

Principles of Object Oriented Programming. Lecture 4

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

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

1 Shyam sir JAVA Notes

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Java Identifiers, Data Types & Variables

Java Threads and intrinsic locks

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

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

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

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

CS 101 Fall 2006 Midterm 1 Name: ID:

1007 Imperative Programming Part II

Programming 2. Object Oriented Programming. Daniel POP

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

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

Java Object Oriented Design. CSC207 Fall 2014

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

CS260 Intro to Java & Android 03.Java Language Basics

Midterm Review Topics Java Basics and Structure Variables Branching Loops Methods Arrays. Midterm Review Kage Weiss Lab 001 TA, SPR 17

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

7. C++ Class and Object

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Java Classes & Primitive Types

Your first C++ program

CS 231 Data Structures and Algorithms, Fall 2016

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

Programming II (CS300)

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Java Classes & Primitive Types

Programming II (CS300)

Object-Oriented Programming, Iouliia Skliarova

CMSC 132: Object-Oriented Programming II

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

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

Motivation was to facilitate development of systems software, especially OS development.

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

class objects instances Fields Constructors Methods static

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

Creating Classes and Objects

Objects and Iterators

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Scope of this lecture

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

CHAPTER 7 OBJECTS AND CLASSES

Object Oriented Software Design II

JAVA MOCK TEST JAVA MOCK TEST II

Programming overview

Lecture 10 Declarations and Scope

Methods CSC 121 Spring 2017 Howard Rosenthal

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

( &% class MyClass { }

Announcements/Follow-ups

Motivation was to facilitate development of systems software, especially OS development.

Chapter 4: Writing Classes

Object-Oriented Programming

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

PIC 20A Number, Autoboxing, and Unboxing

Java: introduction to object-oriented features

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.

9 Working with the Java Class Library

Section 2.2 Your First Program in Java: Printing a Line of Text

Methods CSC 121 Fall 2016 Howard Rosenthal

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

OOSD. Introduction to JAVA. Giuseppe Lipari Scuola Superiore Sant Anna Pisa. September 29, 2010

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

OOSD. Introduction to JAVA. Giuseppe Lipari Scuola Superiore Sant Anna Pisa. September 12, 2011

OCA Java SE 7 Programmer I Certification Guide By Mela Gupta. Arrays

CHAPTER 7 OBJECTS AND CLASSES

Chapter 6 Introduction to Defining Classes

Key Differences Between Python and Java

Good Variable Names: dimensionone, dimension1 Bad Variable Names: dimension One, 1dimension

Fall 2017 CISC124 9/16/2017

Chapter 5 Names, Bindings, Type Checking, and Scopes

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

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

Defining Classes and Methods

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Java Fall 2018 Margaret Reid-Miller

Computer Components. Software{ User Programs. Operating System. Hardware

6.096 Introduction to C++ January (IAP) 2009

Full file at

Programming - 2. Common Errors

CS100J Prelim I, 29 Sept. 2003

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

C a; C b; C e; int c;

Ch. 11: References & the Copy-Constructor. - continued -

COMPUTER APPLICATIONS

Transcription:

1B1b Classes in Java Part I Agenda Defining simple classes. Instance variables and methods. Objects. Object references. 1 2 Reading You should be reading: Part I chapters 6,9,10 And browsing: Part IV chapter 30 Classes & Objects We want to design programs in terms of classes and objects. Write the classes using the programming language. Use the objects when the program runs. 3 4 Classes Reminder a class consists of: A collection of instance methods. A collection of instance variables to represent the state of an object (instance of the class). A very simple example class MyClass { private int ; private String name = Widget ; public int getcode() { return code; } public String getname() { return name; } public void setcode(int n) { code = n; } public void setname(string s) { name = s; } } 2 instance variables, 4 methods 5 6 1

Class MyClass By convention a class name always starts with a capital letter. Instance objects can be created using: As many objects as you need can be created. A MyClass Object Has two instance variables. And two methods, as declared by its class. myobj name = Hello 7 8 Objects Each object has its own private set of variables. Each object is independent. code = 3 name = Cog name = Widget code = 42 name = Wheel Private A class defines a scope. Declaring a method or variable private means that it can only be accessed within the scope of the class. This means within a method body or an instance variable initialisation expression. 9 10 Public Methods and variables declared public can be accessed by anything that has a reference to an object of the class. They form the public interface of objects of the class. The services the object can perform. Encapsulation Public and private are how encapsulation is enforced. Good practice states: Instance variables are always private. Only a minimal number of methods should be made public. 11 12 2

Questions? Type MyClass MyClass is a actually a new type. This is why declarations such as: are possible. In fact, MyClass is a User Defined Type. 13 14 Object references myobj: Object Reference name = Widget References A variable of a class type holds a reference to an object. It doesn t hold the object itself. The variable can go out of scope but the object can still exist (providing it is referenced by some other variable). One object can be referenced by many references and, hence, variables. 15 16 Class Type Assignment Assignment means storing a reference to a different object: variable1: variable2 : name = Widget code = 5 name = Bolt So, When you declare a variable of class type, you get a container that holds an object reference. Assigning an object to a variable means storing a reference to the object in the variable. variable1 = variable2; 17 18 3

Null Reference null No object is referenced, so no methods can be called. MyClass obj = null; Default value if variable not initialised. Questions? variable3 : 19 20 Calling methods Given an object reference, a method can be called: int n = myobj.getcode() ; String s = myobj.getname() ; Only methods declared* by class MyClass can be called. NullPointerException Caused by calling method on a null reference. No object, so no method can be called. If the error occurs find out why the variable is not referencing an object. * Actually inherited methods can also be called. We will find out about this later in the course. 21 22 Method parameters We know that methods can have parameters and return a result. For example: public int f(int n) { return n*2; } Object Reference Parameters You can pass an object reference as a parameter to a method: void somemethod(myclass myobj) { } A parameter variable is declared as normal. 23 24 4

Parameters & References But, consider what happens: myobj: somemethod(myobj) ; Method somemethod(myclass my) Code = 10 name = Widget Object Parameters The parameter value is an object reference, not an object. The parameter variable is initialised to hold a copy of the reference. The object is not copied. 25 26 Consequences If an object reference is passed as a parameter then: Changing the object inside the method changes the object outside the method. They are the same object! Don t forget that arrays are objects. Changing elements in an array parameter object changes the array outside the method. Remember Primitive Types? Values of primitive types (int( int,, char, long, boolean, etc.) are stored directly in variables using a binary representation. They are not objects. You can t have references to values of primitive types. But there are classes to represent values of primitive types. Class Integer, Double, Character, etc. 27 28 Primitive type parameters When a value of a primitive type is passed as a parameter, it is always copied. The parameter variable is initialised to a copy of the argument value. Call-by by-value The parameter passing mechanism used by Java is called Call-by by-value. This means that the value of a parameter is always copied and a parameter variable initialised with the copy. Objects are not passed as parameters, only references. 29 30 5

Parameter passing reminder A value is computed in a method call: obj.f(a + b); b The value is used to initialise the parameter variable of the called method: public void f(int n) ) { } Variable is passed Beware, this means the value held in the variable is passed as a parameter: obj.f(x); The variable itself is not passed. The value of the variable is not changed by the method called. But an object referenced by the variable can change. 31 32 Summary Seen how to construct very simple classes. Methods and instance variables. Object references. References and parameter passing. Call-by by-value. 33 6