Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

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

Objectives. Explain the purpose and objectives of objectoriented. Develop design class diagrams

Cpt S 122 Data Structures. Introduction to C++ Part II

Data Structures (list, dictionary, tuples, sets, strings)

Objects and Classes -- Introduction

Chapter 6 Introduction to Defining Classes

by Pearson Education, Inc. All Rights Reserved. 2

Programming for Engineers in Python

Object oriented programming Concepts

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Object Oriented Programming. Solved MCQs - Part 2

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

CS-202 Introduction to Object Oriented Programming

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Object-Oriented Programming Concepts

CS121/IS223. Object Reference Variables. Dr Olly Gotel

JAVA: A Primer. By: Amrita Rajagopal

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

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

Introduction to Programming Using Java (98-388)

Object Oriented Programming

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

CHAPTER 7 OBJECTS AND CLASSES

1. Write two major differences between Object-oriented programming and procedural programming?

Object Oriented Programming

Object Oriented Software Development CIS Today: Object Oriented Analysis

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

Methods and Data (Savitch, Chapter 5)

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

CS313D: ADVANCED PROGRAMMING LANGUAGE

Data Abstraction. Hwansoo Han

Example: Fibonacci Numbers

CHAPTER 7 OBJECTS AND CLASSES

COP 3330 Final Exam Review

Software Architecture (Lesson 2) Object-Oriented Paradigm (1)

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

CS260 Intro to Java & Android 03.Java Language Basics

Programming II (CS300)

C++ Important Questions with Answers

BM214E Object Oriented Programming Lecture 8

Methods. Methods. Mysteries Revealed

CS313D: ADVANCED PROGRAMMING LANGUAGE

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

PROGRAMMING LANGUAGE 2

CS162: Introduction to Computer Science II

D Programming Language

Object Reference and Memory Allocation. Questions:

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

Lecture 7: Classes and Objects CS2301

What are the characteristics of Object Oriented programming language?

Chapter 11 Object and Object- Relational Databases

About this exam review

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Classes and Objects. COMP1400/INFS1609 Week 8. Monday, 10 September 12

Chapter 4 Defining Classes I

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

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

CS1004: Intro to CS in Java, Spring 2005

Object-Oriented Programming, Iouliia Skliarova

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Object-Oriented Programming

Chapter 15: Object Oriented Programming

Object Oriented Programming

Java Classes - Using your classes. How the classes you write are being used

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

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CSI33 Data Structures

Thursday, February 16, More C++ and root

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

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

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

VARIABLES AND TYPES CITS1001

Unit 1: Preliminaries Part 1: Course Introduction, Abstraction, and ADT s

Object Oriented Design

CSC1322 Object-Oriented Programming Concepts

Chapter 1 Getting Started

Object-Oriented Programming Paradigm

EL2310 Scientific Programming

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

Object-Oriented Programming in Processing

Java Magistère BFA

CS 231 Data Structures and Algorithms, Fall 2016

1. Write the number of the definition on the right next to the term it defines. (a) copy 4

Operator overloading

Chapter 4: Writing Classes

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Recursion 1. Recursion is the process of defining something in terms of itself.

Lecture 5: Methods CS2301

Inheritance (IS A Relationship)

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Chapter 5 Object-Oriented Programming

ECE 122. Engineering Problem Solving with Java

Transcription:

Computer Science and Engineering Indian Institue of Technology Bombay November 27, 2004

What is Object Oriented Programming? Identifying objects and assigning responsibilities to these objects. Objects communicate to other objects by sending messages. Messages are received by the methods of an object

The two steps of Object Oriented Programming Making Classes: Creating, extending or reusing abstract data types. Making Objects interact: Creating objects from abstract data types and defining their relationships.

What are Software Objects? Building blocks of software systems program is a collection of interacting objects objects cooperate to complete a task to do this, they communicate by sending messages to each other Object model tangible things school car Objects model conceptual things meeting date

What are Software Objects? (continued..) Objects model processes finding path through a maze sorting a deck of cards Objects have capabilities: what they can do, how they behave properties: features that describe the objects

