COMP 110/L Lecture 24. Kyle Dewey

Similar documents
COMP 110/L Lecture 19. Kyle Dewey

COMP 110/L Lecture 13. Kyle Dewey

COMP 110/L Lecture 20. Kyle Dewey

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

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

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

COMP 110/L Lecture 7. Kyle Dewey

Chapter 2: Java OOP I

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

COMP 110/L Lecture 6. Kyle Dewey

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Software Development (cs2500)

Overriding Variables: Shadowing

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

CS Programming I: Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

Programming II (CS300)

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

AP CS Unit 6: Inheritance Notes

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Common Misunderstandings from Exam 1 Material

Objects and Iterators

Inheritance and Interfaces

Topic 10. Abstract Classes. I prefer Agassiz in the abstract, rather than in the concrete.

CS-202 Introduction to Object Oriented Programming

CMSC 132: Object-Oriented Programming II

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Example: Count of Points

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

CLASS DESIGN. Objectives MODULE 4

Inheritance and Polymorphism. CS180 Fall 2007

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

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

Week 7 Part I. Kyle Dewey. Monday, August 6, 12

Object-Oriented Design Lecture 23 CS 3500 Fall 2009 (Pucella) Tuesday, Dec 8, 2009

Exercise: Singleton 1

In Java, to create a class named "B" as a subclass of a class named "A", you would write

Inheritance & Polymorphism

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Object-Oriented Programming (Java)

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Inheritance & Interfaces and Code Layout

BM214E Object Oriented Programming Lecture 7

Program Fundamentals

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

COP 3330 Final Exam Review

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Informatik II (D-ITET) Tutorial 6

COMP 110/L Lecture 10. Kyle Dewey

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

JAVA Programming Language Homework I - Nested Class

More Relationships Between Classes

Programming II (CS300)

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

C++ Inheritance and Encapsulation

Java Object Oriented Design. CSC207 Fall 2014

Inheritance (cont.) Inheritance. Hierarchy of Classes. Inheritance (cont.)

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

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

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Abstract Classes and Interfaces

Lecture 2: Java & Javadoc

Implements vs. Extends When Defining a Class

Programming Languages and Techniques (CIS120)

CS24 Week 3 Lecture 1

Lecture 7: Process & Thread Introduction

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

COE318 Lecture Notes Week 10 (Nov 7, 2011)

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

Practice for Chapter 11

PROGRAMMING FUNDAMENTALS

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

CS 61B Discussion 4: Inheritance Fall 2018

Java Magistère BFA

Binghamton University. CS-140 Fall Dynamic Types

Chapter 9 Inheritance

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

COE318 Lecture Notes Week 8 (Oct 24, 2011)

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Equality for Abstract Data Types

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

Inheritance. CSE 142, Summer 2002 Computer Programming 1.

About This Lecture. Data Abstraction - Interfaces and Implementations. Outline. Object Concepts. Object Class, Protocol and State.

First IS-A Relationship: Inheritance

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

What will this print?

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Informatik II Tutorial 6. Subho Shankar Basu

Transcription:

COMP 110/L Lecture 24 Kyle Dewey

Outline this instanceof Casting equals() protected interface

this

this Refers to whatever instance the given instance method is called on.

this Refers to whatever instance the given instance method is called on. public class Foo { public Foo returnmyself() { return this; } }

Example: ThisExample.java

Name Clashes this can be used to refer to instance variables which have the same name as normal variables

Name Clashes this can be used to refer to instance variables which have the same name as normal variables public class NameClash { private int x; public NameClash(int x) { this.x = x; } }

Example: NameClash.java

instanceof

instanceof Returns a boolean indicating if a given instance was made from or inherited from a given class

