Characteristics of OOP Encapsulation-------chap.5 Inheritance-----------chap.6 Polymorphism-------chap.7.

Slides:



Advertisements
Similar presentations
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Advertisements

PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Access Control Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.
Programming Languages and Paradigms Object-Oriented Programming.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 4 Objects and Classes.
Writing Classes (Chapter 4)
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
EE2E1. JAVA Programming Lecture 3 Java Programs and Packages.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Classes Dwight Deugo Nesa Matic
Interfaces and Inner Classes
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Package A package is a logical container that contains classes,interfaces sub packages. Package provide a unique name space to its members and provide.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Topics Instance variables, set and get methods Encapsulation
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Packages When we name a program , we name it by class name…
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Interface.
Packages and Interfaces
Interfaces.
Applying OO Concepts Using Java
Classes and Objects Systems Programming.
CSG2H3 Object Oriented Programming
Presentation transcript:

Characteristics of OOP Encapsulation chap.5 Inheritance chap.6 Polymorphism chap.7

5: Hiding the Implementation /Access Control package: the library unit –Creating unique package names –Collisions –A custom tool library –Using imports to change behavior –Package caveat Java access specifiers –“Friendly” –public: interface access –The default package –private: you can’t touch that! –protected: “sort of friendly” Interface and implementation Class access Exercises

Hiding the Implementation A primary consideration in object-oriented design is “ separating the things that change from the things that stay the same. ” –This is particularly important for libraries. –The user (client programmer) of that library must be able to rely on the part they use, and know that they won ’ t need to rewrite code if a new version of the library comes out. –On the flip side, the library creator must have the freedom to make modifications and improvements with the certainty that the client programmer ’ s code won ’ t be affected by those changes. To solve this problem, Java provides access specifiers – to allow the library creator to say what is available to the client programmer and what is not. –The levels of access control from “ most access ” to “ least access ” are public, protected, “ friendly ” (which has no keyword), and private.

Compilation Unit compilation unit: a source-code file for Java, which have a name ending in.java public class: There can be one and there can be only one in each compilation unit. public class name: the same name as the file (including capitalization, but excluding the.java filename extension). If there are additional classes in that compilation unit, they are hidden from the world outside that package because they’re not public, and they comprise “support” classes for the main public class.

package: the library unit Absolute class name com.bruceeckel.utility.foibles.AAA a=new com.bruceeckel.utility.foibles.AAA(); Import statement import com.bruceeckel.utility.foibles.* ; AAA a=new AAA(); if you use a package statement, it must appear as the first noncomment in the file. Creating unique package names (why & how)

Name visibility/Using other components package com.bruceeckel.utility.foibles ; class AAA {……} ======================= package com.other ; // if omit, means default package import com.bruceeckel.utility.foibles.* ; import java.util.ArrayList; import java.util.*; AAA a=new AAA(); //com.bruceeckel.utility.foibles.AAA a=new com.bruceeckel.utility.foibles.AAA(); ======================= java.lang.* is the exception which is needless to import

CLASSPATH environment variable import com.bruceeckel.simple.*; public class LibTest { public static void main(String[] args) { Vector v = new Vector(); List l = new List(); } } ///:~ –When the compiler encounters the import statement, it begins searching at the directories specified by CLASSPATH, looking for subdirectory com\bruceeckel\simple, then seeking the compiled files of the appropriate names (Vector.class for Vector and List.class for List). –CLASSPATH=.;D:\JAVA\LIB;C:\DOC\JavaT –CLASSPATH=.;D:\JAVA\LIB;C:\flavors\grape.jar

Package: the library unit Package caveat –It ’ s worth remembering that anytime you create a package, you implicitly specify a directory structure when you give the package a name. The package must live in the directory indicated by its name, which must be a directory that is searchable starting from the CLASSPATH. //: com:bruceeckel:simple:Vector.java // Creating a package. package com.bruceeckel.simple; public class Vector { public Vector() { System.out.println("com.bruceeckel.simple.Vector"); } } ///:~

Class Access Class Access: “ friendly ” or public –Note that a class cannot be private (that would make it accessible to no one but the class), or protected. –Inner class: private protected friendly public

Java access specifiers Java access specifiers: public, protected, and private Placed in front of each definition for each member in your class, whether it ’ s a field or a method. Each access specifier controls the access for only that particular definition. This is a distinct contrast to C++ …

Java access specifiers “ Friendly ” –The default access has no keyword, but it is commonly referred to as “ friendly. ” –It means that all the other classes in the current package have access to the friendly member, but to all the classes outside of this package the member appears to be private. public: interface access –When you use the public keyword, it means that the member declaration that immediately follows public is available to everyone,

Java access specifiers package c05.dessert; public class Cookie { public Cookie() { System.out.println("Cookie constructor"); } void bite() { System.out.println("bite"); } } ///:~ ======================= import c05.dessert.*; public class Dinner { public Dinner() { System.out.println("Dinner constructor"); } public static void main(String[] args) { Cookie x = new Cookie(); //! x.bite(); // Can't access } } ///:~

Java access specifiers private: you can ’ t touch that! The private keyword means that no one can access that member except that particular class, inside methods of that class. class Sundae { private Sundae() {} static Sundae makeASundae() { return new Sundae(); } public class IceCream { public static void main(String[] args) { //! Sundae x = new Sundae(); Sundae x = Sundae.makeASundae(); } } ///:~

Java access specifiers protected: “ sort of friendly ” The protected keyword deals with a concept called inheritance, package c05.dessert; public class Cookie { public Cookie() { System.out.println("Cookie constructor"); } void bite() { System.out.println("bite"); } } ///:~ ============= import c05.dessert.*; public class ChocolateChip extends Cookie { public ChocolateChip() { System.out.println( "ChocolateChip constructor"); } public static void main(String[] args) { ChocolateChip x = new ChocolateChip(); //! x.bite(); // Can't access bite } } ///:~

Java access specifiers package c05.dessert; public class Cookie { public Cookie() { System.out.println("Cookie constructor"); } protected void bite() { System.out.println("bite"); } } ///:~ ============= import c05.dessert.*; public class ChocolateChip extends Cookie { public ChocolateChip() { System.out.println( "ChocolateChip constructor"); } public static void main(String[] args) { ChocolateChip x = new ChocolateChip(); x.bite(); // OK! } } ///:~

Example: singleton public class Soup { private Soup() {} // (2) Create a static object and // return a reference upon request. // (The "Singleton" pattern): private static Soup ps1 = new Soup(); public static Soup access() { return ps1; } public void f() {} }

Exercises 10. Following the form of the example Lunch.java, create a class called ConnectionManager that manages a fixed array of Connection objects. The client programmer must not be able to explicitly create Connection objects, but can only get them via a static method in ConnectionManager. When the ConnectionManager runs out of objects, it returns a null reference. Test the classes in main( ).