Comp 249 Programming Methodology

Similar documents
9/10/2018 Programming Data Structures Inheritance

Chapter 7. Inheritance

Comp 249 Programming Methodology Chapter 8 - Polymorphism

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

Comp 249 Programming Methodology

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Copyright 2007 Pearson Addison-Wesley Copyright 2018 Aiman Hanna All rights reserved

Comp 249 Programming Methodology Chapter 9 Exception Handling

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Chapter 14. Inheritance. Slide 1

5/24/12. Introduction to Polymorphism. Chapter 8. Late Binding. Introduction to Polymorphism. Late Binding. Polymorphism and Abstract Classes

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 14 Inheritance. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy

Inheritance -- Introduction

OVERRIDING. 7/11/2015 Budditha Hettige 82

CS313D: ADVANCED PROGRAMMING LANGUAGE

Java Object Oriented Design. CSC207 Fall 2014

Copyright 2007 Pearson Addison-Wesley Copyright 2019 Aiman Hanna All rights reserved

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

CS111: PROGRAMMING LANGUAGE II

Comp 249 Programming Methodology Chapter 11 Recursion

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

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

Inheritance Encapsulation and

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

Programming in C# Inheritance and Polymorphism

Inheritance and Polymorphism

ECE 122. Engineering Problem Solving with Java

Inheritance and Encapsulation. Amit Gupta

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

INHERITANCE. Spring 2019

CS111: PROGRAMMING LANGUAGE II

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

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

What is Inheritance?

Programming II (CS300)

Inheritance (with C++)

Inheritance Basics. Inheritance (with C++) Base class example: Employee. Inheritance begets hierarchies. Writing derived classes

PROGRAMMING LANGUAGE 2

Inheritance, Polymorphism, and Interfaces

What are the characteristics of Object Oriented programming language?

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

Inheritance Motivation

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

Polymorphism and Inheritance

BBM 102 Introduction to Programming II Spring Inheritance

Chapter 10 Classes Continued. Fundamentals of Java

C++ Important Questions with Answers

Chapter 5 Object-Oriented Programming

8. Polymorphism and Inheritance

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

COMP 110/L Lecture 19. Kyle Dewey

CS111: PROGRAMMING LANGUAGE II

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

Lecture 18 CSE11 Fall 2013 Inheritance

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

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

INHERITANCE AND EXTENDING CLASSES

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Object Orientated Programming Details COMP360

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

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

Inheritance and Polymorphism

Java Fundamentals (II)

Chapter 5. Inheritance

CS-202 Introduction to Object Oriented Programming

Module Contact: Dr Taoyang Wu, CMP Copyright of the University of East Anglia Version 1

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

Lecture 5: Inheritance

Arrays Classes & Methods, Inheritance

Ch 14. Inheritance. May 14, Prof. Young-Tak Kim

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

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

ENCAPSULATION AND POLYMORPHISM

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

CS Programming I: Inheritance

Overriding Variables: Shadowing

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

Programming overview

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

C++ Basic Syntax. Constructors and destructors. Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

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

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

CS260 Intro to Java & Android 03.Java Language Basics

CMSC 132: Object-Oriented Programming II

Inheritance. Transitivity

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems

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

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

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

Chapter 14 Abstract Classes and Interfaces

Chapter 11: Inheritance

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

Generalized Code. Fall 2011 (Honors) 2

Transcription:

Comp 249 Programming Methodology Chapter 7 - Inheritance Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of Absolute Java by Savitch; which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is published by Pearson Education / Addison-Wesley. Copyright 2007-2017 Pearson Addison-Wesley Copyright 2018 Aiman Hanna All rights reserved

Introduction to Inheritance Inheritance is one of the main techniques of objectoriented programming (OOP) Using this technique, further classes can be created from existing ones; those classes are said to inherit the methods and instance variables of the class they inherited The new class is called a derived class The original class is called the base class Advantage: Reusing existing code 7-2

Derived Classes When designing certain classes, there is often a natural hierarchy for grouping them For instance, for the employees of a company, there are hourly employees and salaried employees Hourly employees can be divided into full time and part time workers Salaried employees can be divided into those on technical staff, and those on the executive staff 7-3

A Class Hierarchy 7-4

Derived Classes Since an hourly employee is an employee, it is defined as a derived class of the class Employee A derived class is defined by adding instance variables and methods to an existing class The existing class that the derived class is built upon is called the base class The phrase extends BaseClass must be added to the derived class definition: public class HourlyEmployee extends Employee See Inherit1.java 7-5

