Inheritance 1.  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.

Slides:



Advertisements
Similar presentations
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Advertisements

Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance CT Introduction 2  Inheritance  Software reusability  Create new class from existing class  Absorb existing class’s data and behaviors.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
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. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted.
Computer Science I Inheritance Professor Evan Korth New York University.
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
© 2004 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 : Inheritance Intermediate Java Programming Summer 2007.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
What is inheritance? It is the ability to create a new class from an existing class.
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
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,
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.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
Coming up: Inheritance
Topics Inheritance introduction
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance CT Introduction Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance CT Introduction Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
Advanced Programming in Java
Lecture 12 Inheritance.
Chapter 8 Inheritance.
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,
03/10/14 Chapter 9 Inheritance.
Week 4 Object-Oriented Programming (1): Inheritance
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance CT1513.
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
Overriding Methods & Class Hierarchies
Inheritance CT1513.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Inheritance 1

 Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or superclass, or base class  The derived class is called the child class or subclass.  As the name implies, the child inherits characteristics of the parent  That is, the child class inherits the methods and data defined for the parent class 2

Inheritance  To tailor a derived class, the programmer can add new variables or methods, or can modify the inherited ones  Software reuse is at the heart of inheritance  By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software 3

Inheritance  Inheritance relationships often are shown graphically in a UML class diagram, with an arrow with an open arrowhead pointing to the parent class 4 Inheritance should create an is-a relationship, meaning the child is a more specific version of the parent Person Employee

Superclasses and subclasses  Superclasses and subclasses  Object of one class “is an” object of another class  Example: lexus is car.  Class lexus inherits from class car  car : superclass  lexus : subclass  Superclass typically represents larger set of objects than subclasses  Example:  superclass: Vehicle  Cars, trucks, boats, bicycles, …  subclass: Car  Smaller, more-specific subset of vehicles 5

Examples 6

7 Fig. 9.2 | Inheritance hierarchy for university CommunityMembers

Examples 8 Fig. 9.3 | Inheritance hierarchy for Shapes.

Multiple Inheritance  Java supports single inheritance, meaning that a derived class can have only one parent class  Multiple inheritance allows a class to be derived from two or more classes, inheriting the members of all parents  Java does not support multiple inheritance 9

