What is an object? An object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the.

Slides:



Advertisements
Similar presentations
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Information Hiding Chapter 5: Classes and Objects in Depth.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
UML Basics & Access Modifier
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Programming Languages and Paradigms Object-Oriented Programming.
Writing Classes (Chapter 4)
1 OOP in C#: Object Interaction. Inheritance and Polymorphism OOP in C# - Del 1.
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.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Object-Oriented Programming in C++
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
Page 4.1 – Autumn 2010Steffen Vissing Andersen SDJ I1, Autumn 2010 Agenda – Session 4 – 7. September 2009 Java fundamentals, checkpoint from chapter 2.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes and Objects. Object Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Classes and Objects. How can one design a program?  Top-down structured design: uses algorithmic decomposition where each module denotes a major step.
Topics Instance variables, set and get methods Encapsulation
OOP Basics Classes & Methods (c) IDMS/SQL News
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Classes and Objects. Object vs. Class Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2  A class could be considered as a set of objects having.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
OBJECT ORIENTED PROGRAMMING INTRODUCTION. WHAT IS OBJECT-ORIENTATION ALL ABOUT? Principles and techniques for system modeling which: Aim to produce a.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
 Class and Diagram  Representation of Class Name Attributes Operations  Visibility of Attributes and Operations.
Modern Programming Tools And Techniques-I
Data Structures and Algorithms revision
Objects as a programming concept
Java OOP Overview Classes and Objects, Members and Class Definition, Access Modifier, Encapsulation Java OOP Overview SoftUni Team Technical Trainers.
Object-Oriented Programming: Classes and Objects
Classes and OOP.
Topic: Classes and Objects TALENTSPRINT | © Copyright 2012
Objects as a programming concept
Table of Contents Class Objects.
Object Oriented Programming
Object Oriented Concepts -I
Computing with C# and the .NET Framework
CS1201: Programming Language 2
Software Engineering System Modeling Chapter 5 (Part 2) Dr.Doaa Sami
Object-Oriented Programming: Classes and Objects
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Creating and Using Classes
Corresponds with Chapter 7
Chapter 5: Classes and Objects in Depth
Object Oriented Programming Review
JAVA CLASSES.
CS1201: Programming Language 2
Class Diagram.
Introduction to Object-Oriented Programming
Chapter 5: Classes and Objects in Depth
Object-Oriented PHP (1)
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Chapter 7 Objects and Classes
Previous Lecture: Today’s Lecture: Reading (JV):
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

what is an object? An object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain. An object is anything to which a concept applies. An object is a “noun”

How to define an object? An object is defined by: Example Attributes (also called fields) Behaviour---what it can do? (also called methods) Example A man can be defined by: Attributes: name, age, job, address,..etc Behaviour: talk, walk, eat, work, study,...etc How to define a bird? A car? A flight?

Classes Objects of the real world may be classified into types: Cars, Cell-Phones, employee, etc. Objects of the same type have the same characteristics and are manufactured using the same template. A class is an outline or Template from which objects of the same type are created. A class describes a set of objects having the same characteristics and offering the same services.

Class A class represents a template for several objects and describes how these objects are structured internally Objects of the same class have the same definition both for their operations and their information structure Classes are types of things

Class vs. Object Class People Class Vehicle Class Car objects : John , George, Sara . They are instantiated from the people class Class Vehicle objects :Bus, car, train (instances of the vehicle class) Class Car objects : The blue Nissan, the red Vauxhall, my uncle’s car (instances of the car class)

Class vs. Object

Class vs. Object (cont.) Find a class to represent the following items: dog, cat, lion, tiger chair, table, wardrobe banana, orange, apple breakfast, lunch, dinner Provide examples of objects that can be instantiated from the following classes Students, Courses, Modules

Instance An instance is an object created from a class A class describes the behavior and information structure of an instance, while the current state of the instance is defined by the operations performed on the instance System’s behavior is performed via the interactions between instances

