Encapsulation.

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
Accessor Functions 04/13/11. Encapsulation  Like case on my watch Protect from outside Protect from outside Have member functions control data Have member.
Chapter 7 Inheritance Copyright © 2010 Pearson Addison-Wesley. 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.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
UML Basics & Access Modifier
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods.
CSC 142 Computer Science II Zhen Jiang West Chester University
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Programming in Java CSCI-2220 Object Oriented Programming.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters.
11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance and Polymorphism
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
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.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
External Scope CECS 277 Mimi Opkins.
OOP: Encapsulation &Abstraction
Classes C++ representation of an object
Objects as a programming concept
Inheritance and Polymorphism
Computer Science II Exam 1 Review.
Inheritance 2nd Lecture
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Corresponds with Chapter 7
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Packages and Interfaces
Comp 249 Programming Methodology
Inheritance 2nd Lecture
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
Classes and Objects.
Unit 1 Lab16.
Inheritance 2nd Lecture
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
NAME 436.
Object Oriented Programming
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
Classes C++ representation of an object
Chapter 8 Inheritance Part 2.
CMSC 202 Inheritance II.
C++ Object Oriented 1.
CSG2H3 Object Oriented Programming
Chapter 5 Classes.
Presentation transcript:

Encapsulation

Encapsulation and inheritance Private instance variables and methods in a base class cannot be directly accessed (by name) in a derived class. They can be indirectly accessed (via accessors and mutators in the base class). It’s exactly as if they don’t exist. (They can actually be defined and redefined in the derived class.)

What if private instance variables could be accessed in subclasses? Problems: Subversion: Then to change them, all we would have to do is to make a subclass of the base class and then go ahead and change them. Errors: The more common problem is that the accessors and mutators guard against unintentional errors.

Protected access Protected (rather than public or private) access allows: Access by name inside its own class definition. Access by name inside any class derived from it. Access by name in the definition of any class in the same package (even if the class is not derived from it).

Protected access Access between private and public Very weak protection Use is discouraged (use the following (viz., package access) instead)

Package access AKA default access or friendly access. Can be accessed by name by anything in the package but nothing outside of the package.

Package access This is what you get when you don’t specify either public, private, or protected (hence the name default access). (If you don’t specify a package, you belong to the default package.)

Package access More restricted than protected. Removes “Access by name inside any class derived from it.” if the derived class is NOT in the same package. Packages are analogous to directories (folders). If you control the directory, you control the package.

Access example package somePackage; public class A { public int v1; protected int v2; int v3; //package access private int v4; } public class B { can access v1, v2, and v3. cannot access v4. public class C extends A { //default package public class D extends A { can access v1 and v2. cannot access v3 and v4. } public class E { can access v1. cannot access v2, v3, and v4.

Access Access is the same for: static variables instance variables static methods ordinary methods

Summary public private protected package