Deriving Subclasses  In Java, we use the reserved word extends to establish an inheritance relationship 10 class Employee extends Person { // class contents }

Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another class. The subclass inherits all data attributes of its superclass The subclass inherits all methods of its superclass The subclass inherits all associations of its superclass The subclass can: Add new functionality Use inherited functionality Override inherited functionality Person - name: String - dob: Date Employee - employeeID: int - salary: int - startDate: Date superclass: subclass: 11

Person - name: String - dob: Date Employee - employeeID: int - salary: int - startDate: Date public class Person { private String name; private Date dob; [...] public class Employee extends Person { private int employeID; private int salary; private Date startDate; [...] Employee anEmployee = new Employee(); 12

Each Java class has one (and only one) superclass. Inheritance creates a class hierarchy Classes higher in the hierarchy are more general and more abstract Classes lower in the hierarchy are more specific and concrete Class There is no limit to the number of subclasses a class can have There is no limit to the depth of the class tree. Class 13

14 Suppose we want implement a class Employee which has two attributes, id and name, and some basic get- and set- methods for the attributes. We want now define a PartTimeEmployee class; this class will inherit these attributes and methods, but can also have attributes (hourlyPay) and methods of its own (calculateWeeklyPay). Case Study 1:

The super Reference  Constructors are not inherited, even though they have public visibility  Yet we often want to use the parent's constructor to set up the "parent's part" of the object  The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor 15

The super Reference  A child’s constructor is responsible for calling the parent’s constructor  The first line  The first line of a child’s constructor should use the super reference to call the parent’s constructor  The super reference can also be used to reference other variables and methods defined in the parent’s class public class SubClass extends SuperClass { public SubClass(………….) { super (………); //DATA MEMBERS ……. } } 16

Constructors - Example public class BankAccount {private String ownersName; private int accountNumber; private float balance; public BankAccount(int anAccountNumber, String aName) { accountNumber = anAccountNumber; ownersName = aName; } public class OverdraftAccount extends BankAccount { private float overdraftLimit; public OverdraftAccount(int anAccountNumber, String aName, float aLimit) { super(anAccountNumber, aName); overdraftLimit = aLimit; } 17

The protected Modifier  Visibility modifiers determine which class members are inherited and which are not  Variables and methods declared with public visibility are inherited; those with private visibility are not  But public variables violate the principle of encapsulation  There is a third visibility modifier that helps in inheritance situations: protected 18

The protected Modifier  The protected modifier allows a member of a base class to be inherited into a child  Protected visibility provides more encapsulation than public visibility does  However, protected visibility is not as tightly encapsulated as private visibility  Protected variables and methods can be shown with a # symbol preceding them in UML diagrams 19

20 Protected Visibility for Superclass Data  private data are not accessible to subclasses!  protected data fields accessible in subclasses

UML Diagram for Words 21 Person - name: String #lastname : string + age : int Employee - employeeID: int - salary: int - startDate: Date

Protected visibility public class Person { private String name; protected String LastName; Public int age; } public class Employee extends Person { public void Setter () { name = “Sara”; LastName = “Ali”; Age = 20; } Employee anEmployee = new Employee(); System.out.print(anEmplyee.name); System.out.print(anEmplyee.LastName); System.out.print(anEmplyee.age); 22 Clint class

Inheritance and Member Accessibility  We use the following visual representation of inheritance to illustrate data member accessibility. 23

The Effect of Three Visibility Modifiers 24

Accessibility of Super from Sub  Everything except the private members of the Super class is visible from a method of the Sub class. 25

Accessibility from Unrelated Class class Client { public void main (String [] args) { Super mySuper = new Super(); Sub mySub = new Sub(); int i = mySuper.pub_Super_Field; int j = mySub.pub_Super_Field; // inherited int k = mySub.pub_Sub_Field; } VALID  class Super { public int pub_Super_Field; protected int pro_Super_Field; private int pri_Super_Field; public Super () { pub_Super_Field = 10; pro_Super_Field = 20; pri_Super_Field = 30; } class Sub extends Super { public int pub_Sub_Field; protected int pro_Sub_Field; private int pri_Sub_Field; public Sub () { pub_Sub_Field = 100; pro_Sub_Field = 200; pri_Sub_Field = 300; } :Super :Sub :Client

Accessibility from Unrelated Class class Client { public void test() { Super mySuper = new Super(); Sub mySub = new Sub(); int l = mySuper.pri_Super_Field; int m = mySub.pri_Sub_Field; int n = mySub.pri_Super_Field; } class Super { public int pub_Super_Field; protected int pro_Super_Field; private int pri_Super_Field; public Super() { pub_Super_Field = 10; pro_Super_Field = 20; pri_Super_Field = 30; } class Sub extends Super { public int pub_Sub_Field; protected int pro_Sub_Field; private int pri_Sub_Field; public Sub() { pub_Sub_Field = 100; pro_Sub_Field = 200; pri_Sub_Field = 300; } NOT VALID  :Super :Sub :Client

Accessibility from Unrelated Class class Client { public void test() { Super mySuper = new Super(); Sub mySub = new Sub(); int o = mySuper.pro_Super_Field; int p = mySub.pro_Sub_Field; int q = mySub.pro_Super_Field; } class Super { public int pub_Super_Field; protected int pro_Super_Field; private int pri_Super_Field; public Super() { pub_Super_Field = 10; pro_Super_Field = 20; pri_Super_Field = 30; } class Sub extends Super { public int pub_Sub_Field; protected int pro_Sub_Field; private int pri_Sub_Field; public Sub() { pub_Sub_Field = 100; pro_Sub_Field = 200; pri_Sub_Field = 300; } NOT VALID  :Super :Sub :Client

Accessibility of Super from Super class Super { public void superToSuper(Super anotherSuper) { int i = anotherSuper.pub_Super_Field; int j = anotherSuper.pro_Super_Field; int k = anotherSuper.pri_Super_Field; } VALID  :Super anotherSuper

Accessibility of Sub from Sub class Sub extends Super { public void subToSub(Sub anotherSub) { int i = anotherSub.pub_Sub_Field; int j = anotherSub.pro_Sub_Field; int k = anotherSub.pri_Sub_Field; int l = anotherSub.pub_Super_Field; //inherited int m = anotherSub.pro_Super_Field; int n = anotherSub.pri_Super_Field; } VALID  NOT VALID   :Sub anotherSub

Accessibility of Sub from Super class Super { public void superToSub(Sub sub) { int i = sub.pub_Sub_Field; int j = sub.pro_Sub_Field; int k = sub.pri_Sub_Field; } VALID  NOT VALID  :Super :Sub sub

Accessibility of Super from Sub 4 th Ed Cha pter class Sub extends Super { public void subToSuper(Super mySuper) { int i = mySuper.pub_Super_Field; int j = mySuper.pro_Super_Field; int k = mySuper.pri_Super_Field; } VALID  NOT VALID  :Sub :Super mySuper