CSG2H3 Object Oriented Programming

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
UML Basics & Access Modifier
Inheritance using Java
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Writing Classes (Chapter 4)
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
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.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
By : Robert Apeldorn. What is OOP?  Object-oriented programming is a programming paradigm that uses “objects” to design applications and computer programs.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Topics Instance variables, set and get methods Encapsulation
Outline Anatomy of a Class Encapsulation Anatomy of a Method Copyright © 2014 Pearson Education, Inc.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
External Scope CECS 277 Mimi Opkins.
Introduction to Object-oriented Programming
OOP: Encapsulation &Abstraction
Classes (Part 1) Lecture 3
Inheritance ITI1121 Nour El Kadri.
Examples of Classes & Objects
Objects as a programming concept
Inheritance and Polymorphism
Creating Your OwnClasses
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Lecture 9 Concepts of Programming Languages
Can perform actions and provide communication
Encapsulation.
Can perform actions and provide communication
Encapsulation & Visibility Modifiers
Corresponds with Chapter 7
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Classes & Objects: Examples
Encapsulation and Constructors
Can perform actions and provide communication
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Chapter 9 Objects and Classes
Final and Abstract Classes
Classes and Objects CGS3416 Spring 2019.
ITE “A” GROUP 2 ENCAPSULATION.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

CSG2H3 Object Oriented Programming Encapsulation

“Driver” Class Class that contain main method (psvm) To “Drives” the application Only a term, not necessarily that a main class is called “driver” To separate between class “model” and class “application”

“Model” Classes Try not to put any input/output mechanism in model class’ methods Use methods to process function or action by receiving parameters then return the result Let the “driver” class do the I/O interaction For this we learn “Encapsulation”

Encapsulation Accessor and mutators

Information Hiding Get me data from an object! Don’t know how you do it Don’t care how you do it

Encapsulation FIRST LAW of OOP : Data must be hidden Read access through read functions Write access through write functions For Every piece of data : 4 possibilities Read and write Read only Write only No access

Encapsulation Hides the behavior of an object from its implementation Separates what an object looks like from how it does it implements its behavior

Modularity The source code for an object can be written and maintained independently of the source code for the other objects

Data/Information Hiding An object has public interface that other objects can use to communicate with it. The object can maintain private information and method that can be changed at any time without affecting other objects that depend on it.

Setter and Getter To modify the value of a field, use setter void setVarName(varName : varType) To get the value of a field, use getter varType getVarName()

In other words… Technique of making the fields in a class private and providing access to the field via public methods. If a field is declared private, it cannot be accessed by anyone outside the class  data hiding Prevents the code and data being randomly accessed by other code outside the class For that, we use the Access Modifiers

Access Modifiers Access Modifiers specify what classes “see” or “know about” They are the public, private, and protected Effects of visibility … you cannot: reference or instantiate classes which are “invisible” to you call methods that are invisible to you directly access instance variables which are invisible to you

Access Modifier Privilege Same class Same Package Subclass Any class private Y   default protected Public

public Fields, methods and constructors declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.

private Fields, methods or constructors declared private are strictly controlled, which means they cannot be accessed by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter methods for them.

protected Fields, methods and constructors declared protected in a superclass can be accessed only by subclasses in other/same packages. Classes in the same package can also access protected fields and methods as well, even if they are not a subclass of the protected member’s class. we will learn more of this in Inheritance

default no access modifier is present. Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package.

Example error: privateInt has private access public class Encapsulation{ public int publicInt; private int privateInt; int defaultInt; } public class Driver{ public static void main(String args[]){ Encapsulation e = new Encapsulation(); e.publicInt = 10; e.privateInt = 20; e.defaultInt = 30; System.out.println(e.publicInt+" "+ e.privateInt+" "+e.defaultInt); }   error: privateInt has private access

Example public class Encapsulation{ public int publicInt; private int privateInt; int defaultInt; public void setPrivateInt(int privateInt){ this.privateInt = privateInt; } public int getPrivateInt(){ return privateInt; public class Driver{ public static void main(String args[]){ Encapsulation e = new Encapsulation(); e.publicInt = 10; e.setPrivateInt(20); e.defaultInt = 30; System.out.println(e.publicInt+" "+ e.getPrivateInt()+" "+e.defaultInt); }  

Example package test; public class Encapsulation{ public int publicInt; private int privateInt; int defaultInt; public void setPrivateInt(int privateInt){ this.privateInt = privateInt; } public int getPrivateInt(){ return privateInt; import test.Encapsulation; public class Driver{ public static void main(String args[]){ Encapsulation e = new Encapsulation(); e.publicInt = 10; e.setPrivateInt(20); e.defaultInt = 30; System.out.println(e.publicInt+" "+ e.getPrivateInt()+" "+e.defaultInt); }   error: defaultInt is not public in Encapsulation; cannot be accessed from outside package

Question?