CSC 1214: Object-Oriented Programming

Similar documents
BoxDemo1.java jueves, 02 de diciembre de :31

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

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

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

2. Introducing Classes

ANNAMACHARYA INSTITUTE OF TECHNOLOGY AND SCIENCES: : TIRUPATI

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming

Chapter 11. Abstract Data Types and Encapsulation Concepts

Arrays Classes & Methods, Inheritance

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

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

1.00 Lecture 8. Using An Existing Class, cont.

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

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

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

Topic 6: Inner Classes

CSE 303: Concepts and Tools for Software Development

Abstract Data Types and Encapsulation Concepts

JAVA. Lab-9 : Inheritance

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

Implementing Abstractions

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

10. Abstract Data Types

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

Hierarchical abstractions & Concept of Inheritance:

Inheritance. Transitivity

Implementing Subprograms

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

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

PROGRAMMING LANGUAGE 2

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

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

Chapter 11. Abstract Data Types and Encapsulation Concepts

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

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

OBJECT ORIENTED PROGRAMMING USING C++

CS/ENGRD 2110 SPRING 2018

7. C++ Class and Object

Lecture 6 Introduction to Objects and Classes

Java Memory Management

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

Abstract data types &

Abstract Data Types & Object-Oriented Programming

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

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

ITI Introduction to Computing II

Data Structures G5029

Chapter 11. Abstract Data Types and Encapsulation Concepts

CS201 Some Important Definitions

JAVA: A Primer. By: Amrita Rajagopal

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

Inheritance, and Polymorphism.

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

C++ Inheritance and Encapsulation

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

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

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

CMSC 4023 Chapter 11

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

Notes on Chapter Three

Static, Final & Memory Management

OBJECT ORİENTATİON ENCAPSULATİON

Data Structures using OOP C++ Lecture 3

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

CS453 CLASSES, VARIABLES, ASSIGNMENTS

VIRTUAL FUNCTIONS Chapter 10

C++ (classes) Hwansoo Han

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

Fundamentals of Programming. Lecture 19 Hamed Rasifard

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

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

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

Overview of Eclipse Lectures. Module Road Map

Chapter 10 Introduction to Classes

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

Constructor - example

Object-Oriented Programming (Java)

G Programming Languages - Fall 2012

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

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

CPSC 427: Object-Oriented Programming

Principles of Object Oriented Programming. Lecture 4

Fundamentals of Object Oriented Programming

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

class objects instances Fields Constructors Methods static

Chapter 6: Inheritance

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

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

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

Instance Members and Static Members

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

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

Java Lecture Note. Prepared By:

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?

Object-Oriented Programming

Rules and syntax for inheritance. The boring stuff

Transcription:

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: http://muele.mak.ac.ug 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, 2018 1 / 23

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

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, 2018 3 / 23

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, 2018 4 / 23

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, 2018 5 / 23

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, 2018 5 / 23

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, 2018 6 / 23

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, 2018 7 / 23

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, 2018 8 / 23

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, 2018 9 / 23

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, 2018 10 / 23

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, 2018 11 / 23

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, 2018 12 / 23

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, 2018 13 / 23

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, 2018 14 / 23

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, 2018 15 / 23

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; 6. 7. vol = mybox1.volume(); 8. System.out.println("Volume of mybox1 is " + vol); 9. 10. vol = mybox2.volume(); 11. System.out.println("Volume of mybox2 is " + vol); 12. 13. Output: Volume of mybox1 is 3000.0 Volume of mybox2 is 24.0 Kizito (Makerere University) CSC 1214 February, 2018 16 / 23

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]; 4. 5. Stack() { tos = -1; // initialize top-of-stack 6. 7. 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; 12. 13. 14. 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--]; 20. 21. Kizito (Makerere University) CSC 1214 February, 2018 17 / 23

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(); 5. 6. // 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); 9. 10. // 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()); 14. 15. System.out.println("Stack in mystack2:"); 16. for (int i = 0; i < 10; i++) 17. System.out.println(mystack2.pop()); 18. 19. Kizito (Makerere University) CSC 1214 February, 2018 18 / 23

Initialization The test program generates the following output: The Stack Class Example Output Stack in mystack1: 9 8 7 6 5 4 3 2 1 0 Stack in mystack2: 19 18 17 16 15 14 13 12 11 10 Kizito (Makerere University) CSC 1214 February, 2018 19 / 23

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, 2018 20 / 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, 2018 21 / 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, 2018 21 / 23

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, 2018 22 / 23

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, 2018 23 / 23