Object-oriented Basics Fields/attributes Classes define fields (e.g. a Person class could contain fields name, age, gender, etc.) Objects have attributes, which are values stored in fields (e.g. person_1 has attributes “John Smith”, 25, Male) An object’s state is defined by its attributes Methods Classes define methods, which can access or change attributes Objects communicate by calling (invoking) each others’ methods Parameters Methods can have parameters to pass additional information during execution 9

Summary – OO Basics Fundamental concepts Objects Classes Object, Class, Field/attribute, Method, Parameter Objects Represent “things” from the real world, or from some problem domain (e.g. “the red car in the car park”) Classes Objects are created from classes Represent all objects of a kind (e.g. all “cars”) 10

Access Modifiers (cont.) Accessibilities options public – Accessible to all private – Accessible to containing class protected – Accessible to containing or derived classes (will be described in CSC113) In most cases: fields are private or protected, and methods are public.

UML Representation of a Class UML represents a class with a rectangle having 3 compartments stacked vertically. The top compartment shows the class's name. The middle compartment lists the attributes. The bottom compartment lists the operations: methods or services. Links between different classes define relationships (will be covered in CSC113) Methods (Services) Attributes ClassName att1: dataType1 … atti: dataTypei + m1(…): dataType1 + ... + mj(…): dataTypej

UML Representation of a Class (UML Class Diagram) UML uses three symbols to represent the visibility of the class’ members. + : mentions that the member is public. - : mentions that the member is private. # : introduced in the CSC 113. These are the Access Modifiers Methods (Services) Attributes ClassName att1: dataType1 … atti: dataTypei + m1(…): dataType1 + ... + mj(…): dataTypej

Ex: UML Class Diagram Problem Statement: Nouns : Verbs : Write a program to input the length and width of the rectangle, and calculate and print the perimeter and area of rectangle. Nouns : length , width, rectangle, perimeter , area Verbs : print , calculate Now we will address the problem as OOP

UML Example Rectangle length : double + print () void width: double Methods (Operation) Attributes Rectangle width: double length : double + calcuArea():double + calcuPerimeter()double + print () void

Declared as any variable Method access attribute directly public class Rectangle { // Attributes private double width; private double length; // Methods (Operation) public double calcArea(){ return height*width;} public double calcPerimeter(){ return 2*(height + width);} public void print() System.out.print(“The Area is ”+calcArea()); System.out.print(“The Perimeter is ”+calcPerimeter()); } Declared as any variable Rectangle width: double length : double + calcuArea():double + calcuPerimeter()double + print () void Method access attribute directly

Object vs. class Cell phone Brand:string Price:double +addcontant():void Class Obj1 Obj 2 Obj 3 Brand Price Iphone Brand Price Samsung Brand Price LG 2400.0 2000.0 1900.0 Objects

Practical hint Class Course will not execute by itself It does not have method main CourseRegistration uses the class Course. CourseRegistration, which has method main, creates instances of the class Course and uses them.

Set method : Get method :

Example Create a Java class called Account based on the following UML : The class should: Have a default constructor that initializes the attributes to default values , and another constructor that initializes the date attributes to given values. Method deposit will add to balance Method withdraw will reduce the balance Provide set() and get() methods for each attributes. In the main() method of the class TestAccount write statements that will call both constructors and test class Accounts capabilities. Account - number :int - balance : double +deposit (double amount):void +withdraw(double amount) : void

Class Account public class Account { // definition of attributes (data) private int number; private double balance; // constructor public Account () { number=0; balance=0; } public Account (int n , double b) { number=n; balance=b; // definition of operations (methods) public void deposit (double amount) { balance = balance + amount; } //end of deposit public void withdraw(double amount) { if (balance >= amount) balance = balance – amount; } //end of withdraw public void setNumber (int n) { number = n; } //end of setNumber public void setBalance (double b) { balance=b; } //end of setBalance public int getNumber() { return number ; } //end of getNumber public double getBalance() { return balance; } //end of getBalance } //end of class

Class TestAccount public class TestAccount { public static void main(String[] args) { Account Account1=new Account(); Account Account2=new Account(1,6200); Account1. setNumber (2) ; Account1. setBalance (4300) ; Account2. deposit (550) ; Account1. withdraw(200); System.out.println(Account1. getBalance() + "-" +Account2. getBalance() ); }