Chapter 5: Classes and Objects in Depth. Information Hiding

Similar documents
Chapter3: Introduction to Classes and Objects

Chapter 5: Classes and Objects in Depth. Introduction to methods

Principles of Object Oriented Programming. Lecture 4

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Notes on Chapter Three

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Chapter 4. Defining Classes I

Chapter 4. Defining Your Own Classes Part 1. CS 180 Sunil Prabhakar Department of Computer Science Purdue University


Chapter 5 Object-Oriented Programming

Object Oriented Programming

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

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Object-Oriented Programming Concepts

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

CS1004: Intro to CS in Java, Spring 2005

Distributed Systems Recitation 1. Tamim Jabban

Chapter 15: Object Oriented Programming

Chapter 2: Java OOP I

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Java Object Oriented Design. CSC207 Fall 2014

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

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

Object-Oriented Programming (OOP) Fundamental Principles of OOP

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

CH. 2 OBJECT-ORIENTED PROGRAMMING

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

ECOM 2324 COMPUTER PROGRAMMING II

Programming II (CS300)

Distributed Systems Recitation 1. Tamim Jabban


Programming II (CS300)

Object Oriented Programming

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

Object oriented programming Concepts

OBJECT ORIENTED PROGRAMMING

Chapter 4 Defining Classes I

Handout 7. Defining Classes part 1. Instance variables and instance methods.

JAVA: A Primer. By: Amrita Rajagopal

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 5 Diploma in IT OBJECT ORIENTED PROGRAMMING

Data Abstraction. Hwansoo Han

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Object Oriented Programming

Encapsulation. Mason Vail Boise State University Computer Science

CSC 1214: Object-Oriented Programming

Object Orientated Analysis and Design. Benjamin Kenwright

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

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

CS200: Advanced OO in Java

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

Getter and Setter Methods

ITI Introduction to Computing II

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Example: Fibonacci Numbers

Chapter 4: Writing Classes

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

CS304 Object Oriented Programming Final Term


Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Microsoft Visual Basic 2005: Reloaded

Classes, Objects, and OOP in Java. June 16, 2017

I Wish I Knew How To. Begin Object Oriented Programming With Xojo. By Eugene Dakin. July 2015 Edition (1.0)

SEEM4570 System Design and Implementation. Lecture 11 From Design to Implementation

C++ Inheritance and Encapsulation

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

Anatomy of a Class Encapsulation Anatomy of a Method

Java and OOP. Part 2 Classes and objects

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Chapter 4. Defining Your Own Classes Part 1

Classes, objects, methods and properties

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

CS 302 Week 9. Jim Williams

INHERITANCE: EXTENDING CLASSES

The Object Oriented Paradigm

CS-202 Introduction to Object Oriented Programming

Dot and Scope Resolution Operator

ITI Introduction to Computing II

13. Java Classes. Educational Objectives. Classes - Technical. Definition: Classes

Data Structures using OOP C++ Lecture 3

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Introduction to OO Concepts

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

Object-Oriented Programming

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

CS304 Object Oriented Programming

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Polymorphism. Zimmer CSCI 330

Inheritance and Polymorphism

Mobile Application Programming. Swift Classes

Java Fundamentals (II)

System.out.print(); Scanner.nextLine(); String.compareTo();

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Transcription:

Chapter 5: Classes and Objects in Depth Information Hiding

Objectives Information hiding principle Modifiers and the visibility UML representation of a class Methods Message passing principle Passing parameters Getters and setters Constructors Overloading Page 2

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. Page 3

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 view view Page 4

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. Page 5

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 nonintended 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. Page 6

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. Page 7

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. Page 8

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. Page 9

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. Page 10

Accessibility from Inside (the Instance itself) public object:x private - Accessible - Inaccessible All members of an instance are accessible from the instance itself. Page 11

Accessibility from an Instance of another Class Accessibility from The Client class. :Y(client) object:x public private - Accessible - Inaccessible Only public members Are visible from outside. All else is hidden from Outside. Page 12

Accessibility from an Instance of the same Class Accessibility from The Client class. two:x one:x public private - Accessible - Inaccessible If a member is accessible from an instance, it is also accessible from other instances of the same class. Page 13

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. ClassName - att 1 : datatype 1 - - att i : datatype i Attributes + m 1 ( ): datatype 1 +... + m j ( ): datatype j Methods (Services) Page 14

Declaring Private Attributes <modifiers> <data type> <attribute name> ; Modifiers Data Data Type Type Name private String studentname ; Page 15

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 Page 16

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); } } Page 17

Accessibility Example Service obj = new Service(); obj.memberone = 10; obj.membertwo = 20; obj.doone(); obj.dotwo(); Client class Service { public int memberone; private int membertwo; } public void doone() { } private void dotwo() { } Service Page 18