instanceof Returns a boolean indicating if a given instance was made from or inherited from a given class public class InstanceOf { public static void main(string[] a) { InstanceOf i = new InstanceOf(); if (i instanceof InstanceOf && i instanceof Object) { // code reaches this point } } }

Example: InstanceOfExample.java

Casting

Casting Converts a value of one type into another. Not always possible to perform.

Casting Converts a value of one type into another. Not always possible to perform. int myint0 = 16.0; int myint1 = (int)16.0; // myint2 gets set to 16 int myint2 = (int)16.5;

Casting Converts a value of one type into another. Not always possible to perform. int myint0 = 16.0; Does not compile int myint1 = (int)16.0; // myint2 gets set to 16 int myint2 = (int)16.5;

Casting Converts a value of one type into another. Not always possible to perform. int myint0 = 16.0; int myint1 = (int)16.0; // myint2 gets set to 16 int myint2 = (int)16.5;

Casting Converts a value of one type into another. Not always possible to perform. int myint0 = 16.0; int myint1 = (int)16.0; myint1 holds 16 // myint2 gets set to 16 int myint2 = (int)16.5;

Casting Converts a value of one type into another. Not always possible to perform. int myint0 = 16.0; int myint1 = (int)16.0; int myint2 = (int)16.5;

Casting Converts a value of one type into another. Not always possible to perform. int myint0 = 16.0; int myint1 = (int)16.0; int myint2 = (int)16.5; myint2 holds 16

Casting Converts a value of one type into another. Not always possible to perform.

Casting Converts a value of one type into another. Not always possible to perform. public class Foo {... }... Foo f = new Foo(); I define Foo and later on I make an instance of it

Casting Converts a value of one type into another. Not always possible to perform. public class Foo {... }... Foo f = new Foo(); Object o = f; I can assign f to an Object, since Foo is an instance of Object

Casting Converts a value of one type into another. Not always possible to perform. public class Foo {... }... Foo f = new Foo(); Object o = f; Foo g = o; But if I try to assign an object to a Foo...

Casting Converts a value of one type into another. Not always possible to perform. public class Foo {... }... Foo f = new Foo(); Object o = f; Foo g = o; Does not compile...this fails to compile, because any arbitrary Object is not necessarily a Foo

Casting Converts a value of one type into another. Not always possible to perform. public class Foo {... }... Foo f = new Foo(); Object o = f; Foo g = (Foo)o; -If, however, we cast the object as a Foo...

Casting Converts a value of one type into another. Not always possible to perform. public class Foo {... }... Foo f = new Foo(); Object o = f; Foo g = (Foo)o; Compiles and runs ok -...this will work, because we have performed the cast -The cast effectively tells Java I know what I m doing, and this Object is a Foo

Casting Converts a value of one type into another. Not always possible to perform.

Casting Converts a value of one type into another. Not always possible to perform. public class Foo {... } public class Bar {... }... -Let s define these two classes

Casting Converts a value of one type into another. Not always possible to perform. public class Foo {... } public class Bar {... }... Object f = new Foo(); Object b = new Bar();...along with these two instances

Casting Converts a value of one type into another. Not always possible to perform. public class Foo {... } public class Bar {... }... Object f = new Foo(); Object b = new Bar(); f = b;

Casting Converts a value of one type into another. Not always possible to perform. public class Foo {... } public class Bar {... }... Object f = new Foo(); Object b = new Bar(); f = b; Does not compile -Doesn t compile, because a Bar is not a Foo

Casting Converts a value of one type into another. Not always possible to perform. public class Foo {... } public class Bar {... }... Object f = new Foo(); Object b = new Bar(); f = (Foo)b; -If we instead try to cast it...

Casting Converts a value of one type into another. Not always possible to perform. public class Foo {... } public class Bar {... }... Object f = new Foo(); Object b = new Bar(); f = (Foo)b; Compiles, but doesn t run correctly (gives a ClassCastException) -If we instead try to cast it...

equals()

equals() Used to determine if two arbitrary objects are equal. Defined in Object.

equals() Used to determine if two arbitrary objects are equal. Defined in Object. foo.equals( foo ) Returns true

equals() Used to determine if two arbitrary objects are equal. Defined in Object. foo.equals( foo ) Returns true foo.equals( bar )

equals() Used to determine if two arbitrary objects are equal. Defined in Object. foo.equals( foo ) Returns true foo.equals( bar ) Returns false

equals() vs. == With equals(), we test object equality, AKA deep equality Look at the inside of the object With ==, we test reference equality, AKA shallow equality Return true if two references refer to the exact same object

Example: StringEquals.java -This example shows off the difference between reference and object equality

Defining Your Own equals() Usual pattern: see if the given thing is an instance of my class If true, cast it to the class, and do some deep comparisons If false, return false Anything is possible

Example: CustomEquals.java

protected

protected Somewhere between private and public. Like private, but subclasses can access it.

protected Somewhere between private and public. Like private, but subclasses can access it. public class HasPrivate { } private int x;

protected Somewhere between private and public. Like private, but subclasses can access it. public class HasPrivate { private int x; } public class Sub extends HasPrivate {...x... }

protected Somewhere between private and public. Like private, but subclasses can access it. public class HasPrivate { private int x; } public class Sub extends HasPrivate {...x... } Not permitted - x is private in HasPrivate

protected Somewhere between private and public. Like private, but subclasses can access it. public class HasPrivate { private int x; } public class Sub extends HasPrivate {...x... } public class HasProt { protected int x; } public class Sub extends HasProt {...x... }

protected Somewhere between private and public. Like private, but subclasses can access it. public class HasPrivate { private int x; } public class Sub extends HasPrivate {...x... } public class HasProt { protected int x; } public class Sub extends HasProt {...x... OK: Sub is a subclass of HasProt }

interface

Mammal breathe -We had this setup before

Animal breathe Mammal -Restructure: add Animal, birds, and spiders

Animal breathe Mammal -Restructure: add Animal, birds, and spiders

-Restructure: add Animal, birds, and spiders -Now we have a problem: birds breathe too, but they aren t mammals -The property of breathing is not unique to mammals -Potential solution: restructure things to add BreathingAnimals in between Animal and Mammal; have Birds inherit from BreathingAnimals but not Mammal. -That solution is specific to this case; doesn t work in general Animal breathe Mammal

interface Like an abstract class with the following restrictions: Cannot have constructors Cannot have instance variables However, we can inherit from them anywhere, and we can inherit from multiple interfaces

Using interfaces public interface CanBreathe { } public void breathe(); -We can define an interface like so...

Using interfaces public interface CanBreathe { } public void breathe(); public class Foo extends Bar implements CanBreathe { public void breathe() {... } } -And implement it like this -We can extend a class along with implementing an interface

Using interfaces public interface CanBreathe { } public void breathe(); public class Foo extends Bar implements CanBreathe { public void breathe() {... } } public class Multi extends Alpha implements Beta, Gamma, Delta {... } -Can inherit from multiple interfaces, separated with commas

Example Animal.java CanBreathe.java Mammal.java Dog.java Cat.java CanFly.java Parrot.java Bat.java Spider.java AnimalMain.java