Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Similar documents
Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Course Content. Objectives of Lecture 18 Black box testing and planned debugging. Outline of Lecture 18

Course Content. Objectives of Lecture 22 File Input/Output. Outline of Lecture 22. CMPUT 102: File Input/Output Dr. Osmar R.

Course Content. Objectives of Lecture 20 Arrays. Outline of Lecture 20. CMPUT 102: Arrays Dr. Osmar R. Zaïane. University of Alberta 4

Course Content. Objectives of Lecture 11 Tracing Programs and the Debugger. Outline of Lecture 11. CMPUT 102: Tracing Programs Dr. Osmar R.

CMSC 132: Object-Oriented Programming II. Inheritance

C++ Important Questions with Answers

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

Java Object Oriented Design. CSC207 Fall 2014

CIS 110: Introduction to computer programming

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

CS111: PROGRAMMING LANGUAGE II

What are the characteristics of Object Oriented programming language?

Week 5-1: ADT Design

What is Inheritance?

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

C08: Inheritance and Polymorphism

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Chapter 5 Object-Oriented Programming

CS111: PROGRAMMING LANGUAGE II

JAVA MOCK TEST JAVA MOCK TEST II

Course Content. Objectives of Lecture 13 Common Resources Static Variables and Methods. Outline of Lecture 13

INHERITANCE AND EXTENDING CLASSES

Course Content. Objectives of Lecture 17 Vectors and For Statements. Outline of Lecture 17. Containers. Strings and Stacks

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

Create a Java project named week10

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

Conversions and Overloading : Overloading

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming: Based on slides from Skrien Chapter 2

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Practice for Chapter 11

Arrays Classes & Methods, Inheritance

Programming overview

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

Type Hierarchy. Lecture 6: OOP, autumn 2003

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

Inheritance Motivation

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

ECE 122. Engineering Problem Solving with Java

Lecture 18 CSE11 Fall 2013 Inheritance

ITI Introduction to Computing II

Programming Exercise 14: Inheritance and Polymorphism

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

Chapter 7. Inheritance

ITI Introduction to Computing II

Distributed Systems Recitation 1. Tamim Jabban

CS 113 PRACTICE FINAL

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

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

CS-202 Introduction to Object Oriented Programming

C08: Inheritance and Polymorphism

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Java: introduction to object-oriented features

CS111: PROGRAMMING LANGUAGE II

COMP 250 Fall inheritance Nov. 17, 2017

Collections Algorithms

CS313D: ADVANCED PROGRAMMING LANGUAGE

Comp 249 Programming Methodology

CSE 113 A. Announcements - Lab

Inheritance and Polymorphism

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

Darrell Bethea June 6, 2011

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

Introduction to Inheritance

Sorting. Sorting. Selection sort

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

1.00 Lecture 13. Inheritance

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

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

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Basic Object-Oriented Concepts. 5-Oct-17

Design Pattern: Composite

Inheritance -- Introduction

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Inheritance CSC9Y4. Inheritance

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

CompuScholar, Inc. 9th - 12th grades

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

9/10/2018 Programming Data Structures Inheritance

Inheritance. COMP Week 12

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

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

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

Making New instances of Classes

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

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

Lecture 4: Extending Classes. Concept

Inheritance and Interfaces

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses

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

CS 251 Intermediate Programming Inheritance

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Chapter 5. Inheritance

Inheritance (an intuitive description)

G Programming Languages - Fall 2012

BBM 102 Introduction to Programming II Spring Inheritance

ITI Introduction to Computing II

Transcription:

Structural Programming and Data Structures Winter 2000 CMPUT 102: Dr. Osmar R. Zaïane Course Content Introduction Objects Methods Tracing Programs Object State Sharing resources Selection Repetition Vectors Testing/Debugging Arrays Searching Files I/O Sorting Recursion University of Alberta University of Alberta 1 University of Alberta 2 Objectives of Lecture 24 Introduce the notion of inheritance in objectoriented programming; Understand the concepts of superclass (base class) and subclass (derived class); Learn how to take advantage of similarities between objects from different classes to derive one class from another and inherit instance variables and methods. University of Alberta 3 University of Alberta 4 The Idea Behind Extending the capabilities (i.e. behaviour and state) of a class C1 in order to generate a new class C2 with the same capabilities as C1 in addition to new capabilities. Object of class C1 +1 Method_c soccer player professional Hierarchy Scores Team person Salary Profession Physician Name Address Birthdate Etc. student Specialty StudentID GPA Degree University of Alberta 5 University of Alberta 6 1