Derived Classes Derived classes (also referred to as subclasses) inherit all instance variables and methods of the base class (also referred to as superclass). Any object of a derived class can invoke one of these parent methods, just like any of its own methods The derived class can add more instance variables, static variables, and/or methods See Inherit2.java 7-6

Parent and Child Classes A base class is often called the parent class A derived class is then called a child class These relationships are often extended such that a class that is a parent of a parent... of another class is called an ancestor class If class A is an ancestor of class B, then class B can be called a descendent of class A 7-7

Overriding a Method Definition Although a derived class inherits methods from the base class, it can change or override an inherited method if necessary In order to override a method definition, a new definition of the method is simply placed in the class definition, just like any other method that is added to the derived class See Inherit3.java 7-8

Changing the Return Type of an Overridden Method Ordinarily, the type returned may not be changed when overriding a method However, if it is a class type, then the returned type may be changed to that of any descendent class of the returned type This is known as a covariant return type Covariant return types are new in Java 5.0; they are not allowed in earlier versions of Java 7-9

Covariant Return Type Given the following base class: public class BaseClass {... public Employee getsomeone(int somekey)... The following is allowed in Java 5.0: public class DerivedClass extends BaseClass {... public HourlyEmployee getsomeone(int somekey)... 7-10

Changing the Access Permission of an Overridden Method The access permission of an overridden method can be changed from private in the base class to public (or some other more permissive access) in the derived class However, the access permission of an overridden method can not be changed from public in the base class to a more restricted access permission in the derived class 7-11

Changing the Access Permission of an Overridden Method Given the following method header in a base case: private void dosomething() The following method header is valid in a derived class: public void dosomething() However, the opposite is not valid Given the following method header in a base case: public void dosomething() The following method header is not valid in a derived class: private void dosomething() 7-12

Pitfall: Overriding Versus Overloading Do not confuse overriding with overloading When a method is overridden, the new method definition given in the derived class has the exact same number and types of parameters as in the base class When a method in a derived class has a different signature from the method in the base class, that is overloading Note that when the derived class overloads the original method, it still inherits the original method from the base class as well 7-13

The final Modifier If the modifier final is placed before the definition of a method, then that method may not be overridden in a derived class It the modifier final is placed before the definition of a class, then that class may not be used as a base class to derive other classes See Inherit4.java 7-14

The super Constructor A derived class uses a constructor from the base class to initialize all the data inherited from the base class In order to invoke a constructor from the base class, it uses a special syntax: public derivedclass(int p1, int p2, double p3) { super(p1, p2); instancevariable = p3; } In the above example, super(p1, p2); is a call to the base class constructor See Inherit5.java 7-15

The super Constructor A call to the base class constructor can never use the name of the base class, but uses the keyword super instead A call to super must always be the first action taken in a constructor definition Notice that if super is not used, then a call to the default constructor of the base class is automatically issued Consequently, a compilation error would occur if the base class has no default constructor See Inherit6.java See Inherit7.java 7-16

The this Constructor Within the definition of a constructor for a class, this can be used as a name for invoking another constructor of the same class The same restrictions on how to use a call to super apply to the this constructor If it is necessary to include a call to both super and this, the call using this must be made first, and then the constructor that is called must call super as its first action See Inherit8.java 7-17

The this Constructor Often, a no-argument constructor uses this to invoke an explicit-value constructor No-argument constructor (invokes explicit-value constructor using this and default arguments): public ClassName() { this(argument1, argument2); } Explicit-value constructor (receives default values): public ClassName(type1 param1, type2 param2) {... } 7-18

The this Constructor Example: public HourlyEmployee() { this("no name", new Date(), 0, 0); } The above constructor will cause the constructor with the following heading to be invoked: public HourlyEmployee(String thename, Date thedate, double thewagerate, double thehours) 7-19

Tip: An Object of a Derived Class Has More than One Type An object of a derived class has the type of the derived class, and it also has the type of the base class More generally, an object of a derived class has the type of every one of its ancestor classes Therefore, an object of a derived class can be assigned to a variable of any ancestor type An object of a derived class can be plugged in as a parameter in place of any of its ancestor classes In fact, a derived class object can be used anyplace that an object of any of its ancestor types can be used Note, however, that this relationship does not go the other way An ancestor type can never be used in place of one of its derived types See Inherit9.java 7-20

Pitfall: The Terms "Subclass" and "Superclass" The terms subclass and superclass are sometimes mistakenly reversed A superclass or base class is more general and inclusive, but less complex A subclass or derived class is more specialized, less inclusive, and more complex As more instance variables and methods are added, the number of objects that can satisfy the class definition becomes more restricted 7-21