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

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

Inheritance -- Introduction

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

ECE 122. Engineering Problem Solving with Java

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

ECE 122. Engineering Problem Solving with Java

Chapter 6 Reflection

Inheritance and Polymorphism

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

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

Programming II (CS300)

Programming II (CS300)

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance and Polymorphism

ITI Introduction to Computing II

Chapter 5 Object-Oriented Programming

ITI Introduction to Computing II

QUESTIONS FOR AVERAGE BLOOMERS

PROGRAMMING LANGUAGE 2

Programming in C# Inheritance and Polymorphism

Inheritance CSC 123 Fall 2018 Howard Rosenthal

CS-202 Introduction to Object Oriented Programming

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

Polymorphism and Inheritance

CSCE3193: Programming Paradigms

CS313D: ADVANCED PROGRAMMING LANGUAGE

Polymorphism. Agenda

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

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

What is Inheritance?

Lecture 18 CSE11 Fall 2013 Inheritance

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

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

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

Understanding Inheritance and Interfaces

BBM 102 Introduction to Programming II Spring Inheritance

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

Lecture 2: Java & Javadoc

CS1150 Principles of Computer Science Objects and Classes

Chapter 4: Writing Classes

Inheritance (IS A Relationship)

CLASSES AND OBJECTS IN JAVA

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

CMSC 132: Object-Oriented Programming II

Chapter 10 Classes Continued. Fundamentals of Java

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

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

Anatomy of a Class Encapsulation Anatomy of a Method

CS111: PROGRAMMING LANGUAGE II

Overriding Variables: Shadowing

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

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

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Week 7 Inheritance. Written by Alexandros Evangelidis. Adapted from Joseph Gardiner. 10 November 2015

CS111: PROGRAMMING LANGUAGE II

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

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

C++ Important Questions with Answers

CS 112 Introduction to Programming

Admin. q Admin and recap q Class inheritance

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

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

Chapter 10: Inheritance

JAVA MOCK TEST JAVA MOCK TEST II

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

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

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

8. Polymorphism and Inheritance

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Practice for Chapter 11

CS1004: Intro to CS in Java, Spring 2005

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

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

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System?

Arrays Classes & Methods, Inheritance

More on Objects in JAVA TM

COP 3330 Final Exam Review

Inheritance, Polymorphism, and Interfaces

Comp 249 Programming Methodology

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

Inheritance and Encapsulation. Amit Gupta

INHERITANCE. Spring 2019

Computer Science 210: Data Structures

Inheritance. Transitivity

CS111: PROGRAMMING LANGUAGE II

Chapter 11: Inheritance

Chapter 6 Introduction to Defining Classes

CS 251 Intermediate Programming Inheritance

Inheritance and object compatibility

Transcription:

CS257 Computer Science I Kevin Sahr, PhD Lecture 10: Inheritance 1 Object Oriented Features For a programming language to be called object oriented it should support the following features: 1. objects: group data and behavior together 2. encapsulation: control access to object internals through a welldefined interface 3. inheritance 4. polymorphism In this lecture we will discuss inheritance We will discuss polymorphism later 2 Inheritance Inheritance is the ability to derive a new class from an existing one The existing class is called the parent class, superclass, or base class The derived class is called the child class, subclass, or derived class The child class inherits the methods and data defined by the parent class the parent class methods and data are available in the child class just as if they had been declared there (with some exceptions we will discuss) 3 Inheritance Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class Animal Snake Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent EX: a Snake is-a Animal Good design requires that inheritance only be used in situations where an is-a sentence makes sense 4

Inheritance A programmer can tailor a derived class as needed by adding new variables or methods, or by modifying the inherited ones Software reuse is a fundamental benefit 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 Inheritance also enhances maintainability fixing a bug in the base class automatically fixes the error for all subclasses adding a feature to the base class automatically adds that feature to all sub-classes 5 Deriving Subclasses In Java, we use the reserved word extends to establish an inheritance relationship public class Snake extends Animal { // class contents } See Animal.java and Snake.java 6 The protected Modifier Visibility modifiers affect the way that class members can be used in a child class Variables and methods declared with private visibility cannot be referenced by name in a child class They can be referenced in the child class if they are declared with public visibility -- but public variables violate the principle of encapsulation There is a third visibility modifier that helps in inheritance situations: protected 7 The protected Modifier The protected modifier allows a child class to reference a variable or method directly in the child class It provides more encapsulation than public visibility, but is not as tightly encapsulated as private visibility A protected variable is visible to any class in the same package as the parent class protected variables and methods are shown with a # symbol preceding them in UML diagrams recall that - indicates private, and + indicates public 8

Visibility and Access All variables and methods of a parent class (even private ones) are inherited by its children a copy of all instance variables of all ancestor classes is allocated for each newly created child class object But as we've mentioned, private members cannot be referenced by name in the child class Because the parent can refer to it s private members, the child can reference them indirectly using the parent's methods (for example, the parent getter or setter method for an instance variable) 9 The super Reference Recall that the this reference allows an object to refer to itself Sometimes an object needs to refer to the parent s part of the object The super reference can be used to refer to the parent class object The super reference can be used to reference variables and methods defined in the parent s class (assuming they are visible to the subclass) 10 Parent Constructor Constructors are not inherited, even though they have public visibility A child class constructor is responsible for calling the parent s constructor The first line of a child s constructor should use the super reference to call the parent s constructor See Animal.java and Snake.java 11 Overriding Methods A child class can override the definition of an inherited method in favor of its own This is done by defining a new version of the method in the child class The new method must have the same signature as the parent's method, but can have a different body The class of the object executing the method determines which version of the method is invoked See Animal.java and Snake.java 12

Overriding A method defined in the parent class can always be invoked explicitly using the super reference, even if that method has been overridden in the child class EX: invoke the dosomething method as defined in my parent class: super.dosomething(); If a method is declared with the final modifier, it cannot be overridden in a subclass 13 Overloading vs. Overriding Overloading deals with multiple methods with the same name in the same class, but with 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 parameters Overriding lets you define a similar operation in different ways for different object types 14 Class Hierarchies A child class of one parent can be the parent of another child, and so forth, forming a class hierarchy Business RetailBusiness ServiceBusiness KMart Macys Kinkos 15 Class Hierarchies Two children of the same parent are called siblings An inherited member is passed continually down the line Therefore, a child class inherits from all its ancestor classes 16

Class Hierarchy Design There is no single class hierarchy that is appropriate for all situations Make sure all inheritance relationships form valid is-a sentences Common features should be put as high in the hierarchy as possible this maximizes software reuse 17 The Object Class The Object class is defined in the java.lang package of the Java standard class library All classes are derived from the Object class If a class does not explicitly define a parent class, then that class is automatically a subclass of the Object class the Object class is the ultimate root of all class hierarchies because the Object class is always part of every class hierarchy it is not usually depicted in UML Class Hierarchy diagrams 18 The Object Class tostring Method The Object class contains a few useful methods, which are inherited by all classes For example, the tostring method is defined in the Object class The tostring method in the Object class is defined to return a string that contains the name of the object s class along with some other internal information Every time we define the tostring method in one of our classes, we are actually overriding the inherited definition When tostring is invoked on one of our objects, our overridden definition is executed Often we can use the parent class tostring (super.tostring()) to get the parent class portion of our String representation 19 The Object Class equals Method The equals method is defined in the Object class returns true if two references are aliases We can override equals in any class to define equality in some more appropriate way As we've seen, the String class defines the equals method to return true if two String objects contain the same characters The designers of the String class have overridden the equals method inherited from Object in favor of a more useful version 20

A Complete Example see the Animal class UML diagram and source code 21 Lecture 10 Vocabulary inheritance parent/super/base class child/sub/derived class software reuse protected super override class hierarchy sibling 22