Introduction to Programming

Size: px
Start display at page:

Download "Introduction to Programming"

Transcription

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

2 Foundations of Object Orientation Outline Foundations of Object Orientation Data hiding RT 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 UIBK) Chapter 3 4/32

3 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 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 UIBK) Chapter 3 6/32

4 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 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" object Person methods fullname attributes "Mary" "Lee" object Person methods fullname attributes "Sue" "Parker" RT UIBK) Chapter 3 8/32

5 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 UIBK) Chapter 3 9/32 Outline Foundations of Object Orientation Data hiding RT UIBK) Chapter 3 10/32

6 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 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 UIBK) Chapter 3 12/32

7 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 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 UIBK) Chapter 3 14/32

8 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 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 UIBK) Chapter 3 16/32

9 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 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 UIBK) Chapter 3 18/32

10 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 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 UIBK) Chapter 3 20/32

11 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 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 UIBK) Chapter 3 22/32

12 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 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 UIBK) Chapter 3 24/32

13 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 UIBK) Chapter 3 25/32 Data hiding Outline Foundations of Object Orientation Data hiding RT UIBK) Chapter 3 26/32

14 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 UIBK) Chapter 3 27/32 Data hiding External code RT UIBK) Chapter 3 28/32

15 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 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 UIBK) Chapter 3 30/32

16 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 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 UIBK) Chapter 3 32/32

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

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

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable? Peer Instruction 8 Classes and Objects How can multiple methods within a Java class read and write the same variable? A. Allow one method to reference a local variable of the other B. Declare a variable

More information

Chapter 10 Introduction to Classes

Chapter 10 Introduction to Classes C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this

More information

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

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects CmSc 150 Fundamentals of Computing I Lesson 28: Introduction to Classes and Objects in Java 1. Classes and Objects True object-oriented programming is based on defining classes that represent objects with

More information

CS159. Nathan Sprague

