[TAP:AXETF] Inheritance

Similar documents
Introduction to Programming Using Java (98-388)

CSC Java Programming, Fall Java Data Types and Control Constructs

Java Primer 1: Types, Classes and Operators

Basic Object-Oriented Concepts. 5-Oct-17

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

Objects and Iterators

CISC370: Inheritance

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

Programming overview

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

JAVA MOCK TEST JAVA MOCK TEST II

Conversions and Overloading : Overloading

Introduction to Inheritance

Language Features. 1. The primitive types int, double, and boolean are part of the AP

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

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

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

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Java: introduction to object-oriented features

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

type conversion polymorphism (intro only) Class class

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

Inheritance & Polymorphism

2. The object-oriented paradigm

More on Objects in JAVA TM

Chapter 2: Using Data

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

Programming Exercise 14: Inheritance and Polymorphism

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Abstract class & Interface

Computational Expression

Chapter 6 Reflection

ECE 122. Engineering Problem Solving with Java

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

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

Java Notes. J Pickering Department of Computer Science The University of York Heslington York YO10 5DD UK

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Inheritance (IS A Relationship)

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

CMSC 341. Nilanjan Banerjee

Polymorphism. return a.doublevalue() + b.doublevalue();

Java Fundamentals (II)

2. The object-oriented paradigm!

ECE 122. Engineering Problem Solving with Java

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

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

C a; C b; C e; int c;

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

Operators and Expressions

AP CS Unit 6: Inheritance Notes

Week 5-1: ADT Design

Informatik II (D-ITET) Tutorial 6

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

BM214E Object Oriented Programming Lecture 6. Classes and Objects

Introductory Programming Inheritance, sections

Distributed Systems Recitation 1. Tamim Jabban

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

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

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

Lab5. Wooseok Kim

CS1150 Principles of Computer Science Objects and Classes

Inheritance and Polymorphism

1 Shyam sir JAVA Notes

Inheritance. Transitivity

CS260 Intro to Java & Android 03.Java Language Basics

Distributed Systems Recitation 1. Tamim Jabban

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

Lecture Set 4: More About Methods and More About Operators

15CS45 : OBJECT ORIENTED CONCEPTS

Java TM Introduction. Renaud Florquin Isabelle Leclercq. FloConsult SPRL.

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

(2½ hours) Total Marks: 75

C++ Important Questions with Answers

CompuScholar, Inc. 9th - 12th grades

Lecture 10. Overriding & Casting About

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

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

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Java Classes: Math, Integer A C S L E C T U R E 8

OBJECT ORİENTATİON ENCAPSULATİON

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Lecture Set 4: More About Methods and More About Operators

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

Java. Representing Data. Representing data. Primitive data types

CSE 113 A. Announcements - Lab

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

Declarations and Access Control SCJP tips

Practice for Chapter 11

Points To Remember for SCJP

QUESTIONS FOR AVERAGE BLOOMERS

Csci 102: Sample Exam

Number Representation & Conversion

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Chapter 13 Abstract Classes and Interfaces

Transcription:

[TAP:AXETF] Inheritance What makes the latter better? A. Less room for error B. Easier to understand the global structure C. All of the above D. None of the above E. Whatever 1

Administrative Details Lab 1 is done! (You only have 9 more to go.) Lab 2 You have PRE-LAB to complete before lab 2

Agenda (More) Inheritance Overloading & this Overwriting & super Casting Association Generics Wrapper Class 3

chop()/peel() inside eat()? Currently, Cookie Monster cannot eat Apple and Orange if they are not chopped and peeled, respectively. What if you wanted to call chop() and peel() inside eat()? 4

Overloading 5

CookieMonster.java public void eat(edible something){ if(something.isedible()){ int tempcalories = something.getcalories(); calories += tempcalories; System.out.println("Me eat + tempcalories +" calories! Om nom nom nom"); 6

Overloading Overloading is useful when the implementation depends on the parameter types, e.g., String.valueOf(): 7

this() in overloaded constructors public class Baby { private String name; private int age; public Baby(String name){ this.name = name; this.age = 0; public Baby(String name, int age){ this.name = name; this.age = age; public String tostring(){ return name+ "(age "+age+")"; 8

Agenda (More) Inheritance Overloading & this Overwriting & super Casting Association Generics Wrapper Class 9

super() 10

Overwriting public class BossBaby extends Baby{ private String position; public BossBaby(String name, int age){ this(name, age, "unemployed"); public BossBaby(String name, int age, String position){ super(name,age); this.position = position; public String tostring(){ return super.tostring() + ": " + position; 11

Overwriting public class BossBaby extends Baby{ private String position; public BossBaby(String name, int age){ this(name, age, "unemployed"); public BossBaby(String name, int age, String position){ super(name,age); this.position = position; public String tostring(){ return super.tostring() + ": " + position; 12

13

Agenda (More) Inheritance Overloading & this Overwriting & super Casting Association Generics Wrapper Class 14

Casting Implicit Casting (widening conversion) byte short, int, long, float, or double short/char int, long, float, or double int long, float, or double long float, or double float double Subclass to Superclass Explicit Casting (narrow conversion) The opposite direction 15

Agenda (More) Inheritance Overloading & this Overwriting & super Casting Association Generics Wrapper Class 16

Association In real life, information is often stored in key-value pairs:

Association Class We want a general class that captures the key value relationship. public class Association {

Association Class // Association is part of the structure package public class Association { protected Object key; protected Object value; public Association (Object key, Object value) { this.key = key; this.value = value; public Object getkey() { return key; public Object getvalue() { return value; public Object setvalue(object value) { Object old = this.value; this.value = value; return old;

Using Association Class We can use type casting: Association a = new Association( cookie, A cookie is ); String definition = (String) a.getvalue(); Association a = new Association( Bill,new Integer(97)); Integer grade = (Integer) a.getvalue(); 20

Agenda (More) Inheritance Overloading & this Overwriting & super Casting Association Generics Wrapper Class 21

Using Generic Data Types Instead of casting Objects, Java supports generic (or parameterized) data types (Read Ch 4) Instead of: Association a = new Association( Bill,new Integer(97)); Integer grade = (Integer) a.getvalue(); Use: Association<String,Integer> a = new Association<String,Integer>( Bill,new Integer(97)); Integer grade = a.getvalue(); Note, types can be nested: Association<String, Association<String, Integer>> a = new Association<String, Association<String, Integer>>(); Association<String,Integer> a2 = a.getvalue(); Integer grade = a2.getvalue();

Association Class (with generics) // Association is part of the structure package public class Association { protected Object key; protected Object value; public Association (Object key, Object value) { this.key = key; this.value = value; public Object getkey() { return key; public Object getvalue() { return value; public Object setvalue(object value) { Object old = this.value; this.value = value; return old;

Agenda (More) Inheritance Overloading & this Overwriting & super Casting Association Generics Wrapper Class 25

Wrapper Class In Association<K,V>, K and V cannot be a primitive type Luckily, Java provides a wrapper class for each primitive type (java.lang package): Boolean, Character, Byte, Short, Integer, Long, Float, Double. Useful when primitive types can t be used Association<String,Integer> a = new Association<String,Integer>( Bill,new Integer(97)); or for type conversion functionality int num = Integer.parseInt( 2 );