Object Capabilities: Actions Objects have capabilities that allow them to perform specific actions objects are smartthey know how to do things an object gets something done only if some object tells it to use one of its capabilities Capabilities can be: constructors: establish initial state of objects properties commands: change objects properties queries: provide answers based on objects properties

Object Properties: State Properties determine how an object acts some properties may be constant, others variable properties themselves are objects also can receive messages trash cans lid and trash are objects Properties can be: attributes: things that help describe object components: things that are part of an object associations: things it knows about, but are not parts State: collection of all objects properties

Object Instances Object instances are individual objects made from class template one class may represent an indefinite number of object instances making an object instance is called instantiating that object Shorthand: class: object class instance: object instance (not to be confused with instance variable)

Object Instances (continued) Individual instances have individual identities allows other objects to send messages to given object each is unique, even though it has same capabilities think of class of CS15 students A reference is just the address in memory where its instance is stored also called pointer

Memory Revealed Every instance is stored in computers memory memory is a set of consecutively numbered storage locations, each containing a byte instance is stored in a series of contiguous bytes starting at a given location Instance is identified and referenced by unique address of its starting location when it is made address looks like 0xeff8a9f4 (hexadecimal notation, base 16) just like postal address represents actual home

Messages for Object Communication No instance is an island must communicate with others to accomplish task properties allow them to know about other objects Instances send messages to one another to invoke a capability (i.e., to execute a task) method is code that implements message we say call a method instead of invoke capability Each message requires: sender: object initiating action receiver: instance whose method is being called message name: name of method being called optionally parameters: extra info needed by method to operate

Encapsulation Car encapsulates lots of information quite literally, under hood and behind dashboard So, you do not need to know how a car works just to use it steering wheel and gear shift are the interface engine, transmission, driver train, wheels,..., are the (hidden) implementation Likewise, you do not need to know how an object works to send messages to it But, you do need to know what messages it understands (i.e., what its capabilities are) class of instance determines what messages can be sent to it

Views of a Class Objects separate interface from implementation object is black box; hiding internal workings and parts interface protects implementation from misuse Interface: public view allows instances to cooperate with one another without knowing too many details like a contract: consists of list of capabilities and documentation for how they would be used Implementation: private view properties that help capabilities complete their tasks

Notes About Java Syntax Reserved words certain words in Java have a particular meaning and cannot be used for any other purpose case-sensitive (always all lower case) class, public, new, private, extends dentifiers names used for classes, methods, and variables first character must be a letter or underscore rest may be any combination of letters, numbers, and underscores but no spaces

Constructors Next we need to have instances of our class to do something useful Constructor is special method that is called whenever class is instantiated (created) another object sends a message that calls constructor constructor is first message an object receives and cannot be called subsequently establishes initial state of properties for instance If you do not define any constructors for class, Java writes one for you called default constructor initializes all instance variables for instance to their default values default values should not be relied upon ALWAYS write your own constructor for each class ALWAYS give each instance variable an initial value in this constructor Since we dont want a default constructor, we need to write our own constructor...

Constructors(continued..) A constructor is always called when an object is created. We can define our own constructors (Note: a class can have more than one constructor). If an object is copied from another object then the copy constructor is called.

Multiple Constructors Sometimes want to initialize in a number of different ways, depending on circumstance. This can be supported by having multiple constructors having different input arguments.

Method Overloading Constructors all have the same name. Methods are distinguished by their signature: name number of arguments type of arguments position of arguments That means, a class can also have multiple methods with the same name : method overloading. This is a form of polymorphism.

Polymorphism Allows a single method or operator associated with different meaning depending on the type of data passed to it. It can be realised through: Method Overloading Defining the same method with different argument types (method overloading) - polymorphism. The method body can have different logic depending on the date type of arguments.

Automatic garbage collection The object does not have a reference and cannot be used in future. The object becomes a candidate for automatic garbage collection. Java automatically collects garbage periodically and releases the memory used to be used in the future. Q

Object Instantiation Object instantiation makes an object instance from a particular class allows other instances to send messages to instance constructor is first message: makes the instance

Summary A class is a blueprint for an object. Objects are created similar to other data types (int, char, ). The construction of an object can be defined by the user. Messages are sent to an object by calling a method.