CS159. Nathan Sprague CS159 Nathan Sprague What s wrong with the following code? 1 /* ************************************************** 2 * Return the mean, or -1 if the array has length 0. 3 ***************************************************

More information

Introduction to Classes and Objects

Introduction to Classes and Objects 3 Nothing can have value without being an object of utility. Karl Marx Your public servants serve you right. Adlai E. Stevenson Knowing how to answer one who speaks, To reply to one who sends a message.

More information

CIS 110: Introduction to Computer Programming

CIS 110: Introduction to Computer Programming CIS 110: Introduction to Computer Programming Lecture 22 and 23 Objects, objects, objects ( 8.1-8.4) 11/28/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline Object-oriented programming. What is

More information

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

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth Outline CIS 110: Introduction to Computer Programming Lecture 22 and 23 Objects, objects, objects ( 8.1-8.4) Object-oriented programming. What is an object? Classes as blueprints for objects. Encapsulation

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented

More information

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

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals() Ch 7 Designing Java Classes & Class structure Classes comprise fields and methods Fields: Things that describe the class or describe instances (i.e. objects) e.g. last student number assigned, first name,

More information

Answer ALL Questions. Each Question carries ONE Mark.

Answer ALL Questions. Each Question carries ONE Mark. SECTION A (10 MARKS) Answer ALL Questions. Each Question carries ONE Mark. 1 (a) Choose the correct answer: (5 Marks) i. Which of the following is not a valid primitive type : a. char b. double c. int

More information

Cloning Enums. Cloning and Enums BIU OOP

Cloning Enums. Cloning and Enums BIU OOP Table of contents 1 Cloning 2 Integer representation Object representation Java Enum Cloning Objective We have an object and we need to make a copy of it. We need to choose if we want a shallow copy or

More information

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

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in

More information

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

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

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

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance Ch 7 Designing Java Classes & Class structure Classes comprise fields and methods Fields have data values define/describe an instance Constructors values are assigned to fields when an instance/object

More information

BBM 102 Introduction to Programming II Spring Inheritance

BBM 102 Introduction to Programming II Spring Inheritance BBM 102 Introduction to Programming II Spring 2018 Inheritance 1 Today Inheritance Notion of subclasses and superclasses protected members UML Class Diagrams for inheritance 2 Inheritance A form of software

More information

Introduction to Classes and Objects

Introduction to Classes and Objects 3 Introduction to Classes and Objects OBJECTIVES In this chapter you will learn: What classes, objects, methods and instance variables are. How to declare a class and use it to create an object. How to

More information

CS 2340 Objects and Design - Scala

CS 2340 Objects and Design - Scala CS 2340 Objects and Design - Scala Objects and Operators Christopher Simpkins chris.simpkins@gatech.edu Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 1 / 13 Classes

More information

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

Class Foo. instance variables. instance methods. Class FooTester. main { Creating classes Inf1-OP Creating Classes Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics February 26, 2018 Last time we saw how to use a class: create a

More information

The Object Concept. Object-Oriented Programming Spring 2015

The Object Concept. Object-Oriented Programming Spring 2015 The Object Concept Object-Oriented Programming 236703 Spring 2015 Identity The Basics Of Objects It must be possible to determine whether two objects are the same. State Set of characteristics Behavior

More information

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

Inf1-OP. Creating Classes. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics Inf1-OP Creating Classes Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics February 26, 2018 Creating classes Last time we saw how to use a class: create a

More information

Course Material Usage Rules

Course Material Usage Rules Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting institutions Slides not permitted for use in commercial training courses except when taught

More information

OOP++ CSE219, Computer Science III Stony Brook University

OOP++ CSE219, Computer Science III Stony Brook University OOP++ CSE219, Computer Science III Stony Brook University http://www.cs.stonybrook.edu/~cse219 What is memory? A giant array of bytes 0xffffffff Stack Segment How do we assign data to/get data from memory?

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

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

COMP-202: Foundations of Programming. Lecture 14: static, private, public Jackie Cheung, Winter 2015 COMP-202: Foundations of Programming Lecture 14: static, private, public Jackie Cheung, Winter 2015 Announcements Assignment 3 due Tue, Feb 24 at 11:59pm 2 Happy New Year! 恭喜發財! May all your code compile

More information

CLASS DESIGN. Objectives MODULE 4

CLASS DESIGN. Objectives MODULE 4 MODULE 4 CLASS DESIGN Objectives > After completing this lesson, you should be able to do the following: Use access levels: private, protected, default, and public. Override methods Overload constructors

More information

1B1b Classes in Java Part I

1B1b Classes in Java Part I 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

More information

CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 CS1004: Intro to CS in Java, Spring 2005 Lecture #13: Java OO cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Homework due next week Problem #2 revisited Constructors, revisited Remember: a

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Chapter 4: Writing Classes

Chapter 4: Writing Classes Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own

More information

Lecture 18 Tao Wang 1

Lecture 18 Tao Wang 1 Lecture 18 Tao Wang 1 Abstract Data Types in C++ (Classes) A procedural program consists of one or more algorithms that have been written in computerreadable language Input and display of program output

More information

Banaras Hindu University

Banaras Hindu University Banaras Hindu University A Course on Software Reuse by Design Patterns and Frameworks by Dr. Manjari Gupta Department of Computer Science Banaras Hindu University Lecture 5 Basic Design Patterns Basic

More information

TypeScript. Types. CS144: Web Applications

TypeScript. Types. CS144: Web Applications TypeScript Superset of JavaScript (a.k.a. JavaScript++) to make it easier to program for largescale JavaScript projects New features: types, interfaces, decorators,... All additional TypeScript features

More information

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

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes 1 CS257 Computer Science II Kevin Sahr, PhD Lecture 5: Writing Object Classes Object Class 2 objects are the basic building blocks of programs in Object Oriented Programming (OOP) languages objects consist

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

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

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods

More information

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

Introduction to Java. Handout-1d. cs402 - Spring Introduction to Java Handout-1d cs402 - Spring 2003 1 Methods (i) Method is the OOP name for function Must be declared always within a class optaccessqualifier returntype methodname ( optargumentlist )

More information

JAVA OBJECT-ORIENTED PROGRAMMING

JAVA OBJECT-ORIENTED PROGRAMMING SE2205B - DATA STRUCTURES AND ALGORITHMS JAVA OBJECT-ORIENTED PROGRAMMING Kevin Brightwell Thursday January 12th, 2017 Acknowledgements:Dr. Quazi Rahman 1 / 36 LECTURE OUTLINE Composition Inheritance The

More information

Ch 7 Designing Java Classes & Class structure

Ch 7 Designing Java Classes & Class structure Ch 7 Designing Java Classes & Class structure Classes comprise fields and methods Fields: Things that describe the class or describe instances (i.e. objects) e.g. student number, first name, last name,

More information

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

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

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces. CSM 61B Abstract Classes & Interfaces Spring 2017 Week 5: February 13, 2017 1 An Appealing Appetizer 1.1 public interface Consumable { public void consume (); public abstract class Food implements Consumable

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

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

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All Inheritance A form of software reuse in which a new class is created by absorbing an existing class s members and enriching them with

More information

ECOM 2324 COMPUTER PROGRAMMING II

ECOM 2324 COMPUTER PROGRAMMING II ECOM 2324 COMPUTER PROGRAMMING II Object Oriented Programming with JAVA Instructor: Ruba A. Salamh Islamic University of Gaza 2 CHAPTER 9 OBJECTS AND CLASSES Motivations 3 After learning the preceding

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Cpt S 122 Data Structures. Inheritance

Cpt S 122 Data Structures. Inheritance Cpt S 122 Data Structures Inheritance Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Base Classes & Derived Classes Relationship between

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented

More information

Objects and Iterators

Objects and Iterators Objects and Iterators Can We Have Data Structures With Generic Types? What s in a Bag? All our implementations of collections so far allowed for one data type for the entire collection To accommodate a

More information

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

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff and Arrays Comp Sci 1570 Introduction to C++ Outline and 1 2 Multi-dimensional and 3 4 5 Outline and 1 2 Multi-dimensional and 3 4 5 Array declaration and An array is a series of elements of the same type

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

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

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

Chapter 15: Object Oriented Programming

Chapter 15: Object Oriented Programming Chapter 15: Object Oriented Programming Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey How do Software Developers use OOP? Defining classes to create objects UML diagrams to

More information

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

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

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

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods Chapter 3 Classes Lesson page 3-1. Classes Activity 3-1-1 The class as a file drawer of methods Question 1. The form of a class definition is: public class {

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Class Encapsulation Anatomy of a Method Writing Classes Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: class definitions instance data encapsulation and

More information

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

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented Table of Contents L01 - Introduction L02 - Strings Some Examples Reserved Characters Operations Immutability Equality Wrappers and Primitives Boxing/Unboxing Boxing Unboxing Formatting L03 - Input and

More information

PIC 20A The Basics of Java

PIC 20A The Basics of Java PIC 20A The Basics of Java Ernest Ryu UCLA Mathematics Last edited: November 1, 2017 Outline Variables Control structures classes Compilation final and static modifiers Arrays Examples: String, Math, and

More information

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

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java. Classes Introduce to classes and objects in Java. Classes and Objects in Java Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes.

More information

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

OO and Ahh! An Introduction to Object Oriented Programming With PHP. Division 1 Systems. John Valance. Copyright John Valance Division 1 Systems OO and Ahh! An Introduction to Object Oriented Programming With PHP John Valance Division 1 Systems johnv@div1sys.com Copyright John Valance Division 1 Systems About John Valance 30+ years IBM midrange

More information

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

PART 1. Eclipse IDE Tutorial. 1. What is Eclipse? Eclipse Java IDE PART 1 Eclipse IDE Tutorial Eclipse Java IDE This tutorial describes the usage of Eclipse as a Java IDE. It describes the installation of Eclipse, the creation of Java programs and tips for using Eclipse.

More information

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

C++ Basic Syntax. Constructors and destructors. Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology Constructors and destructors 1 1 Department of Computer Science Poznan University of Technology 2012.10.07 / OOP Laboratory Outline 1 2 3 4 5 Outline 1 2 3 4 5 Outline 1 2 3 4 5 Outline 1 2 3 4 5 Outline

More information

JAVA GUI PROGRAMMING REVISION TOUR III

JAVA GUI PROGRAMMING REVISION TOUR III 1. In java, methods reside in. (a) Function (b) Library (c) Classes (d) Object JAVA GUI PROGRAMMING REVISION TOUR III 2. The number and type of arguments of a method are known as. (a) Parameter list (b)

More information

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

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass? 1. Overriding Methods A subclass can modify behavior inherited from a parent class. A subclass can create a method with different functionality than the parent s method but with the same: Name Return type

More information

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

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1. Overview Inf1-OOP Creating Data Types 1 Circle Class Object Default Perdita Stevens, adapting earlier version by Ewan Klein Format Strings School of Informatics January 11, 2015 HotelRoom Class More on

More information

Chapter 4. Defining Classes I

Chapter 4. Defining Classes I Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object-oriented programming (OOP) possible Programming in Java consists of defining a number of classes

More information

Distributed Systems Recitation 1. Tamim Jabban

Distributed Systems Recitation 1. Tamim Jabban 15-440 Distributed Systems Recitation 1 Tamim Jabban Office Hours Office 1004 Tuesday: 9:30-11:59 AM Thursday: 10:30-11:59 AM Appointment: send an e-mail Open door policy Java: Object Oriented Programming

More information

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

COMP-202: Foundations of Programming. Lecture 9: Classes and Objects Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 9: Classes and Objects Sandeep Manjanna, Summer 2015 Announcements Assignment 3: Due on 14 th of June at 11:30 pm. Small Software Glitch Costing Huge The spacecraft

More information

Distributed Systems Recitation 1. Tamim Jabban

Distributed Systems Recitation 1. Tamim Jabban 15-440 Distributed Systems Recitation 1 Tamim Jabban Office Hours Office 1004 Sunday, Tuesday: 9:30-11:59 AM Appointment: send an e-mail Open door policy Java: Object Oriented Programming A programming

More information

COMP 430 Intro. to Database Systems. Encapsulating SQL code

COMP 430 Intro. to Database Systems. Encapsulating SQL code COMP 430 Intro. to Database Systems Encapsulating SQL code Want to bundle SQL into code blocks Like in every other language Encapsulation Abstraction Code reuse Maintenance DB- or application-level? DB:

More information

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

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

Lecture 2: Java & Javadoc

Lecture 2: Java & Javadoc Lecture 2: Java & Javadoc CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Instance Variables or member variables or fields Declared in a class, but outside of any method, constructor or block

More information

CS 302 Week 9. Jim Williams

CS 302 Week 9. Jim Williams CS 302 Week 9 Jim Williams This Week P2 Milestone 3 Lab: Instantiating Classes Lecture: Wrapper Classes More Objects (Instances) and Classes Next Week: Spring Break Will this work? Double d = new Double(10);

More information

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

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal COMSC-051 Java Programming Part 1 Part-Time Instructor: Joenil Mistal Chapter 4 4 Moving Toward Object- Oriented Programming This chapter provides a provides an overview of basic concepts of the object-oriented

More information

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

COMP 430 Intro. to Database Systems. SQL from application code COMP 430 Intro. to Database Systems SQL from application code Some issues How to connect to database Where, what type, user credentials, How to send SQL commands How to get communicate data to/from DB

More information

SSE3052: Embedded Systems Practice

SSE3052: Embedded Systems Practice SSE3052: Embedded Systems Practice Minwoo Ahn minwoo.ahn@csl.skku.edu Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong

More information

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

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

HST 952. Computing for Biomedical Scientists Lecture 5

HST 952. Computing for Biomedical Scientists Lecture 5 Harvard-MIT Division of Health Sciences and Technology HST.952: Computing for Biomedical Scientists HST 952 Computing for Biomedical Scientists Lecture 5 Outline Recursion and iteration Imperative and

More information

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

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW Prepared by: Dr. Abdallah Mohamed, AOU-KW 1 1. Introduction 2. Objects and classes 3. Information hiding 4. Constructors 5. Some examples of Java classes 6. Inheritance revisited 7. The class hierarchy

More information

Xtend Programming Language

Xtend Programming Language Xtend Programming Language Produced by: Eamonn de Leastar (edeleastar@wit.ie) Department of Computing and Mathematics http://www.wit.ie/ Agenda Subtitle Excellent Xtend User Guide (Version 2.6) API Docs

More information

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

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology Computer Science II OO Programming Classes Scott C Johnson Rochester Institute of Technology Outline Object-Oriented (OO) Programming Review Initial Implementation Constructors Other Standard Behaviors

More information

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

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1 Assignment (1) Chapter 8 Objects and Classes Dr. Essam Halim Date: 18-3-2014 Page 1 Section 8.2 Defining Classes for Objects 1 represents an entity in the real world that can be distinctly identified.

More information

Recitation 3 Class and Objects

Recitation 3 Class and Objects 1.00/1.001 Introduction to Computers and Engineering Problem Solving Recitation 3 Class and Objects Spring 2012 1 Scope One method cannot see variables in another; Variables created inside a block: { exist

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

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

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects Administrative Stuff September 12, 2007 HW3 is due on Friday No new HW will be out this week Next Tuesday we will have Midterm 1: Sep 18 @ 6:30 7:45pm. Location: Curtiss Hall 127 (classroom) On Monday

More information

Create a Java project named week9

Create a Java project named week9 Objectives of today s lab: Through this lab, students will explore a hierarchical model for object-oriented design and examine the capabilities of the Java language provides for inheritance and polymorphism.

More information

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

Chapter 5: Classes and Objects in Depth. Introduction to methods Chapter 5: Classes and Objects in Depth Introduction to methods What are Methods Objects are entities of the real-world that interact with their environments by performing services on demand. Objects of

More information

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

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7 Administration Classes CS 99 Summer 2000 Michael Clarkson Lecture 7 Lab 7 due tomorrow Question: Lab 6.equals( SquareRoot )? Lab 8 posted today Prelim 2 in six days! Covers two weeks of material: lectures

More information

Practice Questions for Chapter 9

Practice Questions for Chapter 9 Practice Questions for Chapter 9 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) An object is an instance of a. 1) A) program B) method C) class

More information

OBJECT ORİENTATİON ENCAPSULATİON

OBJECT ORİENTATİON ENCAPSULATİON OBJECT ORİENTATİON Software development can be seen as a modeling activity. The first step in the software development is the modeling of the problem we are trying to solve and building the conceptual

More information

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

Handout 7. Defining Classes part 1. Instance variables and instance methods. Handout 7 CS180 Programming Fundamentals Spring 15 Page 1 of 8 Handout 7 Defining Classes part 1. Instance variables and instance methods. In Object Oriented programming, applications are comprised from

More information

CH. 2 OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED

More information

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

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters) Classes Classes/Objects/Interfaces (Savitch, Various Chapters) TOPICS Classes Public versus Private Static Data Static Methods Interfaces Classes are the basis of object-oriented (OO) programming. They

More information

COMP 2355 Introduction to Systems Programming

COMP 2355 Introduction to Systems Programming COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Today Class syntax, Constructors, Destructors Static methods Inheritance, Abstract

More information

Computer Science is...

Computer Science is... Computer Science is... Machine Learning Machine learning is the study of computer algorithms that improve automatically through experience. Example: develop adaptive strategies for the control of epileptic

More information

STUDENT LESSON A5 Designing and Using Classes

STUDENT LESSON A5 Designing and Using Classes STUDENT LESSON A5 Designing and Using Classes 1 STUDENT LESSON A5 Designing and Using Classes INTRODUCTION: This lesson discusses how to design your own classes. This can be the most challenging part of

More information

Java Threads and intrinsic locks

Java Threads and intrinsic locks Java Threads and intrinsic locks 1. Java and OOP background fundamentals 1.1. Objects, methods and data One significant advantage of OOP (object oriented programming) is data encapsulation. Each object

More information