12 Data abstraction Packages and encapsulation

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
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.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Chapter 10: Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Object Oriented Programming: Java Edition By: Samuel Robinson.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Generic abstraction  Genericity  Generic classes  Generic procedures Programming Languages 3 © 2012 David A Watt, University of Glasgow.
Inheritance in the Java programming language J. W. Rider.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Module Road Map Refactoring Why Refactoring? Examples
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Objects as a programming concept
Inheritance and Polymorphism
Computing with C# and the .NET Framework
Java Programming Language
Polymorphism and access control
Advanced Java Programming
CS18000: Problem Solving and Object-Oriented Programming
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
C++ Object Oriented 1.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

12 Data abstraction Packages and encapsulation Classes, subclasses, and inheritance Programming Languages 3 © 2012 David A Watt, University of Glasgow

Packages A package (or module) is a named group of components declared for a common purpose. These components may be types, constants, variables, procedures, inner packages, etc. (depending on the PL). The meaning of a package is the set of bindings exported by the package often called the package’s application program interface (API).

Example: simple Python module Outline of a module (dictionary.py) words = […] def contains (word): global words return (word in words) def add (word): global words if word not in words: words += [word] This module’s API: { words  a list of words, contains a function that tests whether a word is in the list, add  a procedure that adds a word to the list }

A program-unit’s API consists of its public bindings only. Encapsulation (1) Some of the components of a program-unit (package/class) may be private. This is called encapsulation. Levels of privacy: A component is private if it is visible only inside the program-unit. A component is protected if it is visible only inside the program-unit and certain “friendly” program-units. A component is public if it is visible to application code outside the program-unit. A program-unit’s API consists of its public bindings only.

Encapsulation (2) Most PLs (such as Ada, Java, Haskell) allow individual components of a program-unit to be specified as private/protected/public. Python has a convention that components whose names start with “_” are private, whilst those whose names start with letters are public. This convention is not enforced by the Python compiler.

Example: Python module with encapsulation Outline of a module (dictionary.py) _words = […] def contains (word): global _words return (word in _words) def add (word): global _words if word not in _words: _words += [word] This module’s API: { contains a function that tests whether a word is in the list, add  a procedure that adds a word to the list }

Example: Java packages In Java, the components of a package are classes and inner packages. Package components are added incrementally. Outline of a class declaration within a package: package sprockets; import widgets.*; public class C { … } declares that class C is a component of package sprockets declares that class C uses public components of package widgets

Classes (1) An object is a tagged tuple of variable components (instance variables), equipped with operations that access these instance variables. A constructor is an operation that initializes a newly created object. An instance method is an operation that inspects and/or updates an existing object of class C. That object (known as the receiver object) is determined by the method call. A class is a set of similar objects. All objects of a given class C have similar instance variables, and are equipped with the same operations.

A Java class declaration: Classes (2) A Java class declaration: declares its instance variables defines its constructors and instance methods specifies whether each of these is private, protected, or public. A Java instance method call has the form “O.M(…)”: The expression O yields the receiver object. M is the name of the instance method to be called. The call executes the method body, with this bound to the receiver object.

Example: Java class (1) Class declaration: class Dict { private int size; private String[] words; public Dict (int capacity) { … } public void add (String w) { if (! this.contains(w)) this.words[this.size++] = w; } public boolean contains (String w) { … } }

Possible application code: Example: Java class (2) Possible application code: Dict mainDict = new Dict (10000); Dict userDict = new Dict (1000); … if (! mainDict.contains (currentWord) && ! userDict.contains (currentWord)) userDict.add (currentWord); Illegal application code: userDict.size = 0; out.print (userDict.words[0]); illegal

Subclasses If C' is a subclass of C (or C is a superclass of C' ), then C' is a set of objects that are similar to one another but richer than the objects of class C: An object of class C' has all the instance variables of an object of class C, but may have extra instance variables. An object of class C' is equipped with all the instance methods of class C, but may override some of them, and may be equipped with extra instance methods.

Inheritance By default, a subclass inherits (shares) its superclass’s instance methods. Alternatively, a subclass may override some of its superclass’s instance methods, by providing more specialized versions of these methods.

Example: Java class and subclasses (1) Class declaration: class Shape { protected float x, y; public Shape () { x = 0.0; y = 0.0; } public final void move ( float dx, float dy) { x += dx; y += dy; } public void draw () { … } // draws a point at (x, y) } abbreviations for this.x and this.y

Example: Java class and subclasses (2) Subclass declaration: class Circle extends Shape { private float r; public Circle (float radius) { x = 0.0; y = 0.0; r = radius; } public void draw () { … } // draws a circle centred at (x, y) public float diameter () { return 2.0*r; } }

Example: Java class and subclasses (3) Subclass declaration: class Box extends Shape { private float w, h; public Box (…) { … } public void draw () { … } // draws a box centred at (x, y) public float width () { return w; } public float height () { return h; } }

Example: Java class and subclasses (4) Possible application code: Shape s = new Shape(); Circle c = new Circle(10.0); s.move(12.0, 5.0); c.move(3.0, 4.0); … c.diameter() … s.draw(); c.draw(); s = c; s.draw(); draws a point at (12, 5) draws a circle centred at (3, 4) ditto! (dynamic dispatch)

Overriding Each instance method of a class C is inherited by the subclass C', unless it is overridden by C'. The overriding method in class C' has the same name and type as the original method in class C. Most OO PLs allow the programmer to specify whether an instance method is virtual (may be overridden) or not: In C++, an instance method specified as virtual may be overridden. In Java, an instance method specified as final may not be overridden.

Dynamic dispatch In every OO PL, a variable of type C may refer to an object of any subclass of C. If method M is virtual, then the method call “O.M(…)” entails dynamic dispatch: The compiler infers the type of O, say class C. It then checks that class C is equipped with an instance method named M, of the appropriate type. At run-time, however, it might turn out that the receiver object is of class C', a subclass of C. The receiver object’s tag is used to determine its actual class, and hence determine which of the methods named M is to be called.

Single inheritance An OO PL supports single inheritance if each class has at most one superclass. Single inheritance gives rise to a hierarchy of classes. Single inheritance is supported by most OO PLs, including Java.

Example: Java single inheritance clone equals … Object Date y, m, d draw width height Box w, h diameter Circle r move Shape x, y Declared classes: Date (subclass of Object) Shape (subclass of Object) Circle, Box (both subclasses of Shape). Hierarchy of classes:

Multiple inheritance Multiple inheritance allows each class to have any number of superclasses. Multiple inheritance is supported by C++. Nevertheless, multiple inheritance gives rise to both conceptual and implementation problems.

Example: C++ multiple inheritance (1) Declared classes: Animal Mammal, Flier, Bird (subclasses of Animal) Cat (subclass of Mammal) Bat (subclass of Mammal, Flier) etc. Class relationships: … Mammal gestation Flier span Bird egg-size Cat Bat sonar Animal weight speed Eagle Penguin

Example: C++ multiple inheritance (2) Suppose: the Animal class defines a method named move the Mammal and Flier classes both override that method. Which method does the Bat class inherit? Bat b = …; b.move(…); Possible answers: Make it call the Mammal method. Force the programmer to choose. Make this method call illegal.