Has-a Relationships A pen “has a” or “contains a” ball.

Slides:



Advertisements
Similar presentations
Abstract Classes We often define classes as subclasses of other classes – First, define the superclass – In some cases, a superclass may not have enough.
Advertisements

Object Oriented Programming
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
C++ Classes & Data Abstraction
Reusable Classes.  Motivation: Write less code!
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Chapter 10: Introduction to Inheritance
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
Inheritance1 Inheritance Software re-use with derived classes.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
UML Class Diagram: class Rectangle
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Inheritance using Java
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Object Oriented Programming in Java Habib Rostami Lecture 6.
© 2011 Pearson Addison-Wesley. All rights reserved 9 B-1 Chapter 9 (continued) Advanced Java Topics.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Abstract Classes. Review PA – 3 Design Considerations /Web/CS239/programs/pa3/draft.php ?harrisnlCS239
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Advanced C++ Topics Chapter 8. CS 308 2Chapter 8 -- Advanced C++ Topics This chapter describes techniques that make collections of reusable software components.
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Variations on Inheritance Object-Oriented Programming Spring
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton August 19, 2005.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Web Design & Development Lecture 9
OOP: Encapsulation &Abstraction
Advanced Java Topics Chapter 9
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
CMPE 135: Object-Oriented Analysis and Design October 3 Class Meeting
Object-Oriented Programming
12 Data abstraction Packages and encapsulation
Advanced C++ Topics Chapter 8.
UML Class Diagram: class Rectangle
C++ Classes C++ Interlude 1.
Advanced Java Topics Chapter 9 (continued)
Abstract Classes.
Figure 8.1 Inheritance: Relationships among timepieces.
Advanced Java Topics Chapter 9
Polymorphism CT1513.
Abstract Classes Page
Polymorphism Polymorphism - Greek for “many forms”
Chapter 9 Carrano Chapter 10 Small Java
Chapter 8: Class Relationships
Chapter 14 Abstract Classes and Interfaces
Lecture 10 Concepts of Programming Languages
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Figure 8.1 Inheritance: Relationships among timepieces.
Presentation transcript:

Has-a Relationships A pen “has a” or “contains a” ball

Has-a Relationships Has-a relationship Also called containment Cannot be implemented using inheritance Example: To implement the has-a relationship between a pen and a ball  Define a data field point – whose type is Ball – within the class Pen Examples: when implementing Stack or Queue using ADT List we used containment!

Has-a Relationships in C++ To implement the has-a relationship in C++, we can use 2 different ways: containment – but the objects are directly contained in the containing class (not just references to them) – you have to use initialization list to initialize them private inheritance – seldom used, allows overriding the methods of contained class

Abstract Classes Example CD player and DVD player Both involve an optical disk Operations  Insert, remove, play, record, and stop such discs

Abstract Classes CDP and DVDP have an abstract base class GDP

Abstract Classes Abstract classes An abstract class is used only as the basis for subclasses It defines a minimum set of methods and data fields for its subclasses An abstract class has no instances An abstract class should, in general, omit implementations except for the methods that Provide access to private data fields Express functionality common to all of the subclasses

Abstract Classes Abstract classes (Continued) A class that contains at least one abstract method must be declared as an abstract class A subclass of an abstract class must be declared abstract if it does not provide implementations for all abstract methods in the superclass

Abstract Base Class – Example in C++ class Shape // abstract base class { public: virtual void move(int dx, int dy) =0; // pure virtual function virtual void show() const =0; // pure virtual function }; class Circle : public Shape // concrete class { int xcenter,ycenter,radius; public: Circle(int xc, int yc, int r) : xcenter(xc), ycenter(yc), radius(r) {} void move(int dx, int dy) { xcenter += dx; ycenter += dy; } void show() const { cout <<"Circle centered at ("<<xcenter<<","<<ycenter<<") with radius " <<radius<<"."<<endl; } }; class Square : public Shape // concrete class { int xll,yll,xur,yur; public: Square(int x1, int y1, int x2, int y2) : xll(x1), yll(y1), xur(x2), yur(y2) {} void move(int dx, int dy) { xll += dx; yll += dy; xur += dx; yur += dy; } void show() const { cout <<"Square with lower left corner at ("<<xll<<","<<yll <<") and upper right corner at ("<<xur<<","<<yur<<")."<<endl; } };

Abstract Base Class - Example int main() { // Shape s; // compiler error, Shape is abstract Circle c(10,20,5); Square s(0,0,15,10); Shape *array[2]={&c,&s}; cout <<"Before move:"<<endl; for (int i=0; i<2; i++) array[i]->show(); for (int i=0; i<2; i++) array[i]->move(5,-5); cout <<endl<<"After move:"<<endl; for (int i=0; i<2; i++) array[i]->show(); cin.get(); return 0; }

Generic programming ADTs we have design so far where holding references/pointers to Object in most practical application, we want to store in ADT only particular type; for instance: Integer or String typecasting from Object to desired type is troublesome one option would be rewrite the code of ADT each time storing different type (Integer instead of Object, etc.), but that’s not a good design (changes are not localized)

Generic programming – example solution: use templates Example: (implementation of Stack with linked list) public class Node { private T item; private Node next; public Node(T newItem) { item = newItem; next = null; } // end constructor public Node(T newItem, Node nextNode) { item = newItem; next = nextNode; } // end constructor

Generic programming – example public void setItem(T newItem) { item = newItem; } // end setItem public T getItem() { return item; } // end getItem public void setNext(Node nextNode) { next = nextNode; } // end setNext public Node getNext() { return next; } // end getNext } // end class Node

Generic Stack public class StackReferenceBased { private Node top; public StackReferenceBased() { top = null; } // end default constructor public boolean isEmpty() { return top == null; } // end isEmpty public void push(T newItem) { top = new Node (newItem, top); } // end push public T pop() throws StackException { if (!isEmpty()) { Node temp = top; top = top.getNext(); return temp.getItem(); } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop

Generic Stack (continued) public void popAll() { top = null; } // end popAll public T peek() throws StackException { if (!isEmpty()) { return top.getItem(); } else { throw new StackException("StackException on " + "peek: stack empty"); } // end if } // end peek } // end StackReferenceBased how to use: Stack S=new Stack (); S.push(new Character('a'));

Generic programming in Java and C++ Java: the substituted type has to be a class (hence it’s derived from Object) C++: different syntax: template class Node { private: T item; Node next; public: Node(T newItem) { item = newItem; next = NULL; } // end constructor etc.. T can be any type also int or char there are other specifics of generic programming in Java and C++ (not discussed in the course, see textbook or other material)