CSC 1214: Object-Oriented Programming

Size: px
Start display at page:

Download "CSC 1214: Object-Oriented Programming"

Transcription

1 CSC 1214: Object-Oriented Programming J. Kizito Makerere University www: materials: e-learning environment: office: block A, level 3, department of computer science alt. office: institute of open, distance, and elearning, room 2 Classes, Using Objects, and Cleanup Kizito (Makerere University) CSC 1214 February, / 23

2 Overview 1 Classes 2 Objects 3 Initialization 4 Cleanup 5 Access control Kizito (Makerere University) CSC 1214 February, / 23

3 Classes Classes Introduction A class is a template for an object An object is an instance of a class A class defines a new type which can be used to create objects of that type Declared by use of the class keyword Definition specifies the data it contains and the code that operates on that data Think of a class as a plan for a house. Before you build a house, you should have a plan. Once you have a plan, however, you can build any number of houses that follow that plan. An object has behaviours i.e., what it can do (what can be done to it) Kizito (Makerere University) CSC 1214 February, / 23

4 Classes Classes General Form class classname { type instance-variable1; type instance-variable2; //... type instance variablen; type methodname1(parameter-list) { // body of method type methodname2(parameter-list) { // body of method //... type methodnamen(parameter-list) { // body of method Kizito (Makerere University) CSC 1214 February, / 23

5 Classes Classes Members of the Class Data, or variables, defined within a class are called instance variables Methods are groups of related statements in a class that perform a specific task i.e., The code that operates on the data is contained within methods A simple class class Box { double width; double height; double depth; The methods and the code are called members of the class The new data type here is called Box Box mybox = new Box(); // create a Box object Members of an object can be accessed using the dot (.) operator mybox.width = 100; Kizito (Makerere University) CSC 1214 February, / 23

6 Classes Classes Members of the Class Data, or variables, defined within a class are called instance variables Methods are groups of related statements in a class that perform a specific task i.e., The code that operates on the data is contained within methods A simple class class Box { double width; double height; double depth; The methods and the code are called members of the class The new data type here is called Box Box mybox = new Box(); // create a Box object Members of an object can be accessed using the dot (.) operator mybox.width = 100; Kizito (Makerere University) CSC 1214 February, / 23

7 Objects Objects Declaring objects class Box { double width; double height; double depth; Two steps involved 1 Box mybox; // declare reference to object 2 mybox = new Box(); // allocates an object Can be combined into one statement Box mybox = new Box(); Kizito (Makerere University) CSC 1214 February, / 23

8 Objects Methods class Box { double width; double height; double depth; void volume() { System.out.print("Volume is "); System.out.println(width*height*depth); Kizito (Makerere University) CSC 1214 February, / 23

9 Objects Objects Methods (returning a value) class Box { double width; double height; double depth; double volume() { double vol = width * height * depth; return vol; Kizito (Makerere University) CSC 1214 February, / 23

10 Objects Methods (returning a value) Smarter class Box { double width; double height; double depth; double volume() { return width * height * depth; Kizito (Makerere University) CSC 1214 February, / 23

11 Objects Objects Methods can take parameters int square(int i) { return i * i; square() will return the square of whatever value it is called with Example int x, y, z; x = square(5); // x equals 25 y = square(9); // y equals 81 z = square(y); // z equals 4 Note that the syntax of calling it varies from that of C since the method will now be contained in some class Kizito (Makerere University) CSC 1214 February, / 23

12 Initialization Initialization Java initializes all class member variables to zero whenever we create an object Java allows us to write constructors, special methods, one of which will be called on object creation Using constructors, we can initialize class members to values of our choice Kizito (Makerere University) CSC 1214 February, / 23

13 Initialization Initialization Constructors A constructor initializes an object upon creation Has the same name as the class in which it resides Syntactically similar to a method but has no return type, not even void Automatically called immediately after the object is created, before the new operator completes Like methods, constructors can be parameterized Kizito (Makerere University) CSC 1214 February, / 23

14 Initialization Constructors Example (1) class Box { double width; double height; double depth; Box(double w, double h, double d) { width = w; height = h; this.depth = d; // redundant use of this double volume() { return width * height * depth; Kizito (Makerere University) CSC 1214 February, / 23

15 Initialization Constructors Example (2) Proper use of this class Box { double width; double height; double depth; Box(double width, double height, double depth) { this.width = width; this.height = height; this.depth = depth; double volume() { return width * height * depth; Kizito (Makerere University) CSC 1214 February, / 23

16 Initialization Constructors The default constructor How come even when I don t write constructors, my program runs? The compiler will write one for you! But only if you haven t written any constructors at all (for this class) A default constructor has no arguments (but still has the same name as the class) Kizito (Makerere University) CSC 1214 February, / 23

17 Initialization Creating Objects Example (3) DemoBox.java 1. class DemoBox { 2. public static void main(string args[]) { 3. Box mybox1 = new Box(10, 20, 15); 4. Box mybox2 = new Box(2, 3, 4); 5. Double vol; vol = mybox1.volume(); 8. System.out.println("Volume of mybox1 is " + vol); vol = mybox2.volume(); 11. System.out.println("Volume of mybox2 is " + vol); Output: Volume of mybox1 is Volume of mybox2 is 24.0 Kizito (Makerere University) CSC 1214 February, / 23

18 Initialization Example (4) The Stack Class Stack.java 1. // this class defines an integer stack that can hold 10 values 2. class Stack { 3. int tos, stck[] = new int[10]; Stack() { tos = -1; // initialize top-of-stack void push(int item) { // push an item onto the stack 8. if (tos==9) 9. System.out.println("Stack is full."); 10. else 11. stck[++tos] = item; int pop() { // pop an item from the stack 15. if (tos < 0) { 16. System.out.println("Stack underflow."); 17. return 0; 18. else 19. return stck[tos--]; Kizito (Makerere University) CSC 1214 February, / 23

19 Initialization Example (4) The Stack Class Tester Test.java 1. class Test { 2. public static void main(string args[]) { 3. Stack mystack1 = new Stack(); 4. Stack mystack2 = new Stack(); // push some numbers onto the stack 7. for (int i = 0; i < 10; i++) mystack1.push(i); 8. for (int i = 10; i < 20; i++) mystack2.push(i); // pop those numbers from the stack 11. System.out.println("Stack in mystack1:"); 12. for (int i = 0; i < 10; i++) 13. System.out.println(mystack1.pop()); System.out.println("Stack in mystack2:"); 16. for (int i = 0; i < 10; i++) 17. System.out.println(mystack2.pop()); Kizito (Makerere University) CSC 1214 February, / 23

20 Initialization The test program generates the following output: The Stack Class Example Output Stack in mystack1: Stack in mystack2: Kizito (Makerere University) CSC 1214 February, / 23

21 Initialization Initialization Static Methods class A { void meth1() { /* implementation */ static void meth2() { /* implementation */ class Test { public static void main(string args[]){ A obj = new A(); obj.meth1(); // Ok obj.meth2(); // Illegal, meth2() is a class method A.meth2(); // Ok A.meth1(); // Illegal, meth1 is not a class method Kizito (Makerere University) CSC 1214 February, / 23

22 Cleanup Example Cleanup (Garbage collection) In some languages like C and C++, dynamically allocated objects must be manually released Java handles de-allocation automatically When no reference exists, the memory is reclaimed Garbage collection occurs during the execution of the program Java run-time implementations take varying approaches to garbage collection String name1 = new String("John Mary"); String name2 = new String("Michael Smith"); name2 = name1; // no more reference to Michael Smith object Kizito (Makerere University) CSC 1214 February, / 23

23 Cleanup Example Cleanup (Garbage collection) In some languages like C and C++, dynamically allocated objects must be manually released Java handles de-allocation automatically When no reference exists, the memory is reclaimed Garbage collection occurs during the execution of the program Java run-time implementations take varying approaches to garbage collection String name1 = new String("John Mary"); String name2 = new String("Michael Smith"); name2 = name1; // no more reference to Michael Smith object Kizito (Makerere University) CSC 1214 February, / 23

24 Cleanup Cleanup The finalize() method Necessary for objects that need to perform some actions when it is destroyed Although Java supports constructors, it does not have destructors. Instead, it has a finalize() function protected void finalize() { // finalization code goes here... The keyword protected (discussed on next slide) is a specifier that prevents access to finalize() by code defined outside its class Kizito (Makerere University) CSC 1214 February, / 23

25 Access control Access control private, public, and protected Three access specifiers: private, public, and protected private: can only be accessed by other members of its class public: can be accessed by any other code in the program protected: like private but only applies when inheritance is involved Variables and methods declared with public visibility are inherited; those with private visibility are not The protected modifier allows a member of a superclass to be inherited into a subclass We shall discuss inheritance in the next Lecture Default: public in the same package Kizito (Makerere University) CSC 1214 February, / 23

BoxDemo1.java jueves, 02 de diciembre de :31

BoxDemo1.java jueves, 02 de diciembre de :31 BoxDemo1.java jueves, 0 de diciembre de 0 13:31 1 package boxdemo1; 3 class Box { 4 double width; 5 double height; 6 double depth; 7 } 8 9 // This class declares an object of type Box. class BoxDemo1 {

More information

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani Introducing Classes A class defines a new data type (User defined data type). This

More information

1SFWJFX FYDMVTJWF FYDFSQUT GSPN CSBOE OFX BOE GPSUIDPNJOH 0SBDMF 1SFTT +BWB +%, CPPLT

1SFWJFX FYDMVTJWF FYDFSQUT GSPN CSBOE OFX BOE GPSUIDPNJOH 0SBDMF 1SFTT +BWB +%, CPPLT TM 1SFWJFX FYDMVTJWF FYDFSQUT GSPN CSBOE OFX BOE GPSUIDPNJOH 0SBDMF 1SFTT +BWB +%, CPPLT 'FBUVSJOH BO JOUSPEVDUJPO CZ CFTUTFMMJOH QSPHSBNNJOH BVUIPS )FSC 4DIJMEU 8SJUUFO CZ MFBEJOH +BWB FYQFSUT 0SBDMF

More information

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

Keyword this. Can be used by any object to refer to itself in any class method Typically used to Keyword this Can be used by any object to refer to itself in any class method Typically used to Avoid variable name collisions Pass the receiver as an argument Chain constructors Keyword this Keyword this

More information

2. Introducing Classes

2. Introducing Classes 1 2. Introducing Classes Class is a basis of OOP languages. It is a logical construct which defines shape and nature of an object. Entire Java is built upon classes. 2.1 Class Fundamentals Class can be

More information

ANNAMACHARYA INSTITUTE OF TECHNOLOGY AND SCIENCES: : TIRUPATI

ANNAMACHARYA INSTITUTE OF TECHNOLOGY AND SCIENCES: : TIRUPATI ANNAMACHARYA INSTITUTE OF TECHNOLOGY AND SCIENCES: : TIRUPATI Venkatapuram(Village),Renigunta(Mandal),Tirupati,Chitoor District, Andhra Pradesh-517520. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING NAME

More information

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3 Class A class is a template that specifies the attributes and behavior of things or objects. A class is a blueprint or prototype from which objects are created. A class is the implementation of an abstract

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

Chapter 11. Abstract Data Types and Encapsulation Concepts

Chapter 11. Abstract Data Types and Encapsulation Concepts Chapter 11 Abstract Data Types and Encapsulation Concepts The Concept of Abstraction An abstraction is a view or representation of an entity that includes only the most significant attributes The concept

More information

Arrays Classes & Methods, Inheritance

Arrays Classes & Methods, Inheritance Course Name: Advanced Java Lecture 4 Topics to be covered Arrays Classes & Methods, Inheritance INTRODUCTION TO ARRAYS The following variable declarations each allocate enough storage to hold one value

More information

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Inheritance Consider a new type Square. Following how we declarations for the Rectangle and Circle classes we could declare it as follows:

More information

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

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

1.00 Lecture 8. Using An Existing Class, cont.

1.00 Lecture 8. Using An Existing Class, cont. .00 Lecture 8 Classes, continued Reading for next time: Big Java: sections 7.9 Using An Existing Class, cont. From last time: is a Java class used by the BusTransfer class BusTransfer uses objects: First

More information

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario The Story So Far... Classes as collections of fields and methods. Methods can access fields, and

More information

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts ISBN 0-321-49362-1 Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language

More information

Unit 3 INFORMATION HIDING & REUSABILITY. -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method

Unit 3 INFORMATION HIDING & REUSABILITY. -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method Unit 3 INFORMATION HIDING & REUSABILITY -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method Inheritance Inheritance is one of the cornerstones of objectoriented programming

More information

Topic 6: Inner Classes

Topic 6: Inner Classes Topic 6: Inner Classes What's an inner class? A class defined inside another class Three kinds: inner classes static nested classes anonymous classes this lecture: Java mechanisms later: motivation & typical

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

Abstract Data Types and Encapsulation Concepts

Abstract Data Types and Encapsulation Concepts Abstract Data Types and Encapsulation Concepts The Concept of Abstraction An abstraction is a view or representation of an entity that includes only the most significant attributes The concept of abstraction

More information

JAVA. Lab-9 : Inheritance

JAVA. Lab-9 : Inheritance JAVA Prof. Navrati Saxena TA- Rochak Sachan Lab-9 : Inheritance Chapter Outline: 2 Inheritance Basic: Introduction Member Access and Inheritance Using super Creating a Multilevel Hierarchy Method Overriding

More information

Roll Number. Common to II Year B.E.CSE & EIE INTERNAL ASSESSMENT TEST - I. Credit 3 R-2017

Roll Number. Common to II Year B.E.CSE & EIE INTERNAL ASSESSMENT TEST - I. Credit 3 R-2017 Roll Number ERODE SENGTHAR ENGINEERING COLLEGE PERDURI, ERODE 638 057 (Accredited by NBA, Accredited by NAAC with A grade, Accredited by IE (I), Kolkotta, Permanently affiliated to Anna University, Chennai

More information

Implementing Abstractions

Implementing Abstractions Implementing Abstractions Pointers A pointer is a C++ variable that stores the address of an object. Given a pointer to an object, we can get back the original object. Can then read the object's value.

More information

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes CS/ENGRD 2110 FALL 2017 Lecture 7: Interfaces and Abstract Classes http://courses.cs.cornell.edu/cs2110 1 Announcements 2 A2 is due tomorrow night (17 February) Get started on A3 a method every other day.

More information

10. Abstract Data Types

10. Abstract Data Types 10. Abstract Data Types 11.1 The Concept of Abstraction The concept of abstraction is fundamental in programming Nearly all programming languages support process abstraction with subprograms Nearly all

More information

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes CS/ENGRD 2110 SPRING 2019 Lecture 7: Interfaces and Abstract Classes http://courses.cs.cornell.edu/cs2110 1 Announcements 2 A2 is due Thursday night (14 February) Go back to Lecture 6 & discuss method

More information

Hierarchical abstractions & Concept of Inheritance:

Hierarchical abstractions & Concept of Inheritance: UNIT-II Inheritance Inheritance hierarchies- super and subclasses- member access rules- super keyword- preventing inheritance: final classes and methods- the object class and its methods Polymorphism dynamic

More information

Inheritance. Transitivity

Inheritance. Transitivity Inheritance Classes can be organized in a hierarchical structure based on the concept of inheritance Inheritance The property that instances of a sub-class can access both data and behavior associated

More information

Implementing Subprograms

Implementing Subprograms Implementing Subprograms 1 Topics The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing Subprograms with Stack-Dynamic Local Variables Nested Subprograms Blocks Implementing

More information

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). UNITII Classes Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). It s a User Defined Data-type. The Data declared in a Class are called Data- Members

More information

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

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides 1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of

More information

PROGRAMMING LANGUAGE 2

PROGRAMMING LANGUAGE 2 31/10/2013 Ebtsam Abd elhakam 1 PROGRAMMING LANGUAGE 2 Java lecture (7) Inheritance 31/10/2013 Ebtsam Abd elhakam 2 Inheritance Inheritance is one of the cornerstones of object-oriented programming. It

More information

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting Overview ITI 1121. Introduction to Computing II Rafael Falcon and Marcel Turcotte (with contributions from R. Holte) Electrical Engineering and Computer Science University of Ottawa Interface Abstract

More information

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at  Interfaces. Today Book-keeping Inheritance Subscribe to sipb-iap-java-students Interfaces Slides and code at http://sipb.mit.edu/iap/java/ The Object class Problem set 1 released 1 2 So far... Inheritance Basic objects,

More information

Chapter 11. Abstract Data Types and Encapsulation Concepts

Chapter 11. Abstract Data Types and Encapsulation Concepts Chapter 11 Abstract Data Types and Encapsulation Concepts Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language Examples Parameterized

More information

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Internal Examination 1 Answer Key DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Branch & Sec : CSE Date : 08.08.2014 Semester : V Sem Max Marks : 50 Marks Sub Code& Title : CS2305 Programming Paradigms

More information

About This Lecture. Data Abstraction - Interfaces and Implementations. Outline. Object Concepts. Object Class, Protocol and State.

About This Lecture. Data Abstraction - Interfaces and Implementations. Outline. Object Concepts. Object Class, Protocol and State. Revised 01/09/05 About This Lecture Slide # 2 Data Abstraction - Interfaces and Implementations In this lecture we will learn how Java objects and classes can be used to build abstract data types. CMPUT

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ Abstract Data Types and Encapsulation Concepts ISBN 0-321-33025-0 Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract

More information

CS/ENGRD 2110 SPRING 2018

CS/ENGRD 2110 SPRING 2018 CS/ENGRD 2110 SPRING 2018 Lecture 7: Interfaces and http://courses.cs.cornell.edu/cs2110 1 2 St Valentine s Day! It's Valentines Day, and so fine! Good wishes to you I consign.* But since you're my students,

More information

7. C++ Class and Object

7. C++ Class and Object 7. C++ Class and Object 7.1 Class: The classes are the most important feature of C++ that leads to Object Oriented programming. Class is a user defined data type, which holds its own data members and member

More information

Lecture 6 Introduction to Objects and Classes

Lecture 6 Introduction to Objects and Classes Lecture 6 Introduction to Objects and Classes Outline Basic concepts Recap Computer programs Programming languages Programming paradigms Object oriented paradigm-objects and classes in Java Constructors

More information

Java Memory Management

Java Memory Management Java Memory Management Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each code (classes and interfaces) objects running methods 1-2 Memory Areas

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

Abstract data types &

Abstract data types & Abstract Data Types & Object-Oriented Programming COS 301 - Programming Languages Chapters 11 & 12 in the book Slides are heavily based on Sebesta s slides for the chapters, with much left out! Abstract

More information

Abstract Data Types & Object-Oriented Programming

Abstract Data Types & Object-Oriented Programming Abstract Data Types & Object-Oriented Programming COS 301 - Programming Languages Chapters 11 & 12 in the book Slides are heavily based on Sebesta s slides for the chapters, with much left out! Abstract

More information

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc. Chapter 13 Object Oriented Programming Contents 13.1 Prelude: Abstract Data Types 13.2 The Object Model 13.4 Java 13.1 Prelude: Abstract Data Types Imperative programming paradigm Algorithms + Data Structures

More information

Chapter 11. Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构. 孟小亮 Xiaoliang MENG, 答疑 ISBN

Chapter 11. Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构. 孟小亮 Xiaoliang MENG, 答疑   ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构 孟小亮 Xiaoliang MENG, 答疑 EMAIL: 1920525866@QQ.COM ISBN 0-321-49362-1 Chapter 11 Topics The Concept of Abstraction Introduction to Data

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Interface Abstract data types Version of January 26, 2013 Abstract These lecture notes are meant

More information

Data Structures G5029

Data Structures G5029 Data Structures G5029 Lecture 2 Kingsley Sage Room 5C16, Pevensey III khs20@sussex.ac.uk University of Sussex 2006 Lecture 2 Stacks The usual analogy is the stack of plates. A way of buffering a stream

More information

Chapter 11. Abstract Data Types and Encapsulation Concepts

Chapter 11. Abstract Data Types and Encapsulation Concepts Chapter 11 Abstract Data Types and Encapsulation Concepts Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language Examples Parameterized

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

JAVA: A Primer. By: Amrita Rajagopal

JAVA: A Primer. By: Amrita Rajagopal JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently

More information

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year Object Oriented Programming Assistant Lecture Omar Al Khayat 2 nd Year Syllabus Overview of C++ Program Principles of object oriented programming including classes Introduction to Object-Oriented Paradigm:Structures

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

C++ Inheritance and Encapsulation

C++ Inheritance and Encapsulation C++ Inheritance and Encapsulation Protected members Inheritance Type Public Inheritance Private Inheritance Protected Inheritance Special method inheritance Private vs. Protected Private: private members

More information

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() { Scoping, Static Variables, Overloading, Packages In this lecture, we will examine in more detail the notion of scope for variables. We ve already indicated that variables only exist within the block they

More information

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects CS256 Computer Science I Kevin Sahr, PhD Lecture 11: Objects 1 Programs as Models remember: we write programs to solve realworld problems programs act as models of the real-world problem to be solved one

More information

Object Oriented Programming 2015/16. Final Exam June 28, 2016

Object Oriented Programming 2015/16. Final Exam June 28, 2016 Object Oriented Programming 2015/16 Final Exam June 28, 2016 Directions (read carefully): CLEARLY print your name and ID on every page. The exam contains 8 pages divided into 4 parts. Make sure you have

More information

CMSC 4023 Chapter 11

CMSC 4023 Chapter 11 11. Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language Examples Parameterized Abstract Data Types Encapsulation Constructs Naming Encapsulations

More information

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed 1 1 COMP200 ABSTRACT CLASSES OOP using Java, from slides by Shayan Javed Abstract Classes 2 3 From the previous lecture: public class GeometricObject { protected String Color; protected String name; protected

More information

Notes on Chapter Three

Notes on Chapter Three Notes on Chapter Three Methods 1. A Method is a named block of code that can be executed by using the method name. When the code in the method has completed it will return to the place it was called in

More information

Static, Final & Memory Management

Static, Final & Memory Management Static, Final & Memory Management The static keyword What if you want to have only one piece of storage regardless of how many objects are created or even no objects are created? What if you need a method

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

Data Structures using OOP C++ Lecture 3

Data Structures using OOP C++ Lecture 3 References: th 1. E Balagurusamy, Object Oriented Programming with C++, 4 edition, McGraw-Hill 2008. 2. Robert L. Kruse and Alexander J. Ryba, Data Structures and Program Design in C++, Prentice-Hall 2000.

More information

A Formal Presentation On Writing Classes With Rules and Examples CSC 123 Fall 2018 Howard Rosenthal

A Formal Presentation On Writing Classes With Rules and Examples CSC 123 Fall 2018 Howard Rosenthal A Formal Presentation On Writing Classes With Rules and Examples CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Defining a Class Defining Instance Variables Writing Methods The Object Reference this The

More information

CS453 CLASSES, VARIABLES, ASSIGNMENTS

CS453 CLASSES, VARIABLES, ASSIGNMENTS CS453 CLASSES, VARIABLES, ASSIGNMENTS CS453 Lecture Code Generation for Classes 1 PA6 new in MeggyJava member / instance variables local variables assignments let s go check out the new MeggyJava grammar

More information

VIRTUAL FUNCTIONS Chapter 10

VIRTUAL FUNCTIONS Chapter 10 1 VIRTUAL FUNCTIONS Chapter 10 OBJECTIVES Polymorphism in C++ Pointers to derived classes Important point on inheritance Introduction to virtual functions Virtual destructors More about virtual functions

More information

C++ (classes) Hwansoo Han

C++ (classes) Hwansoo Han C++ (classes) Hwansoo Han Inheritance Relation among classes shape, rectangle, triangle, circle, shape rectangle triangle circle 2 Base Class: shape Members of a class Methods : rotate(), move(), Shape(),

More information

We will work with Turtles in a World in Java. Seymour Papert at MIT in the 60s. We have to define what we mean by a Turtle to the computer

We will work with Turtles in a World in Java. Seymour Papert at MIT in the 60s. We have to define what we mean by a Turtle to the computer Introduce Eclipse Create objects in Java Introduce variables as object references Aleksandar Stefanovski CSCI 053 Department of Computer Science The George Washington University Spring, 2010 Invoke methods

More information

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Fundamentals of Programming. Lecture 19 Hamed Rasifard Fundamentals of Programming Lecture 19 Hamed Rasifard 1 Outline C++ Object-Oriented Programming Class 2 C++ C++ began as an expanded version of C. C++ improves on many of C s features and provides object-oriented-programming

More information

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005 Introduction to C++ Part II Søren Debois Department of Theoretical Computer Science IT University of Copenhagen September 12th, 2005 Søren Debois (Theory, ITU) Introduction to C++ September 12th, 2005

More information

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

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java 1 CS/ENGRD 2110 SPRING 2014 Lecture 2: Objects and classes in Java http://courses.cs.cornell.edu/cs2110 Java OO (Object Orientation) 2 Python and Matlab have objects and classes. Strong-typing nature of

More information

COSC252: Programming Languages: Abstraction and OOP. Jeremy Bolton, PhD Asst Teaching Professor. Copyright 2015 Pearson. All rights reserved.

COSC252: Programming Languages: Abstraction and OOP. Jeremy Bolton, PhD Asst Teaching Professor. Copyright 2015 Pearson. All rights reserved. COSC252: Programming Languages: Abstraction and OOP Jeremy Bolton, PhD Asst Teaching Professor Copyright 2015 Pearson. All rights reserved. Copyright 2015 Pearson. All rights reserved. Topics The Concept

More information

Overview of Eclipse Lectures. Module Road Map

Overview of Eclipse Lectures. Module Road Map Overview of Eclipse Lectures 1. Overview 2. Installing and Running 3. Building and Running Java Classes 4. Refactoring Lecture 2 5. Debugging 6. Testing with JUnit 7. Version Control with CVS 1 Module

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

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003 Outline Object Oriented Programming Autumn 2003 2 Course goals Software design vs hacking Abstractions vs language (syntax) Java used to illustrate concepts NOT a course about Java Prerequisites knowledge

More information

Constructor - example

Constructor - example Constructors A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class name. The constructor is invoked whenever

More information

Object-Oriented Programming (Java)

Object-Oriented Programming (Java) Object-Oriented Programming (Java) Topics Covered Today 2.1 Implementing Classes 2.1.1 Defining Classes 2.1.2 Inheritance 2.1.3 Method equals and Method tostring 2 Define Classes class classname extends

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level

More information

Lecture 22: Java. Overall Structure. Classes & Objects. Every statement must end with ';' Carl Kingsford, , Fall 2015

Lecture 22: Java. Overall Structure. Classes & Objects. Every statement must end with ';' Carl Kingsford, , Fall 2015 Carl Kingsford, 0-0, Fall 0 Lecture : Java Overall Structure Classes & Objects Every function in Java must be inside a class, which are similar to Go's struct s. For example: 8 9 0 8 9 class MyProgram

More information

One of these "compartments" is more correctly referred to as an element of the array

One of these compartments is more correctly referred to as an element of the array An array is a special type of variable in that it can contain many values If a standard variable is like a box, think of an array as being like a box with compartments: One of these "compartments" is more

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 10 October 1, 2018 CPSC 427, Lecture 10, October 1, 2018 1/20 Brackets Example (continued from lecture 8) Stack class Brackets class Main

More information

Principles of Object Oriented Programming. Lecture 4

Principles of Object Oriented Programming. Lecture 4 Principles of Object Oriented Programming Lecture 4 Object-Oriented Programming There are several concepts underlying OOP: Abstract Types (Classes) Encapsulation (or Information Hiding) Polymorphism Inheritance

More information

Fundamentals of Object Oriented Programming

Fundamentals of Object Oriented Programming INDIAN INSTITUTE OF TECHNOLOGY ROORKEE Fundamentals of Object Oriented Programming CSN- 103 Dr. R. Balasubramanian Associate Professor Department of Computer Science and Engineering Indian Institute of

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

class objects instances Fields Constructors Methods static

class objects instances Fields Constructors Methods static Class Structure Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance variables)that hold the data for each object Constructors that

More information

Chapter 6: Inheritance

Chapter 6: Inheritance Chapter 6: Inheritance EECS 1030 moodle.yorku.ca State of an object final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80; GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT);

More information

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

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. CMSC 131: Chapter 28 Final Review: What you learned this semester The Big Picture Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. Java

More information

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

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM This handout explains what you have to know for the first prelim. Terms and their meaning Below, we summarize the terms you should

More information

Media Computation. Lecture 16.1, December 8, 2008 Steve Harrison

Media Computation. Lecture 16.1, December 8, 2008 Steve Harrison Media Computation Lecture 16.1, December 8, 2008 Steve Harrison Today -- Details of Creating Classes From requirements to classes Creating a class that will simulate a number game Practice going from requirements

More information

Instance Members and Static Members

Instance Members and Static Members Instance Members and Static Members You may notice that all the members are declared w/o static. These members belong to some specific object. They are called instance members. This implies that these

More information

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001 CSE143 Exam with answers Problem numbering may differ from the test as given. All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to

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

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation. Java: Classes Introduction A class defines the abstract characteristics of a thing (object), including its attributes and what it can do. Every Java program is composed of at least one class. From a programming

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

Java Lecture Note. Prepared By:

Java Lecture Note. Prepared By: Java Lecture Note Prepared By: Milan Vachhani Lecturer, MCA Department, B. H. Gardi College of Engineering and Technology, Rajkot M 9898626213 milan.vachhani@yahoo.com http://milanvachhani.wordpress.com

More information

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance? CS6501 - Internet programming Unit- I Part - A 1 Define Java. Java is a programming language expressly designed for use in the distributed environment of the Internet. It was designed to have the "look

More information

Object-Oriented Programming

Object-Oriented Programming Object-Oriented Programming 1. What is object-oriented programming (OOP)? OOP is a technique to develop logical modules, such as classes that contain properties, methods, fields, and events. An object

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information