ECE 122. Engineering Problem Solving with Java

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

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

Inheritance -- Introduction

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

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

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

Chapter 6 Reflection

Darrell Bethea June 6, 2011

PROGRAMMING LANGUAGE 2

Inheritance and Polymorphism

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

INHERITANCE AND EXTENDING CLASSES

Programming II (CS300)

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance and Polymorphism

Inheritance. Transitivity

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

CMSC 132: Object-Oriented Programming II. Inheritance

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

Lecture 18 CSE11 Fall 2013 Inheritance

Programming in C# Inheritance and Polymorphism

CIS 110: Introduction to computer programming

Building custom components IAT351

What is Inheritance?

ITI Introduction to Computing II

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

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

Chapter 5 Object-Oriented Programming

Chapter 7. Inheritance

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

ITI Introduction to Computing II

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

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

Inheritance. Chapter 7. Chapter 7 1

Object Oriented Programming. I. Object Based Programming II. Object Oriented Programming

OVERRIDING. 7/11/2015 Budditha Hettige 82

9/10/2018 Programming Data Structures Inheritance

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

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Systems programming. Object Oriented Programming. I. Object Based Programming II. Object Oriented Programming

Comp 249 Programming Methodology

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Java Fundamentals (II)

Practice for Chapter 11

Programming II (CS300)

Overriding Variables: Shadowing

Object Orientated Programming Details COMP360

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

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

Chapter 5. Inheritance

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

8. Polymorphism and Inheritance

CS 112 Introduction to Programming

CS105 C++ Lecture 7. More on Classes, Inheritance

CS-202 Introduction to Object Oriented Programming

Admin. q Admin and recap q Class inheritance

Programming Language Concepts: Lecture 2

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Object-Oriented Concepts

Inheritance and Encapsulation. Amit Gupta

CMSC 132: Object-Oriented Programming II

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

Programming overview

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Polymorphism and Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Inheritance 2. Inheritance 2. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

CS111: PROGRAMMING LANGUAGE II

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Lecture 4: Extending Classes. Concept

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

C++ Important Questions with Answers

CSCE3193: Programming Paradigms

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

Review of Object-Oriented Concepts in JAVA

Week 5-1: ADT Design

Basic Object-Oriented Concepts. 5-Oct-17

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

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

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

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

Arrays Classes & Methods, Inheritance

ECE 122. Engineering Problem Solving with Java

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

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

JAVA MOCK TEST JAVA MOCK TEST II

! labs last week. ! still time to work through lab 7 (midterm correction) ! can earn back up to 5 out of 70 points

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

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

Inheritance CSC 123 Fall 2018 Howard Rosenthal

CS111: PROGRAMMING LANGUAGE II

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

Transcription:

ECE 122 Engineering Problem Solving with Java Lecture 17 Inheritance

Overview Problem: Can we create bigger classes from smaller ones without having to repeat information? Subclasses: a class inherits variables and methods from its parents The leads to code reuse and general code portability Lots of bookkeeping and notation to keep in mind

Hierarchy Employee Class Super Class Faculty Class Staff Class Subclass Subclass

Subclass Class subclass additional features attributes behaviors

Extends public class subclassname extends superclassname { variables; methods; constructor; }

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 That is, the child class inherits the methods and data defined by the parent class

Inheritance A programmer can tailor a derived class as needed by adding new variables or methods, Also can be created by modifying the inherited information Software reuse is a fundamental benefit of inheritance 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

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

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 Public variables violate the principle of encapsulation There is a third visibility modifier that helps in inheritance situations: protected

The protected Modifier The protected modifier allows a child class to reference a variable or method directly in the child class Provides more encapsulation than public visibility, Not as tightly encapsulated as private visibility A protected variable is visible to any class in the same package as the parent class

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, Often is used to invoke the parent's constructor

Super Call the method in the superclass In constructor, super must be in the first line public Faculty(String n, String t, String s) { super(n, t); } secname = s; It will call the Employee s constructor: public Employee(String n, String t) { name = n; tel = t; }

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 parent s constructor The super reference is useful Can also be used to reference other variables and methods defined in the parent s class

Staff Class public class Staff extends Employee { private String office; public Staff(String n, String t, String o) { super(n, t); //have to be the first line office = o; } // you may have other methods here }

This vs. Super This: the current object private String name; public void setname( String name ) { this.name = name; } Super: the superclass public Faculty(String n, String t, String s) { super(n, t); // call superclass Employee(.) secname = s; }

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, Classes inherit the members of all parents Collisions, such as the same variable name in two parents, have to be resolved Java does not support multiple inheritance

Overriding Methods (not Overloading!) 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 The type of the object executing the method determines which version of the method is invoked

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 The type of the object executing the method determines which version of the method is invoked

Overriding A method in the parent class can be invoked explicitly using the super reference If a method is declared with the final modifier, it cannot be overridden The concept of overriding can be applied to data and is called shadowing variables Shadowing variables should be avoided because it tends to cause unnecessarily confusing code

Motivation for Overriding Faculty is too busy today to receive a call make printinfo() method in Faculty s class - print the secretary name - don t print faculty's telephone Staff helps Faculty to answer the phone make printinfo() method in Staff s class - print name, phone and office address

Overriding Methods In subclass, create a new method with the same name in superclass Write new code for this method, or extend more function for this method Example: In Faculty class, we will create a totally new printinfo() method In Staff class, we will create a new printinfo() method which calls superclass s printinfo()

Overriding In Faculty Complete Example subclass: FacultyOverriding.java superclass: EmployeeWithGetName.java In FacultyOverriding.java public void printinfo() { System.out.println( getname() + "'s secretary name is "+ secname); } In EmployeeWithGetName.java public void printinfo() { System.out.println(name+"'s number is "+tel); }

Result For Faculty Call printinfo() in FacultyOverriding.java FacultyOverriding tessier = new FacultyOverriding( Tessier", 0160", Tessier"); tessier.printinfo(); Result for printinfo(); Tessier's secretary name is Chris not Tessier's number is 0160

Overriding In Staff Complete Example subclass: StaffOverriding.java superclass: EmployeeWithGetName.java In StaffOverriding.java public void printinfo() { super.printinfo(); System.out.println("office address is " + office); } In EmployeeWithGetName.java public void printinfo() { System.out.println(name + "'s number is +tel); }

Result For Staff Call printinfo() in StaffOverriding.java StaffOverriding chris = new StaffOverriding( Chris", "4999", KEB309"); chris.printinfo(); Result for printinfo(); Chris' number is 4999 Office address is KEB309 not Chris's number is 4999

Overloading vs. Overriding Overloading deals with multiple methods with the same name in the same class Methods have different signatures Overriding deals with two methods, one in a parent class and one in a child class, They 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

Summary Inheritance is an important part of object oriented programming Overriding methods provides a powerful way to customize classes without rewriting the class Important to understand the implication of public, private, and protected variables Be sure to review the specific details Use of super, use of final, constructor creation, etc