Chapter 5: Classes and Objects in Depth

Slides:



Advertisements
Similar presentations
OBJECT ORIENTED PROGRAMMING M Taimoor Khan
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Object Oriented Programming & Software Engineering.
Road Map Introduction to object oriented programming. Classes
Information Hiding Chapter 5: Classes and Objects in Depth.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Class attributes Chapter 3: Introduction to Classes and Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Introduction to methods Chapter 5: Classes and Objects in Depth.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
Objects and Instance attributes and variables Chapter 3: Introduction to Classes and Objects.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 10 METHODS AND CONSTRUCTORS 1. Accessing Objects  Referencing the object’s data: objectReference.data myCircle.radius  calling the object’s.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Object-Oriented Programming Chapter Chapter
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CS1201: Programming Language 2 Classes and 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.
Classes and Objects. How can one design a program?  Top-down structured design: uses algorithmic decomposition where each module denotes a major step.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Comp1004: Building Better Objects II Encapsulation and Constructors.
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.
OBJECT ORIENTED PROGRAMMING INTRODUCTION. WHAT IS OBJECT-ORIENTATION ALL ABOUT? Principles and techniques for system modeling which: Aim to produce a.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Topic: Classes and Objects
Classes (Part 1) Lecture 3
Object Oriented Programming
CS1201: Programming Language 2
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Creating Your OwnClasses
Chapter 7 Classes & Objects.
CS1201: Programming Language 2
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Object Oriented Analysis and Design
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.
Corresponds with Chapter 7
IFS410: Advanced Analysis and Design
Chapter 9 Classes & Objects.
Chapter 9 Objects and Classes
Chapter 5: Classes and Objects in Depth
Classes and Objects.
Chapter 5: Classes and Objects in Depth
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object-Oriented Programming
By Rajanikanth B OOP Concepts By Rajanikanth B
CS1201: Programming Language 2
CIS 199 Final Review.
OO Programming Concepts
Chapter 7 Objects and Classes
CSG2H3 Object Oriented Programming
Presentation transcript:

Chapter 5: Classes and Objects in Depth Information Hiding

Dr. S. GANNOUNI & Dr. A. TOUIR Objectives Information hiding principle Modifiers and the visibility UML representation of a class Methods Message passing principle Passing parameters Getters and setters Constructors Overloading Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Object Oriented Basic Principles Abstraction Encapsulation Information Hiding Message Passing Overloading Inheritance Overriding Polymorphism Dynamic Binding Information hiding and Message passing are discussed in this chapter. Overloading is discussed in chapter 6. Inheritance, Polymorphism, Overriding and Dynamic binding are discussed in CSC 113. Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Abstraction Principle Data Abstraction In order to process something from the real world we have to extract the essential characteristics of that object. Data abstraction is the process of: Refining away the unimportant details of an object, Keeping only the useful characteristics that define the object. For example, depending on how a car is viewed (e.g. in terms of something to be registered, or alternatively something to be repaired, etc.) different sets of characteristics will emerge as being important. Functionality Abstraction Modeling functionality suffers from unnecessary functionality may be extracted, or alternatively, an important piece of functionality may be omitted. Functionality abstraction is the process of determining which functionality is important. view Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Encapsulation Principle Abstraction involves reducing a real world entity to its abstraction essential defining characteristics. Encapsulation extends this idea by also modeling and linking each data of an entity to the appropriate functionality of that entity. Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Encapsulation Gives Classes OOP makes use of encapsulation to ensure that data is used in an appropriate manner. by preventing from accessing data in a non-intended manner (e.g. asking if an Integer is true or false, etc.). Through encapsulation, only a predetermined appropriate group of operations may be applied (have access) to the data. Place data and the operations that act on that data in the same class. Encapsulation is the OO principle that allows objects containing the appropriate operations that could be applied on the data they store. My Nokia-N71 cell-phone stores: My contacts, Missed calls … etc. My Nokia-N71 may perform the following operations on the data it contains: Edit/Update/Delete an existing contact Add a new contact Display my missed calls. …etc. Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Information Hiding Principle Limit access to data only to internal operations that need it. OO classes hide the data as private data members and use public accessor operations to get at it. The scope of the data is limited to the class. Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Information Hiding Objectives Information hiding protects from exposing: data items (attributes). the difference between stored data and derived data. the internal structure of a class. implementation details of a class. Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Encapsulation and Information Hiding Encapsulation (is a language construct that ) facilitates the bundling of data with the operations acting on that data. Place data and the operations that perform on that data in the same class Information hiding is a design principle that strives to shield client classes from the internal workings of a class. Encapsulation facilitates, but does not guarantee, information hiding. Smearing the two into one concept prevents a clear understanding of either. Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

public and private modifiers Let’s consider a class X. Let’s consider Y a client class of X. Y is a class that uses X. Attributes (and methods) of X declared with the public modifier are accessible from instances of Y. The public modifier does not guarantee the information hiding. Attributes (and methods) of X declared with the private modifier are not accessible from instances of Y. The private modifier guarantee the information hiding. Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Accessibility from Inside (the Instance itself) object:X Accessible Inaccessible public private All members of an instance are accessible from the instance itself. Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Accessibility from an Instance of another Class object:X public private Accessible Inaccessible :Y(client) Accessibility from The Client class. Only public members Are visible from outside. All else is hidden from Outside. Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Accessibility from an Instance of the same Class one:X Accessible Inaccessible two:X Accessibility from The Client class. public private If a member is accessible from an instance, it is also accessible from other instances of the same class. Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

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. Methods (Services) Attributes ClassName att1: dataType1 … atti: dataTypei + m1(…): dataType1 + ... + mj(…): dataTypej Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Declaring Private Attributes <modifiers> <data type> <attribute name> ; Modifiers Data Type Name private String studentName ; Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Example of a Class with Private attributes ClassName studentName: String courseCode: String public class Course { // Attributes private String studentName; private String courseCode ; // No method Members } Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Dr. S. GANNOUNI & Dr. A. TOUIR class Course { // Data Member private String studentName; private String courseCode ; } public class CourseRegistration { public static void main(String[] args) { Course course1, course2; //Create and assign values to course1 course1 = new Course( ); course1.courseCode= “CSC112“; course1.studentName= “Majed AlKebir“; //Create and assign values to course2 course2 = new Course( ); course2.courseCode= “CSC107“; course2.studentName= “Fahd AlAmri“; System.out.println(course1.studentName + " has the course “+ course1.courseCode); System.out.println(course2.studentName + " has the course “+ course2.courseCode); } Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP

Accessibility Example … Service obj = new Service(); obj.memberOne = 10; obj.memberTwo = 20; obj.doOne(); obj.doTwo(); class Service { public int memberOne; private int memberTwo; public void doOne() { … } private void doTwo() { } This is a simple example of public and private modifiers. See how the client can access the public data member and method. Client Service Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP