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

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

Inheritance -- Introduction

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

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

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

Chapter 6 Reflection

Inheritance and Polymorphism

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

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

Inheritance and Polymorphism

Programming II (CS300)

Java Object Oriented Design. CSC207 Fall 2014

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

Programming II (CS300)

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

ITI Introduction to Computing II

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

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

Programming in C# Inheritance and Polymorphism

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

COMP 250 Fall inheritance Nov. 17, 2017

ITI Introduction to Computing II

Polymorphism and Inheritance

CSCE3193: Programming Paradigms

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

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

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

Inheritance, Polymorphism, and Interfaces

CS-202 Introduction to Object Oriented Programming

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

Computer Science 210: Data Structures

OVERRIDING. 7/11/2015 Budditha Hettige 82

CMSC 132: Object-Oriented Programming II

CS111: PROGRAMMING LANGUAGE II

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

PROGRAMMING LANGUAGE 2

CS313D: ADVANCED PROGRAMMING LANGUAGE

C++ Important Questions with Answers

Lecture 18 CSE11 Fall 2013 Inheritance

Polymorphism. Agenda

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

CS260 Intro to Java & Android 03.Java Language Basics

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

Practice for Chapter 11

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

Cmpt 135 Assignment 2: Solutions and Marking Rubric Feb 22 nd 2016 Due: Mar 4th 11:59pm

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

8. Polymorphism and Inheritance

What is Inheritance?

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

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

Inheritance and Interfaces

Java Fundamentals (II)

Object-Oriented Concepts

Inheritance (IS A Relationship)

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Overriding Variables: Shadowing

CS1004: Intro to CS in Java, Spring 2005

CS1150 Principles of Computer Science Objects and Classes

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

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

COP 3330 Final Exam Review

Arrays Classes & Methods, Inheritance

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

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

Chapter 9 Inheritance

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

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

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

BBM 102 Introduction to Programming II Spring Inheritance

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

Inheritance. Transitivity

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

More on Inheritance. Interfaces & Abstract Classes

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 10: Inheritance

INHERITANCE AND EXTENDING CLASSES

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

CS111: PROGRAMMING LANGUAGE II

JAVA MOCK TEST JAVA MOCK TEST II

COMP 110/L Lecture 19. Kyle Dewey

Java: introduction to object-oriented features

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Chapter 5 Object-Oriented Programming

Programming overview

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

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

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

Introduction to Inheritance

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Declarations and Access Control SCJP tips

CS 251 Intermediate Programming Inheritance

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Building custom components IAT351

Example: Count of Points

CMSC 132: Object-Oriented Programming II

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

Transcription:

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

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

Inheritance Inheritance is based on an is-a relationship The child is a more specific version of the parent Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class (Note: Similar graphic notation as Interface) Vehicle Solid line instead of dotted line Car 3

Inheritance 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 However, a programmer can tailor a derived class as needed by adding new variables or by overriding some of the inherited methods 4

Inheritance vs. Interfaces General Idea: a Vehicle can start(), turn(), etc. A Car IS-A Vehicle, so it can start(), turn(), etc. Two ways to go: Vehicle Car A Car IS-A Vehicle, and because of the interface, we know it has all the actions (methods) prescribed for a Vehicle. Car has its own code for start(), turn(), etc. with no code at all in Vehicle.java Vehicle A Car IS-A Vehicle, and because of the inheritance, we know that Car can depend on start(), turn(), etc., coded in Vehicle.java (and maybe also in Car.java) Car 5

Inheritance vs. Interfaces An interface is lightweight, just an idea Inheritance is heavyweight, full of code that may or may not be exactly what you want for a certain class Knowing when to use inheritance is very tricky When in doubt, use an interface to gather classes together under one roof Note that inheritance is in common use, so need to be able to understand it 6

Deriving Subclasses In Java, we use the reserved word extends to establish an inheritance relationship public class Car extends Vehicle { // class contents } 7

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 8

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 in any class that is a child of the class where it is defined Protected variables and methods can be shown with a # symbol in UML class diagrams 9

Class Diagram for Words # pages : int Book + pagemessage() : void Words + main (args : String[]) : void - definitions : int Dictionary + definitionmessage() : void 10

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 invoke the parent's constructor public class Child extends Parent { public Child() } { } super(); // a call to Parent() // plus whatever code we need for Child 11

The super Reference A child s 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 The super reference can also be used to reference (with a dot.) other variables and methods defined in the parent s class 12

Overriding Methods A child class can override the definition of an inherited method in favor of its own The new method must have the same signature as the parent's method, but can have a different body In Vehicle: public boolean hastrunk { return false; } In Car: public boolean hastruck { return true; } Here we say method hastrunk of Car overrides hastruck of Vehicle: it behaves differently 13

Overriding Methods The class of the object used to execute an overridden method determines which version of the method is invoked If have Vehicle v and Car c, then v.hastrunk() returns false c.hastrunk() returns true In the child, the method in the parent class can be invoked explicitly using the super reference If a method is declared in the parent class with the final modifier, it cannot be overridden 14

Overloading vs. Overriding Overloading deals with multiple methods with the same name in the same class that have different signatures (different parameter lists) Overriding deals with two methods (one in a parent class and one in a child class) that have the same signature (same parameter list) Overloading lets you define a similar operation in different ways (using different input parameters) Overriding lets you redefine a parent s method in a child (using the same input parameters) 15

Class Hierarchies A child class of one parent can be the parent of another child, forming a class hierarchy Two children of the same parent are called siblings Business RetailBusiness ServiceBusiness KMart Macys Kinkos 16

Class Hierarchies A child class inherits from all its ancestor classes An inherited variable, constant, or method is passed continually down the line (unless it is declared private) Common features should be put as high in the hierarchy as is reasonable There is no single class hierarchy that is appropriate for all situations 17

The Object Class A class called Object is defined in the java.lang package of the Java standard class library All classes 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 Therefore, the Object class is the ultimate root of all class hierarchies 18

The Object Class 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 other information (e.g. the address of its location in memory) System.out.println(new Object()); java.lang.object@952905 Every time we define the tostring method, we are actually overriding the inherited definition 19

The Object Class The equals method of the Object class returns true if the 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 the 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

Abstract Classes An abstract class is a placeholder in a class hierarchy that represents a generic concept An abstract class cannot be instantiated, it can only be extended in a class hierarchy It s like an interface but can have some code Don t worry about it for cs110 it s just in the book for completeness, without any examples! 21