Code reuse through subtyping

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
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.
Topics Recap of the Object Model Inheritance Polymorphism – virtual functions Abstract classes, Pure virtual functions Design issues UML examples Templates.
Chapter 10: Introduction to Inheritance
More about classes and objects Classes in Visual Basic.NET.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
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.
Inheritance COMP53 Sept Inheritance Inheritance allows us to define new classes by extending existing classes. A child class inherits all members.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
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. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
An Object-Oriented Approach to Programming Logic and Design
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
What is inheritance? It is the ability to create a new class from an existing class.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Inheritance Building one object from another. Background Object-oriented programming is normally described has offering three capabilities Encapsulation:
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
ISBN Object-Oriented Programming Chapter Chapter
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Inheritance vs Composition Picking the right tool.
© 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.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle Circle & Rectangle could use toString() but chose.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities.
Lecture 12 Inheritance.
One class is an extension of another.
Chapter 11 Object-Oriented Design
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Advanced Programming in Java
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Recitation 6.
One class is an extension of another.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Programming Behnam Hatami Fall 2017.
Virtual Functions & Polymorphism
Object-Oriented Programming: Inheritance
Fundaments of Game Design
Object Oriented Analysis and Design
Chapter 8 Class Inheritance and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 8 Inheritance Part 2.
Presentation transcript:

Code reuse through subtyping Inheritance Basics Code reuse through subtyping

Inheritance : What is It Technical mechanism for code reuse Conceptual design mechanism : is-a relationship

Inheritance Terminology Inheritance : derive a class from another class Terminology Parent or Base class Derived or Child class UML Arrow from derived to parent class

Conceptual Model How to visualize inheritance: Apple consists of a Fruit object and an Apple wrapper Wrapper can add new features / hide existing ones Fruit Apple Other Code

Inheritance Heirarchies Multiple classes can derive from one base Can derive from a class that is derived

Geometric Object Example Function also available in Circle & Rectagle Circle & Rectangle could use toString() but chose to override

Circle Interactions Calls on a derived class handled by code there if possible, otherwise by parent class Geometric Object Circle

Circle Code State inheritance with : public ParentClass Do not redeclare parent members unless overriding them

Using Inherited Members Derived class does NOT have access to private members of parent:

Using Inherited Members Derived class does NOT have access to private members of parent Must use public interface to access

Generic Programming A derived class can be treated as parent type:

Protected Access Private Only accessible in this class

Protected Access Private Only accessible in this class Public Accessible anywhere

Protected Access Private Only accessible in this class Public Accessible anywhere Protected Accessible in this class and any subclasses

Access Summary Private Protected Public Member variables Helper functions no one else should use Protected Member variables it is reasonable for child classes to work with Functions you only want subclasses to use Public Functions Constants