Inheritance -- Introduction

Similar documents
11/19/2014. Inheritance. Chapter 7: Inheritance. Inheritance. Inheritance. Java Software Solutions for AP* Computer Science A 2nd Edition

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Inheritance and Polymorphism

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Chapter 6 Reflection

Polymorphism. Final Exam. November 26, Method Overloading. Quick Review of Last Lecture. Overriding Methods.

Programming in C# Inheritance and Polymorphism

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CS-202 Introduction to Object Oriented Programming

Chapter 5 Object-Oriented Programming

Polymorphism. Agenda

Object Oriented Programming. Java-Lecture 11 Polymorphism

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Java Object Oriented Design. CSC207 Fall 2014

Lecture 18 CSE11 Fall 2013 Inheritance

Inheritance and Polymorphism

Polymorphism. Polymorphism is an object-oriented concept that allows us to create versatile software designs

More On inheritance. What you can do in subclass regarding methods:

What is Inheritance?

Practice for Chapter 11

Programming II (CS300)

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

CS313D: ADVANCED PROGRAMMING LANGUAGE

C++ Important Questions with Answers

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Computer Science II (20073) Week 1: Review and Inheritance

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Polymorphism and Inheritance

Inheritance CSC 123 Fall 2018 Howard Rosenthal

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

OVERRIDING. 7/11/2015 Budditha Hettige 82

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

9/10/2018 Programming Data Structures Inheritance

Introductory Programming Inheritance, sections

CS313D: ADVANCED PROGRAMMING LANGUAGE

Programming II (CS300)

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

PROGRAMMING LANGUAGE 2

Comp 249 Programming Methodology

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

CLASS DESIGN. Objectives MODULE 4

Chapter 7. Inheritance

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

CMSC 132: Object-Oriented Programming II

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Big software. code reuse: The practice of writing program code once and using it in many contexts.

CSCE3193: Programming Paradigms

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Overriding Variables: Shadowing

Example: Count of Points

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Inheritance and object compatibility

Introduction to Inheritance

Inheritance. Chapter 7. Chapter 7 1

CS111: PROGRAMMING LANGUAGE II

Lecture 4: Extending Classes. Concept

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Polymorphism. return a.doublevalue() + b.doublevalue();

3. Can an abstract class include both abstract methods and non-abstract methods? D

ITI Introduction to Computing II

What are the characteristics of Object Oriented programming language?

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

8. Polymorphism and Inheritance

CIS 110: Introduction to computer programming

CS Programming I: Inheritance

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

More Relationships Between Classes

Implements vs. Extends When Defining a Class

Binghamton University. CS-140 Fall Dynamic Types

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

INHERITANCE. Spring 2019

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Java Fundamentals (II)

Java: introduction to object-oriented features

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

ITI Introduction to Computing II

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Inheritance, Polymorphism, and Interfaces

Inheritance. Quick Review of Last Lecture. November 12, Passing Arguments. Passing Arguments. Variable Assignment Revisited

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Data Structures (list, dictionary, tuples, sets, strings)

Inheritance Motivation

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Method Resolution Approaches. Dynamic Dispatch

First IS-A Relationship: Inheritance

CS 251 Intermediate Programming Inheritance

Transcription:

Inheritance -- Introduction Another fundamental object-oriented technique is called inheritance, which, when used correctly, supports reuse and enhances software designs Chapter 8 focuses on: the concept of inheritance inheritance in Java the protected modifier adding and modifying methods through inheritance creating class hierarchies 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 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 In programming, the child class inherits the methods and data defined for the parent class 2

Inheritance Inheritance relationships are often shown graphically, with the arrow pointing to the parent class: Vehicle Car Inheritance should create an is-a relationship, meaning the child is-a more specific version of the parent 3

Deriving Subclasses In Java, the reserved word extends is used to establish an inheritance relationship class Car extends Vehicle { // class contents } See Words.java 4

The protected Modifier The visibility modifiers determine which class members get inherited and which do not Variables and methods declared with public visibility are inherited, and those with private visibility are not But public variables violate our goal of encapsulation The protected visibility modifier allows a member to be inherited, but provides more protection than public does The details of each modifier are given in Appendix F 5

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 is often used to invoke the parent's constructor See Words2.java 6

Defined vs. Inherited A subtle feature of inheritance is the fact that even if a method or variable is not inherited by a child, it is still defined for that child An inherited member can be referenced directly in the child class, as if it were declared in the child class But even members that are not inherited exist for the child, and can be referenced indirectly through parent methods See Eating.java and School.java 7

Overriding Methods A child class can override the definition of an inherited method in favor of its own That is, a child can redefine a method it inherits from its parent The new method must have the same signature as the parent's method, but can have different code in the body The object type determines which method is invoked See Messages.java 8

Overloading vs. Overriding Don't confuse the concepts of overloading and overriding Overloading deals with multiple methods in the same class with the same name but different signatures Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature Overloading lets you define a similar operation in different ways for different data Overriding lets you define a similar operation in different ways for different object types 9

The super Reference Revisited The super reference can be used to invoke any method from the parent class This ability is often helpful when using overridden methods The syntax is: super.method(parameters) See Firm.java and Accounts.java 10

Class Hierarchies A child class of one parent can be the parent of another child, forming class hierarchies: Business Retail_Business Service_Business Macy's K-Mart Kinko's 11

Class Hierarchies Two children of the same parent are called siblings Good class design puts all common features as high in the hierarchy as is reasonable Class hierarchies often have to be extended and modified to keep up with changing needs There is no single class hierarchy that is appropriate for all situations See Accounts2.java 12

The Object Class All objects are derived from the Object class If a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object class The Object class is therefore the ultimate root of all class hierarchies The Object class contains a few useful methods, such as tostring(), which are inherited by all classes See Test_toString.java 13

References and Inheritance An object reference can refer to an object of its class, or to an object of any class related to it by inheritance For example, if the Holiday class is used to derive a child class called Christmas, then a Holiday reference could actually be used to point to a Christmas object: Holiday day; day = new Christmas(); 14

References and Inheritance Assigning a predecessor object to an ancestor reference is considered to be a widening conversion, and can be performed by simple assignment Assigning an ancestor object to a predecessor reference can also be done, but it is considered to be a narrowing conversion and must be done with a cast The widening conversion is the most useful 15

Polymorphism A polymorphic reference is one which can refer to one of several possible methods Suppose the Holiday class has a method called celebrate, and the Christmas class overrode it Now consider the following invocation: day.celebrate(); If day refers to a Holiday object, it invokes Holiday's version of celebrate; if it refers to a Christmas object, it invokes that version 16

Polymorphism In general, it is the type of the object being referenced, not the reference type, that determines which method is invoked See Messages2.java Note that, if an invocation is in a loop, the exact same line of code could execute different methods at different times Polymorphic references are therefore resolved at runtime, not during compilation 17

Polymorphism Note that, because all classes inherit from the Object class, an Object reference can refer to any type of object A Vector is designed to store Object references The instanceof operator can be used to determine the class from which an object was created See Variety.java 18

Polymorphism See Firm2.java Staff_Member Employee Volunteer Hourly Executive 19