Introduction to Classes and Objects. Real Life When a design engineer needs an electrical motor he doesn’t need to worry about –How a foundry will cast.

Slides:



Advertisements
Similar presentations
Linked List: Traversal Insertion Deletion. Linked List Traversal LB.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
Software Engineering and Design Principles Chapter 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Road Map Introduction to object oriented programming. Classes
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Software Testing and Quality Assurance
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Introduction to Object-oriented programming and software development Lecture 1.
Writing Classes (Chapter 4)
An Object-Oriented Approach to Programming Logic and Design
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
38 4/11/98 CSE 143 Modules [Chapter 2]. 39 4/11/98 What is a Module?  Collection of related items packaged together  Examples:  Stereo System Components.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Introduction to Classes, Objects, Methods and Attributes Lecture # 5.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Documentation Javadocs. Design/Documentation An essential ingredient of good Object Oriented programming is known as design by contract. This means that.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Chapter 2 Principles of Programming and Software Engineering.
OOP Basics Classes & Methods (c) IDMS/SQL News
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Programming Logic and Design Seventh Edition
Abstract Data Types and Encapsulation Concepts
Classes and OOP.
Chapter 3: Using Methods, Classes, and Objects
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Chapter 4: Writing Classes
Abstract Data Types and Encapsulation Concepts
Encapsulation & Visibility Modifiers
Abstract Data Types and Encapsulation Concepts
Abstraction in Object-Oriented Programming
Outline Anatomy of a Class Encapsulation Anatomy of a Method
COP 3330 Object-oriented Programming in C++
Introduction to Data Structure
Abstraction and Objects
Presentation transcript:

Introduction to Classes and Objects

Real Life When a design engineer needs an electrical motor he doesn’t need to worry about –How a foundry will cast the motor housing –How the copper wire will be refined and manufactured –How the motor is constructed He will simply be concerned with selecting –The right mounting –The correct power requirements –The correct rotational speed and horsepower –Providing adequate ventilation If he meets the specifications then so will the motor. It’s like a contract

The Scenario We want to provide this same sort of a “black box” approach to reusable components… Don’t know how it works, but it works! InputOutput

The Scenario We want to create reusable components for other programmers to use. They are focused on their work and don’t want to know the details of how our component works. They are also busy trying to do their work, so maybe they’ll take shortcuts and “hack.” We want to provide services to them but protect the implementation details of our components.

Classes A class is the fundamental unit of object-oriented programming. It allows us to implement ADTs, combining both data and operations into one logical bundle.

Objects Once we have a class defined, we can then declare any number of objects of that class. For example: Once we have a queue class defined, we can then declare any number of queue objects. Each queue object will then have all the data and procedural abstractions needed for a queue.

Classes and Objects A class is like a blueprint or specification: An object is an instantiated class (like a building):

One to Many Once we have the class which is like a blueprint or spec... We may produce one item –Like the College of Computing Building –Like Turner Field We may produce many copies –Like 1 HP 3600 RPM Electrical Motors –Like F-22 Fighters –Like golf balls The choice simply depends on the given application

Classes vs. Objects Class refers to the template by which we implement the functionality of an ADT. Thus, it is analogous to data type: it is only a definition or template. Object refers to a specific instance of the class. Thus, it is analogous to variable: it is an instance of a template. Class : Object :: Type : Variable

Classes Allow for Reuse! Algorithms which make use of the queue class (by declaring instances of the queue, or objects) are clients who obtain services: Restaurant Check-out line Printer queue Automated distribution center The authors of these algorithms can instantiate and use queues without writing them!

Components of a Class The individual data elements of a class are referred to as attributes or fields of the class. The values of the entire collection of data items maintained within an object is referred to as the state of the object. The operations (procedures and functions) that are provided by the class (and thus allowed on the data) are called the methods of the class or object.

Controlling Visibility Recall we want to protect the implementation of the class. –Users shouldn’t need to know how it works –Users shouldn’t be able to violate the behaviors (may only do what we allow them). Encapsulation allows us to hide the implementation details.

Visibility Within a Class Public Section (Interface) Private Section (Implementation) The rest of the world

Anatomy of a Class For now, consider class components to have two types: Public - components which are "visible” to clients Private - components which are "hidden” from clients [A third type, Protected, will show up later]

Public Parts Everything that the client algorithm needs to know to use the class The visible parts of the class Defines the interface and contract with the user.

Private Parts Contains the details of the implementation of the class Cannot be seen by the client algorithm Hide attributes and method implementation

Interactions Private Data & Methods Public Methods and Contract Client Algorithm Server Object

Encapsulation and Hiding A client algorithm can see only the public parts of the class. A client has no idea what the private parts contain. A client algorithm can interact with an object only by calling the methods listed as public. A client algorithm cannot directly access any of the private data or methods within an object. It may “get at” these only via calls to public methods.

class data public method 1 public method 2 public method 3 a private method Public vs. Private Methods Clients may call any/all of the three public methods. Clients don’t know the private method exists; they cannot call it. Private methods will be called by other public methods Some Class

Documentation

Design by Contract An essential ingredient of good Object Oriented programming is known as design by contract. This means that before modules are written a specification or contract is written which states What preconditions must be met for the module to properly function What is the purpose of the module What will be the state of things after the module executes which is known as the postconditions

Example A module located in a Queue class which will dequeue the element at the head of the queue. // Precondition -- The queue must be instantiated. It is recommended that isEmpty be run to assure that their is an element to dequeue // Purpose -- Remove the element at the head of the queue and return it to the user. It will be an Object. // Postcondition -- The queue will have one fewer element unless the queue was empty to start with in which case the method will return null and the queue will be unchanged

Recall Simple Linked List Record LLNode definesa record data isoftype Num Next isoftype Ptr toa Node Endrecord // LLNode Now with object oriented programming we wish to create Linked List Node objects They will contain data similar to above Plus methods that can act on that data

A Simple Class class LLNode { private int data; private LLNode next; // Precon: instantiation // Purpose: sets data field // Postcon: data field will be set public void setData(int i) { // modifier data = i; } // Precon: data filed should be initialized // Purpose: returns contents of data field // Postcon: no change to object public int getData() { // accessor return data; }

A Simple Class // Precon: initialization // Purpose: sets value of next field // Postcon: value of next field will be changed public void setNext(LLNode n) { // modifier next = n; } // Precon: next field has been initialized // Purpose: return value of next field // Postcon: no change to object public LLNode getNext() { // accessor return next; } } // LLNode This may seem like a lot of busy work but it’s the general way of doing it!

Scope of Data Access Recall in Pseudocode variables always had scope or visibility only within the module wherein they were declared Constants and Definitions (Records and Arrays) were Global (i.e visible anywhere) One of the necessities of our desire to hide or encapsulate details from the user of a class is that variables declared inside the class (not in a method) have Global Scope inside the class!!!

A Simple Class class LLNode { data private int data; next private LLNode next; public void setData(int i) { // modifier data data = i; } public int getData() { // accessor data return data; } public void setNext(LLNode n) { // modifier next next = n; } public LLNode getNext() { // accessor next return next; } } // LLNode