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

Similar documents
CS-202 Introduction to Object Oriented Programming

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

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

25. Generic Programming

Java Object Oriented Design. CSC207 Fall 2014

Practice for Chapter 11

Inheritance and Polymorphism

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

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

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

Inheritance and object compatibility

Inheritance and Polymorphism

Inheritance. Transitivity

Chapter 11 Inheritance and Polymorphism

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

Inheritance -- Introduction

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

What is Inheritance?

Lecture Notes Chapter #9_b Inheritance & Polymorphism

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

Inheritance, Polymorphism, and Interfaces

Chapter 5 Object-Oriented Programming

Inheritance (continued) Inheritance

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object Oriented Programming: Based on slides from Skrien Chapter 2

C++ Important Questions with Answers

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

CST242 Object-Oriented Programming Page 1

INHERITANCE. Spring 2019

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

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

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

CS 251 Intermediate Programming Inheritance

CH. 2 OBJECT-ORIENTED PROGRAMMING

Inheritance, polymorphism, interfaces

Exercise: Singleton 1

OVERRIDING. 7/11/2015 Budditha Hettige 82

Type Hierarchy. Lecture 6: OOP, autumn 2003

Polymorphism and Inheritance

CS1150 Principles of Computer Science Objects and Classes

Self-review Questions

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

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Polymorphism: Inheritance Interfaces

[ L5P1] Object-Oriented Programming: Advanced Concepts

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

Introduction to Inheritance

COP 3330 Final Exam Review

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

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

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

Example: Count of Points

CS Programming I: Inheritance

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

Chapter 14 Abstract Classes and Interfaces

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

CLASSES AND OBJECTS IN JAVA

ECE 122. Engineering Problem Solving with Java

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

JAVA MOCK TEST JAVA MOCK TEST II

Java Fundamentals (II)

Example: Count of Points

More Relationships Between Classes

8. Polymorphism and Inheritance

Inheritance, and Polymorphism.

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

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

Java: introduction to object-oriented features

Intro to Computer Science 2. Inheritance

Classes and Inheritance Extending Classes, Chapter 5.2

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

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

CO Java SE 8: Fundamentals

Programming II (CS300)

CSCE3193: Programming Paradigms

Compaq Interview Questions And Answers

Java 8 Programming for OO Experienced Developers

Lecture 18 CSE11 Fall 2013 Inheritance

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Inheritance and Polymorphism

CS313D: ADVANCED PROGRAMMING LANGUAGE

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

First IS-A Relationship: Inheritance

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

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

Everything is an object. Almost, but all objects are of type Object!

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

ITI Introduction to Computing II

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

Object Orientated Programming Details COMP360

FOR BEGINNERS 3 MONTHS

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

Chapter 10 Classes Continued. Fundamentals of Java

Inheritance and Encapsulation. Amit Gupta

Transcription:

C A N A D I A N I N T E R N A T I O N A L S C H O O L O F H O N G K O N G INHERITANCE & POLYMORPHISM P2 LESSON 12 P2 LESSON 12.1 INTRODUCTION inheritance: OOP allows a programmer to define new classes from existing classes the procedural paradigm focuses on designing methods and the object-oriented paradigm couples data and methods together into objects P2 LESSON 12.1 INTRODUCTION if a programmer needs to define classes to model circles, rectangles, and triangles these classes have many common features avoid redundancy and make the system easy to comprehend and easy to maintain 1

P2 LESSON 12.2 SUPERCLASS & SUBCLASS inheritance enables a programmer to define a general class (i.e., a superclass) extend it to more specialized classes (i.e., subclasses) a superclass is also referred to as a parent class or a base class, and a subclass as a child class, an extended class, or a derived class a subclass inherits accessible data fields and methods from its superclass and may also add new data fields and methods P2 LESSON 12.2 SUPERCLASS & SUBCLASS syntax for creating subclasses public class subclassname extends superclassname P2 LESSON 12.2 SUPERCLASS & SUBCLASS contrary to the conventional interpretation, a subclass is not a subset of its superclass a subclass usually contains more information and methods than its superclass private data fields in a superclass are not accessible outside the class not all is-a relationships should be modeled using inheritance 2