in the Real World How is a student like a person? Well, every student is a person! Students have all of the properties of persons, plus some others. For example, every person has a name and an age and so does every student. However, not every person is a student. Every student has a student id and a grade point average, that other persons don't have. University of Alberta 7 Two Different Approaches In Java, we model a person by a Person class. In Java, we model a student by a Student class. Introduce two independent classes, one for Student and one for Person we lost relationships between the two a Student class has to redefine all the properties of a Person class Define a Student class as a specialization of a Person class characterize special relationships software reusability University of Alberta 8 Subclasses and Superclasses Since a student is like a person with extra properties, we say the class Student is a subclass of the class Person (or derived class). We also say that Person is a superclass of Student (or base class). Superclass Subclass University of Alberta 9 The Java Tree In general, Person can have other subclasses as well, say Teacher. We put all the classes in an inheritance tree with class Object as the root. We draw the tree with the root at the top. String Student Person Teacher Object Button Component Label TextField University of Alberta 10 Type We say that a subclass inherits all of the messages from its superclass. Any message that can be sent to an instance of a class can also be sent to an instance of its subclasses. However, you can add additional instance messages and static messages to a subclass. University of Alberta 11 University of Alberta 12 2

Type (con t) If you declare the type of a variable to be some class, it can then be bound to an instance of that class or any subclass. If the type of a message parameter or the return type of a message is a class, you can use any subclass as well. The property of being able to use an instance of a subclass, wherever you can use an instance of a class is called substitutability. Type Example Assume that we are defining a class called Store. Assume that we have already defined a class called Person, with a message called name() and two subclasses: Student and Teacher. Assume that we have defined a message in this Store class called register that takes a Person as a parameter: public void register(person aperson) { // Register the given Person as a customer. University of Alberta 13 University of Alberta 14 Type Example (con t) Here is a method that creates a Person, Student or Teacher customer, depending on a char parameter. public Person createcustomer(char achar, String astring){ Person customer; if (achar == T ) else if (achar == S ) else customer = new Teacher(nameString); customer = new Student(nameString); customer = new Person(nameString); System.out.println( Welcome + customer.name()); this.register(customer); return customer; University of Alberta 15 Instance Variable and Static Variable (Representation) In Java, a subclass also inherits all of the instance variables and all of the static variables of its superclass. However, if a variable is private, it cannot be accessed directly in the subclass code. If a variable is declared as protected it can be accessed directly in the subclass code. A subclass can also add state by defining additional instance and static variables. University of Alberta 16 University of Alberta 17 Method (Implementation) In Java, a subclass also inherits the methods of its superclass, so they do not have to be reimplemented. However, you can also override any method if you want. In addition, you can add some code to an inherited method, using the super object reference. University of Alberta 18 3

Object of class C1 Class C1 Method Override Class C2 +1 Super.( ) University of Alberta 19 University of Alberta 20 Representation (or Data) Object of class C1 +1 Method_c University of Alberta 21 Representation/Implementation - Example public class Person { // Each instance represents a Person. // Public methods public void output() { // Output a representation of myself System.out.print( name: + this.name + age: ); System.out.print(this.age()); // Instance Variables protected String name; private Date birthdate; name is protected: it is accessed only by class Person and its subclasses. University of Alberta 22 Representation /Implementation - Example (con t) public class Student extends Person { // Each instance represents a Student. // Public methods public void output() { // Output a representation of myself super.output(); System.out.print( id: ); System.out.print(this.id); // Instance Variables Calls the output() method of the superclass Person. // cannot access birthdate, but can access name because it is protected private int id; University of Alberta 23 University of Alberta 24 4

Constructor Chaining Constructors are not inherited like other methods. We say constructors are chained. If you want to call another constructor in the same subclass, you just use this() with the appropriate arguments. If you want to call another constructor in the superclass, you just use super() with the appropriate arguments. University of Alberta 25 Constructor Chaining (con t) However, each constructor must ultimately call one of the constructors in its superclass. This can be done in one of three ways: An explicit call to super() with arguments. A call to another constructor in the subclass using this() with arguments. If neither of these appear as the first statement of the subclass constructor, the compiler inserts an implicit call to the zero argument super constructor super(). However, the a constructor with no arguments should exist in the superclass. University of Alberta 26 Constructors - Example public class Person { // Each instance represents a Person. // Constructors public Person() { // Set the name unknown and birtdate: today this.name = unknown ; this.birthdate = new Date(); public Person(String namestring) { // Set the given name and birthdate: today this(); // do the 0 argument constructor first this.name = namestring; University of Alberta 27 Constructors - Example (con t) public class Student extends Person { // Each instance represents a Student. public Student() { // Set the name: unknown, birtdate: today, id: 0 this.id = 0; // implicit call to super(); first public Student(String namestring) { // Set the given name, birthdate: today, id: 0 super(namestring); // explicit call this.id = 0; public Student(String namestring, int anint) { // Set the given name and id, birthdate: today this(namestring); // or super(namestring) this.id = anint; University of Alberta 28 Multiple Multiple inheritance is the inheritance of properties from more than just one base class. Java does not allow multiple inheritance. Other Object-Oriented languages such as C ++ allow multiple inheritance; parent parent1 parent2 child Simple inheritance child Multiple inheritance University of Alberta 29 5