Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
(10-1) OOP: Inheritance in C++ D & D Chapter 20 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
Object-Oriented PHP (1)
L3-1-S1 OO Concepts © M.E. Fayad SJSU -- CMPE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 SWE Introduction to Software Engineering Lecture 23 – Architectural Design (Chapter 13)
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
1 Software Testing and Quality Assurance Lecture 28 – Testing Class Hierarchies.
Lecture 13: Object- Oriented Concepts Anita S. Malik Adapted from Schach (2004) Chapter 7.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
UML Class Diagrams: Basic Concepts. Objects –The purpose of class modeling is to describe objects. –An object is a concept, abstraction or thing that.
Inheritance using Java
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
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.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Next-generation databases Active databases: when a particular event occurs and given conditions are satisfied then some actions are executed. An active.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
E.Bertino, L.Matino Object-Oriented Database Systems 1 Chapter 5. Evolution Seoul National University Department of Computer Engineering OOPSLA Lab.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Topics Inheritance introduction
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)
Classes, Interfaces and Packages
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
ATS Application Programming: Java Programming
UML Class Diagrams: Basic Concepts
Abstract Classes.
Comp 249 Programming Methodology
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Object-Oriented PHP (1)
Chapter 11 Inheritance and Polymorphism Part 1
Inheritance Lakshmish Ramaswamy.
Chapter 7 Inheritance.
Presentation transcript:

Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”

Common properties in classes

Inheritance Software Reuse: Generalized properties defined in superclasses should be made available to subclasses without having to declare them explicitly in the subclasses. In object-oriented programming, such reuse is possible via inheritance. Inheritance is the ability of a subclass to take on the general properties of super- classes in the inheritance chain.

Using Inheritance

Class Definition

Using Inheritance Only downward propagation of properties from superclasses to subclasses is permissible. There is no upward propagation of properties in object-oriented programming. The Superclass–Subclass relationship in a class hierarchy is denoted in the code by the keyword extends.

Implementation of inheritance

class Employee extends Person {... } class Manager extends Employee {... } class SalesPerson extends Employee {... } class Secretary extends Employee {... }

State of objects

Code Reuse By allowing information of a superclass to be taken on by subclasses, the information is said to be reused at the subclass level. All newly created instances of the subclasses would have as part of their definition the inherited information.

Changes in class hierarchy Changes to software specification are inevitable. Let us consider how changes in a class hierarchy impact software maintenance as a whole. ◦ Change in property definition for all subclasses. ◦ Change in property definition for some subclasses. ◦ Adding/deleting a class.

Change in Property Definition for All Subclasses Suppose a change in representational scheme of the employee number This change will affect not only the attribute employee number but also the method getEmployeeNumber() and possibly other classes that inherit employee number.

Change in Property Definition for All Subclasses Inheritance Is Not Available ◦ Redundancy arising ◦ Inconsistency ◦ software maintenance is difficult Inheritance Is Available ◦ Redundancy can be minimized ◦ Consistency ◦ software maintenance is simple

Change in Property Definition for Some Subclasses Let us assume the following for a HomeCare employee: ◦ a manager—basic salary plus allowance; ◦ a salesperson—basic salary plus commission; ◦ a secretary—basic salary; ◦ a technician—basic salary; ◦ a clerk—basic salary;

Change in Property Definition for Some Subclasses At the Employee class, a getPay() method is defined to return the monthly pay of an employee since the method applies to all classes of employee.

Remove the getPay() method and define it individually in the subclass

Redefining getPay() method of Manager.

Adding/Deleting a Class Adding a class into an existing class hierarchy can be detrimental to the stability of the hierarchy. It is always recommended that the addition of a new class be created as a subclass in the class hierarchy. Subclassing is specialization and is thus a desired design practice in object-oriented software engineering because it has minimal impact on software maintenance.

Adding/Deleting a Class Thus, the deletion of subclasses that are not superclasses to other classes has a minimal impact on software maintenance.

Adding/Deleting a Class

Inheritance Chain Class hierarchies whose classes have only one parent or superclass. Such hierarchies are said to exhibit single inheritance. Besides single inheritance, there is also multiple inheritance. The path of inheritance over the classes is known as the inheritance chain.

Single inheritance A single inheritance chain can be single- or multilevel. In a single-level single inheritance chain, there is only one level of superclass that a subclass can inherit properties from. In contrast, in a multilevel single inheritance chain, a subclass can inherit from more than one level of superclasses.

Single vs Multi in single Inheritance

Multiple Inheritance Multiple inheritance if a subclass in the hierarchy inherits properties from two or more superclasses in more than one inheritance path.

Multiple Inheritance

Problems Associated with Multiple Inheritance

Redefined attribute and method in multiple inheritance paths.

Ambiguities in language implementation

Contract and Implementation Parts Basically, a method has two parts: contract and implementation. The contract part is also known as the method signature and specifies the name of the method, its return type and formal parameters (if any). The implementation part contains statements that are executed when a method is called.

Contract and Implementation Inheritance A class inherits property definitions and the way the properties are implemented from its superclasses in a singleinheritance chain. In other words, a class in a single inheritance chain inherits both the contract and implementation parts of the superclasses’ properties.