Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
ITEC200 – Week03 Inheritance and Class Hierarchies.
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
Dept. of Computer Engineering, Amir-Kabir University 1 Design Patterns Dr. Noorhosseini Lecture 2.
UML Class Diagram: class Rectangle
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
© 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.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
(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.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance in the Java programming language J. W. Rider.
Introduction to Object Oriented Programming CMSC 331.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
1 Computer Science 340 Software Design & Testing Inheritance.
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.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Object Oriented Software Development
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
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 Chapter Chapter
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
ISBN Object-Oriented Programming Chapter Chapter
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
Testing in OO Environment The reasons for testing is not any different for any of the design and implementation methodologies, including OO methodology.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
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 Design
Inheritance and Polymorphism
OOP What is problem? Solution? OOP
Object-Oriented Programming
Types of Programming Languages
UML Class Diagram: class Rectangle
Object Oriented Programming
Java Programming Language
Advanced Java Topics Chapter 9
Object Oriented Programming
Object-Oriented Programming
Lecture 10 Concepts of Programming Languages
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
Presentation transcript:

Inheritance Revisited Other Issues

Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not permitted in Java, but is used in C++ Also Lisp, Eiffel, etc Also Lisp, Eiffel, etc Allows a way to combine several classes Allows a way to combine several classes

The Diamond Problem virtual void print() = 0; print() A BC D Which print()?

C++ Solution Need to disambiguate the print() method by fully qualifying the members, e.g. B::print() in B. Need to disambiguate the print() method by fully qualifying the members, e.g. B::print() in B. Using nonvirtual inheritance there is still a problem with polymorphism if a and d are pointers to A and D respectively, what is Using nonvirtual inheritance there is still a problem with polymorphism if a and d are pointers to A and D respectively, what is A *a = &d; ? Way out:Use virtual base class inheritance Way out:Use virtual base class inheritance

C++ Solution This works if use this technique This works if use this technique Idea: Follow each inheritance path separately Idea: Follow each inheritance path separately In a way D is the union of B and C, so 2 A objects are created In a way D is the union of B and C, so 2 A objects are created  If A->B and A->C are virtual then only one A object is created  If virtual and non-virtual are mixed then there is one virtual A and a nonvirtual A for each nonvirtual inheritance path  D should provide the functionality of B and C

Advantages to Inheritance Defined statically, i.e. at compile time Defined statically, i.e. at compile time Easy to modify the implementation being reused Easy to modify the implementation being reused SW reuse is greatly facilitated SW reuse is greatly facilitated

Disadvantages to Inheritance Cannot change implementations inherited from parent class at run-time Cannot change implementations inherited from parent class at run-time Parent classes often define at least part of their subclasses implementation Parent classes often define at least part of their subclasses implementation  A change to the parent entails a change to the subclass  “Inheritance breaks encapsulation”  Best to inherit from an abstract class

Inheritance: Controlling Access Modifiers Modifiers  public accessible everywhere  private only accessible in defining class  protected accessible within all subclasses  accessible to classes in package  final no subclass, no override  static one copy, the class  abstract implementation in subclass final is the opposite of abstract final is the opposite of abstract

The Modifiers static and final static applied both to data and methods static applied both to data and methods static shared by all instances static shared by all instances static means data/method is not part of an instance static means data/method is not part of an instance final means change/override no more final means change/override no more static final variables should be private static final variables should be private

Benefits of Inheritance White-box reuse: reuse code/data from parent White-box reuse: reuse code/data from parent Increased reliability--reuse exposes bugs Increased reliability--reuse exposes bugs Code sharing : define a class in one place but use it in several, e.g. Java library Code sharing : define a class in one place but use it in several, e.g. Java library Interface consistency: parent propogates same behavior Interface consistency: parent propogates same behavior Rapid prototyping, e.g. beans Rapid prototyping, e.g. beans Frameworks: structure a generic solution to a common problem and use polymorphism for specific solutions Frameworks: structure a generic solution to a common problem and use polymorphism for specific solutions

Cost of Inheritance Using polymorphism together with inheritance means run-time lookup to decide which method is executed: dynamic binding Using polymorphism together with inheritance means run-time lookup to decide which method is executed: dynamic binding Program size: lots of superfluous baggage in importing from libraries Program size: lots of superfluous baggage in importing from libraries Message-passing overhead: Sending information back and forth between objects is not really that expensive Message-passing overhead: Sending information back and forth between objects is not really that expensive

Cost of Inheritance Program complexity: coupling high in an inheritance hierarchy means propogating changes is difficult: refactoring (later) Program complexity: coupling high in an inheritance hierarchy means propogating changes is difficult: refactoring (later) HW: Budd, ch. 8, due 17 May

Alternates to Inheritance Inheritance isn’t the only way to relate classes to each other Inheritance isn’t the only way to relate classes to each other  We can also use composition (later)  Use instance variables that refer to other objects  Embodies the has_a relation better than inheritance  Has a beautiful recursive formulation (allows unified treatment of composite and primitive objects)

For Example Instead of inheriting a cockroach from an insect Instead of inheriting a cockroach from an insect Insect Cockroach class Insect{ //... } class Cockroach extends Insect{ //... }

For Example Relate Cockroach to Insect by composition: Relate Cockroach to Insect by composition: class Insect{...// } class Cockroach{ private Insect critter= new Insect(); //... }

UML Diagram critter Cockroach Insect

Guideline Prefer composition to inheritance Prefer composition to inheritance Composition does not break encapsulation Composition does not break encapsulation