CPSC 233 Tutorial 13 March 11/12th, 2015.

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Reusable Classes.  Motivation: Write less code!
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Intro to OOP with Java, C. Thomas Wu
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013.
Inheritance ndex.html ndex.htmland “Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism.
Overriding Method.
Inheritance and Polymorphism
Lecture 17: Polymorphism (Part II)
Week 8 Lecture -3 Inheritance and Polymorphism
Computing Adjusted Quiz Total Score
CSC 113 Tutorial QUIZ I.
Building Java Programs
Phil Tayco Slide version 1.0 Created Oct 9, 2017
Chapter 9: Polymorphism and Inheritance
Building Java Programs
Announcements Lab 8 Was Due Today Lab 7 Regrade Due Thursday
Method Overriding in Java
Inheritance Cse 3rd year.
Building Java Programs
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Object-Oriented Programming
Inheritance in Java CS 3331 Fall 2009.
Building Java Programs
C++ Programming CLASS This pointer Static Class Friend Class
Building Java Programs
Binary Search Trees reading: 17.3 – 17.4
reading: 8.6 self-check: #18, exercises: #9, 14
Methods/Functions.
CS Problem Solving and Object Oriented Programming Spring 2019
Inheritance and Polymorphism
Presentation transcript:

CPSC 233 Tutorial 13 March 11/12th, 2015

Upcasting Because the relationship between child and parent class is “is a”, an instance of child classes can be treated as an instance of the parent class. Person Note: I omit the attributes and methods of Person and Student in purpose. They are the same as those defined in Tutorial 13. Person has an attribute name while Student has an additional attribute university. Person p1 = new Person(“Foo”); Person p2 = new Student(“Foo”, “UoC”); Student 2

Dynamic Binding Given a class hierarchy and a method definition, what is the output or which class’s toString is called? Person public String toString(){ return “I am a person”; } public void print(Person p){ System.out.println(p); } Answer: we don’t know because we don’t know which class’s instance is passed as the parameter. We could know the information only in runtime. Student public String toString(){ return “I am a student”; } 3

Dynamic Binding public static void main(String[] args){ Person p1 = new Person(“John”); Person p2 = new Student(“Jack”,”UoC”); print(p1); print(p2); } public static void print(Person p){ System.out.println(p); We could ask student the outputs of the 1st and 2nd print which are “I am a person” and “I am a student”, respectively. 4

Exercise Given a base class Operator, define three subclasses called Addition, Subtraction, Multiplication. Override the public int compute(int left, int right) Override the public String getFormula(int left, int right, int result) 5

Exercise Add a fourth sub-class Division Override the public int compute(int left, int right) Override the public String getFormula(int left, int right, int result) Change the getOperator method to return an instance of Division when “/” is input.

Advantage When an operator is deleted or a new operator is added, the code of main keeps stable.