Introduction to Programming

Similar documents
Chapter 4 Defining Classes I

Java Object Oriented Design. CSC207 Fall 2014

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

Chapter 10 Introduction to Classes

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CS159. Nathan Sprague

Introduction to Classes and Objects

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

Object Oriented Programming

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

Answer ALL Questions. Each Question carries ONE Mark.

Cloning Enums. Cloning and Enums BIU OOP

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

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

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

BBM 102 Introduction to Programming II Spring Inheritance

Introduction to Classes and Objects

CS 2340 Objects and Design - Scala

Class Foo. instance variables. instance methods. Class FooTester. main {

The Object Concept. Object-Oriented Programming Spring 2015

Inf1-OP. Creating Classes. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Course Material Usage Rules

OOP++ CSE219, Computer Science III Stony Brook University

Inheritance and Polymorphism

COMP-202: Foundations of Programming. Lecture 14: static, private, public Jackie Cheung, Winter 2015

CLASS DESIGN. Objectives MODULE 4

1B1b Classes in Java Part I

CS1004: Intro to CS in Java, Spring 2005

CS-202 Introduction to Object Oriented Programming

Chapter 4: Writing Classes

Lecture 18 Tao Wang 1

Banaras Hindu University

TypeScript. Types. CS144: Web Applications

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

Programming II (CS300)

Computational Expression

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

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

JAVA OBJECT-ORIENTED PROGRAMMING

Ch 7 Designing Java Classes & Class structure

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

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Chapter 6 Introduction to Defining Classes

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

ECOM 2324 COMPUTER PROGRAMMING II

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Cpt S 122 Data Structures. Inheritance

Introduction to Programming Using Java (98-388)

Object Oriented Programming

Objects and Iterators

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Programming II (CS300)

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Chapter 15: Object Oriented Programming

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

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

Programming II (CS300)

Anatomy of a Class Encapsulation Anatomy of a Method

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

PIC 20A The Basics of Java

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

OO and Ahh! An Introduction to Object Oriented Programming With PHP. Division 1 Systems. John Valance. Copyright John Valance Division 1 Systems

PART 1. Eclipse IDE Tutorial. 1. What is Eclipse? Eclipse Java IDE

C++ Basic Syntax. Constructors and destructors. Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

JAVA GUI PROGRAMMING REVISION TOUR III

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Chapter 4. Defining Classes I

Distributed Systems Recitation 1. Tamim Jabban

COMP-202: Foundations of Programming. Lecture 9: Classes and Objects Sandeep Manjanna, Summer 2015

Distributed Systems Recitation 1. Tamim Jabban

COMP 430 Intro. to Database Systems. Encapsulating SQL code

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

Lecture 2: Java & Javadoc

CS 302 Week 9. Jim Williams

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

COMP 430 Intro. to Database Systems. SQL from application code

SSE3052: Embedded Systems Practice

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

HST 952. Computing for Biomedical Scientists Lecture 5

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

Xtend Programming Language

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

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

Recitation 3 Class and Objects

Pace University. Fundamental Concepts of CS121 1

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Create a Java project named week9

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

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Practice Questions for Chapter 9

OBJECT ORİENTATİON ENCAPSULATİON

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

COMP 2355 Introduction to Systems Programming

Computer Science is...

STUDENT LESSON A5 Designing and Using Classes

Java Threads and intrinsic locks

Transcription:

Introduction to Programming René Thiemann Institute of Computer Science University of Innsbruck WS 2008/2009 RT (ICS @ UIBK) Chapter 3 1/32 Outline Foundations of Object Orientation Data hiding RT (ICS @ UIBK) Chapter 3 2/32

Foundations of Object Orientation Outline Foundations of Object Orientation Data hiding RT (ICS @ UIBK) Chapter 3 3/32 Foundations of Object Orientation Storing Persons in Arrays p u b l i c c l a s s SomePersons { p u b l i c s t a t i c void main ( S t r i n g [ ] a r g s ) { S t r i n g [ ] f i r s t N a m e s = new S t r i n g [ 3 ] ; S t r i n g [ ] lastnames = new S t r i n g [ 3 ] ; double [ ] s a l a r i e s = new double [ 3 ] ;... // p e r s o n [ 0 ] = p e r s o n [ 1 ] ; f i r s t N a m e s [ 0 ] = f i r s t N a m e s [ 1 ] ; lastnames [ 0 ] = lastnames [ 1 ] ; s a l a r i e s [ 0 ] = s a l a r i e s [ 1 ] ; S t r i n g fullname = f i r s t N a m e s [0]+ " "+lastnames [ 0 ] ; RT (ICS @ UIBK) Chapter 3 4/32

Foundations of Object Orientation Object oriented version p u b l i c c l a s s Person { S t r i n g firstname, lastname ; double s a l a r i e s ; S t r i n g fullname ( ) { return f i r s t N a m e + " " + lastname ; p u b l i c c l a s s SomePersons { p u b l i c s t a t i c void main ( S t r i n g [ ] a r g s ) { Person [ ] somepersons = new Person [ 3 ] ;... somepersons [ 0 ] = somepersons [ 1 ] ; S t r i n g fullname = somepersons [ 0 ]. fullname ( ) ; RT (ICS @ UIBK) Chapter 3 5/32 Foundations of Object Orientation Classes and objects usually, we want to have several objects of a similar structure describing each of these objects uniquely is too much effort use (one) class to describe the common structure and create several objects of this class (also called instances) use one class for each structure (e.g., persons, companies,... ) class object class: (common) structure without data object: instance of class with data variables always reference objects, never classes RT (ICS @ UIBK) Chapter 3 6/32

Foundations of Object Orientation Structure of a class definition p u b l i c c l a s s ClassName { // a t t r i b u t e d e c l a r a t i o n s sometype s o m e A t t r i b u t e ; someothertype s o m e O t h e r A t t r i b u t e ;... / method / d e c l a t i o n s RT (ICS @ UIBK) Chapter 3 7/32 Foundations of Object Orientation Object oriented view variables p 1 p 2 p 3 class Person methods fullname attributes firstname lastname salary object Person methods fullname attributes "John" "Wood" 33000 object Person methods fullname attributes "Mary" "Lee" 20000 object Person methods fullname attributes "Sue" "Parker" 41000 RT (ICS @ UIBK) Chapter 3 8/32

Foundations of Object Orientation Working with classes and objects classes: each class has its own Java-file public class Person {... in Person.java public class Company {... in Company.java declaring variables: ClassName variablename; creating objects: new ClassName(); Person p1 = new Person(); Person p2 = null; Company c = new Company(); accessing the attributes of an object: o.attributename p1.firstname = "John"; p1.lastname = p1.firstname; assignments and equality: = and == boolean b = p1 == p2; b = p2 == null; p1 = p2; RT (ICS @ UIBK) Chapter 3 9/32 Outline Foundations of Object Orientation Data hiding RT (ICS @ UIBK) Chapter 3 10/32

Motivation for putting every computation in public static void main (...) allow to develop large programs use methods to structure the code structure of problem can be reflected in structure of methods reuse code easily without copy & paste better adaptation of code better testing possibilities (each method separately) does not // s o r t i n g a l g o r i t h m p u b l i c s t a t i c void s o r t ( i n t [ ] a r r ) { / code / // s o r t i n g two a r r a y s s o r t ( a ) ; s o r t ( b ) ; RT (ICS @ UIBK) Chapter 3 11/32 Structure of p u b l i c s t a t i c restype methodname ( type1 arg1,... ) { / s t a t e m e n t s / each method has one unique result type restype restype can be int, String, Company, Person[],...... or void (no value is computed) result of method is determined by return-statement: return / expr /; executing return -statement leaves the method return 5; // return int value return new Person(); // return Person return "foo"+ p.firstname; // return String return; // return nothing = void value methods must end with return-statement (except: void result type) RT (ICS @ UIBK) Chapter 3 12/32

Structure of p u b l i c s t a t i c type methodname ( type1 arg1,... ) {... there can be arbitrary many arguments type1 arg1 : the 1. argument has type type1 and is named by variable arg1 methods are called by: methodname(expr1,...) each expression must correspond to type of argument method calls of void-methods are statements method calls of type-methods are expressions of type type example: public static int f(int x, String y) {... int z = 7 f(5, "foo") + 4; // okay z = f(5); // not okay z = f(5, 7); // not okay String s = f(5, "foo"); // not okay RT (ICS @ UIBK) Chapter 3 13/32 Passing Arguments p u b l i c s t a t i c void main ( S t r i n g [ ] a r g s ) { i n t y = 4 ; i n t z = f ( y ) ; p r i n t l n ( y + "," + z ) ; p u b l i c s t a t i c i n t f ( i n t x ) { i n t r = x + 3 ; x = 2 ; return r ; outputs: RT (ICS @ UIBK) Chapter 3 14/32

Passing Arguments - Call by Value p u b l i c s t a t i c void f ( type1 arg1,... ) { arg1 = exp2 ;... p u b l i c s t a t i c void g (... ) {... f ( exp1,... ) ; the call of a method evaluates first its arguments to their values (when f is called, then exp1 is evaluated first to some value v) these values are used for the argument variables within the method (initially, arg1 has value v in method f) changes of argument variables are like changes of local variables (if exp1 = x then assignment arg1 = exp2 has no impact on x ) RT (ICS @ UIBK) Chapter 3 15/32 Illustration p u b l i c s t a t i c void main ( S t r i n g [ ] a r g s ) { i n t y = 4 ; i n t z = f ( y ) ; p r i n t l n ( y + "," + z ) ; p u b l i c s t a t i c i n t f ( i n t x ) { i n t r = x + 3 ; x = 2 ; return r ; args [] y 4 z 7 main x 42 r 7 f RT (ICS @ UIBK) Chapter 3 16/32

Passing Arguments - Call by Value changing an argument variable has no impact on calling variable special situation for reference types: changing the argument reference has no impact on calling reference changing the object referenced by argument variable has impact on (the same) object that is referenced by calling variable RT (ICS @ UIBK) Chapter 3 17/32 Illustration p u b l i c s t a t i c void main ( S t r i n g [ ] a r g s ) { Person p = new Person ( ) ; p. f i r s t N a m e = "John" ; f ( y ) ; p u b l i c s t a t i c void f ( Person x ) { x. f i r s t N a m e = "Sue" ; x = n u l l ; args [] p x null object Person methods attributes fullname "John" "Sue" null null 0.0 RT (ICS @ UIBK) Chapter 3 18/32

Method Calls: Summary Java only supports call-by-value for call somemethod(x), in the method body of somemethod x cannot be changed for primitive types x cannot be changed for reference types the object that is referenced by x can be changed this change is often desired for void-methods such as public static void sort (int [] somearray) but these changes should be documented; they might occur unexpected RT (ICS @ UIBK) Chapter 3 19/32 The keyword static so far, we only considered non-static attribute declarations class C { type attname; each person has a first name, each person has a salary,... what if we want to count how often the fullname()-method is called? p u b l i c c l a s s Person { S t r i n g f i r s t N a m e ;... i n t count ; p u b l i c S t r i n g fullname ( ) { count++; return f i r s t N a m e + " " + lastname ; problem: every Person-object has its own counter use static attributes which are stored once for each class RT (ICS @ UIBK) Chapter 3 20/32

Static attributes p u b l i c c l a s s Person { S t r i n g f i r s t N a m e ;... s t a t i c i n t count ; p u b l i c S t r i n g fullname ( ) { count++; return f i r s t N a m e + " " + lastname ; declare attributes as static using static type attributename; since static attributes are independent of objects, one does not need to create objects to access them; usage: ClassName.attributeName accessing static attributes within the class possible without class name example: code within Person-class can use Person.count or count RT (ICS @ UIBK) Chapter 3 21/32 Static and non-static methods so far nearly all methods have been static static methods can be called via class name without creating object, e.g., Array. sort (...), IO. println () in static methods access to non-static methods/attrib. only via objects p u b l i c c l a s s Person { i n t s a l a r y ;... s t a t i c i n t count ; p u b l i c S t r i n g fullname ( ) {... p u b l i c s t a t i c void main ( S t r i n g [ ] a r g s ) { i n t x = s a l a r y ; // not okay x = count ; // okay S t r i n g s = fullname ( ) ; // not okay Person p = new Person ( ) ; s = p. fullname ( ) ; // okay RT (ICS @ UIBK) Chapter 3 22/32

Static vs. non-static non-static attributes one value per object declared by omitting keyword static accessed via object static attributes globally store one value declared using keyword static accessed via class name non-static methods (of some object) have direct access to non-static attributes declared by omitting keyword static called from other non-static methods of same class or via object static methods declared using keyword static called from other methods of same class or via class name RT (ICS @ UIBK) Chapter 3 23/32 tostring () whenever a class contains the definition of the method p u b l i c S t r i n g t o S t r i n g ( ) {... then this method is called for printing an object of that class for concatenating a string with an object of that class example: p u b l i c c l a s s Person {... p u b l i c S t r i n g t o S t r i n g ( ) { return fullname ( ) + " (earns " + s a l a r y + ")" ; Person p =... ; p r i n t l n ( p ) ; S t r i n g t e x t = "The person " + p ; RT (ICS @ UIBK) Chapter 3 24/32

Validity of identifiers local variables and parameter names have priority over attributes to explicitly access static attributes, use class name to explicitly access non-static attributes, use keyword this (this can only be used within non-static methods and refers to the object from that the non-static method was called) p u b l i c c l a s s V a l i d i t y { s t a t i c i n t x ; i n t y ; s t a t i c i n t v ; i n t w; p u b l i c void f ( i n t x, i n t u ) { v = w; i n t y ; y = x ; V a l i d i t y. x = t h i s. y ; RT (ICS @ UIBK) Chapter 3 25/32 Data hiding Outline Foundations of Object Orientation Data hiding RT (ICS @ UIBK) Chapter 3 26/32

Data hiding Motivation for Data Hiding data hiding: hide internal details, provide unchanged interface to outside user of class can concentrate on interface (and is not overwhelmed by all internal attributes/methods) internals can be changed without changing external code RT (ICS @ UIBK) Chapter 3 27/32 Data hiding External code RT (ICS @ UIBK) Chapter 3 28/32

Data hiding Getters and Setters p u b l i c c l a s s Person { S t r i n g firstname, lastname ; p u b l i c S t r i n g getfirstname ( ) { return f i r s t N a m e ; p u b l i c S t r i n g getfullname ( ) { return f i r s t N a m e + " " + lastname ; p u b l i c S t r i n g setlastname ( S t r i n g lastname ) { t h i s. lastname = lastname ; p u b l i c S t r i n g setfullname ( S t r i n g fullname ) { S t r i n g [ ] f i r s t L a s t = fullname. s p l i t ( " ", 2 ) ; f i r s t N a m e = f i r s t L a s t [ 0 ] ; lastname = f i r s t L a s t [ 1 ] ; RT (ICS @ UIBK) Chapter 3 29/32 Data hiding Getters and Setters - Alternative Implementation p u b l i c c l a s s Person { S t r i n g fullname ; p u b l i c S t r i n g getfirstname ( ) { return fullname. s p l i t ( " ", 2 ) [ 0 ] ; p u b l i c S t r i n g getfullname ( ) { return fullname ; p u b l i c S t r i n g setlastname ( S t r i n g lastname ) { t h i s. fullname = getfirstname ( ) + " " + lastname ; p u b l i c S t r i n g setfullname ( S t r i n g fullname ) { t h i s. fullname = name ; RT (ICS @ UIBK) Chapter 3 30/32

Data hiding Enforcing good external code problem: so far no specification (in class Person) what is internal solution: use access modifiers public and private access modifiers can be written in front of attribute- and method-declarations private String firstname, lastname; public String getfullname() {... code within the class can access all attributes and methods code outside the class can only access public attributes and methods bad external code is refused by compiler enables clear focus for external usage: only look at public methods (and their documentation) RT (ICS @ UIBK) Chapter 3 31/32 Data hiding Summary objects can store data of different types object = data + methods to access the data classes describe common structure of similar objects use methods to reuse algorithms easily and to structure programs passing arguments using call-by-value (but be aware of references) static attributes for global usage non-static methods for algorithms that depend on object-data data hiding via public and private to distinguish internal attributes and methods from those that should be externally used easy to make internal changes afterwards RT (ICS @ UIBK) Chapter 3 32/32