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