P2 LESSON 12.2 SUPERCLASS & SUBCLASS inheritance is used to model the is-a relationship some programming languages allows a subclass from several classes known as multiple inheritance Java does not allow multiple inheritance P2 LESSON 12.2 SUPERCLASS & SUBCLASS Practice refer to the Coffee Shop UML diagram create a superclass for the coffee shop decide which attributes and methods belong to the superclass assign new attributes and create new methods for the subclass P2 LESSON 12.3 THE super KEYWORD the keyword super refers to the superclass and can be used to invoke the superclass s methods and constructors the use of the keyword this to reference the calling object the keyword super refers to the superclass of the class in which super appears 3

P2 LESSON 12.3 THE super KEYWORD the syntax to call a superclass s constructor is: super() or super(parameters); must use the keyword super to call the superclass constructor, and the call must be the first statement in the constructor invoking a superclass constructor s name in a subclass causes a syntax error P2 LESSON 12.3 THE super KEYWORD the syntax to call a superclass s method is: super.method(parameters); P2 LESSON 12.4 OVERRIDING METHODS to override a method, the method must be defined in the subclass using the same signature and the same return type as in its superclass method overriding: the subclass modifies the implementation of a method defined in the superclass an instance method can be overridden only if it is accessible a static method cannot be overridden 4

P2 LESSON 12.5 OVERRIDING & OVERLOADING overloading: to define multiple methods with the same name but different signatures overriding: to provide a new implementation for a method in the subclass P2 LESSON 12.5 OVERRIDING & OVERLOADING overridden methods are in different classes related by inheritance overloaded methods can be either in the same class or different classes related by inheritance P2 LESSON 12.5 OVERRIDING & OVERLOADING overridden methods have the same signature and return type overloaded methods have the same name but a different parameter list 5

P2 LESSON 12.6 THE OBJECT CLASS every class in Java is descended from the java.lang.object class for example: the signature of the tostring() method is: public String tostring() P2 LESSON 12.7 POLYMORPHISM polymorphism means that a variable of a supertype can refer to a subtype object the three pillars of object-oriented programming are: encapsulation inheritance polymorphism P2 LESSON 12.7 POLYMORPHISM a class defines a type a type defined by a subclass is called a subtype a type defined by its superclass is called a supertype inheritance relationship enables a subclass to inherit features from its superclass with additional new features subclass is a specialization of its superclass 6

P2 LESSON 12.7 POLYMORPHISM every instance of a subclass is also an instance of its superclass, but not vice versa always pass an instance of a subclass to a parameter of its superclass type an object of a subclass can be used wherever its superclass object is used polymorphism means that a variable of a supertype can refer to a subtype objec P2 LESSON 12.8 DYNAMIC BINDING a method can be implemented in several classes along the inheritance chain the JVM decides which method is invoked at runtime beyond the SL curriculum P2 LESSON 12.9 CASTING OBJECTS casting object: one object reference can be typecast into another object reference upcasting: to cast an instance of a subclass to a variable of a superclass, because an instance of a subclass is always an instance of its superclass downcasting: casting an instance of a superclass to a variable of its subclass 7

P2 LESSON 12.10 equal METHOD like the tostring() method, the equals(object) method is another useful method defined in the Object class the equals method is overridden in many classes in the Java API, such as java.lang.string and java.util.date P2 LESSON 12.11 THE ARRAYLIST CLASS an ArrayList object can be used to store a list of objects. P2 LESSON 12.12 USEFUL METHODS FOR LISTS Java provides the methods for creating a list from an array, for sorting a list, and finding maximum and minimum element in a list, and for shuffling a list beyond the SL curriculum 8

P2 LESSON 12.13 CASE STUDY A Custom Stack Class P2 LESSON 12.14 THE protected DATA a protected member of a class can be accessed from a subclass to allow subclasses to access data fields or methods defined in the superclass, but not to allow nonsubclasses to access these data fields and methods P2 LESSON 12.14 THE protected DATA 9

P2 LESSON 12.15 PREVENTING OVERRIDING neither a final class nor a final method can be extended a final data field is a constant P2 LESSON 12 INHERITANCE & POLYMORPHISM Homework Pg. 445 #11.1, 11.3 10