COE318 Lecture Notes Week 8 (Oct 24, 2011)

Similar documents
COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

COE318 Lecture Notes Week 9 (Oct 31, 2011)

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

COE318 Lecture Notes Week 6 (Oct 10, 2011)

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

Java Magistère BFA

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

Abstract Classes and Interfaces

Inheritance (Outsource: )

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

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

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

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

Object-Oriented Programming More Inheritance

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Java Fundamentals (II)

What is Inheritance?

Distributed Systems Recitation 1. Tamim Jabban

Introduction to Programming Using Java (98-388)

Inheritance (Part 5) Odds and ends

Super-Classes and sub-classes

Object-Oriented Concepts

Rules and syntax for inheritance. The boring stuff

COE318 Lecture Notes Week 10 (Nov 7, 2011)

INHERITANCE. Spring 2019

CLASS DESIGN. Objectives MODULE 4

Introduction to Inheritance

1 Shyam sir JAVA Notes

Practice for Chapter 11

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

Object-Oriented ProgrammingInheritance & Polymorphism

Building Java Programs. Inheritance and Polymorphism

CS-202 Introduction to Object Oriented Programming

Basic Object-Oriented Concepts. 5-Oct-17

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

CS260 Intro to Java & Android 03.Java Language Basics

JAVA MOCK TEST JAVA MOCK TEST II

AP CS Unit 6: Inheritance Notes

Java Classes, Inheritance, and Interfaces

Informatik II (D-ITET) Tutorial 6

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

Logistics. Final Exam on Friday at 3pm in CHEM 102

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

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

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

type conversion polymorphism (intro only) Class class

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

COP 3330 Final Exam Review

First IS-A Relationship: Inheritance

Inheritance and Polymorphism

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Generics method and class definitions which involve type parameters.

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

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

The class Object. Lecture CS1122 Summer 2008

Lecture 4: Extending Classes. Concept

Class, Variable, Constructor, Object, Method Questions

Object Oriented Programming. Java-Lecture 11 Polymorphism

interface MyAnno interface str( ) val( )

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

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

Declarations and Access Control SCJP tips

Classes and Inheritance Extending Classes, Chapter 5.2

Informatik II Tutorial 6. Subho Shankar Basu

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

Distributed Systems Recitation 1. Tamim Jabban

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Chapter 11: Collections and Maps

COMP 250 Fall inheritance Nov. 17, 2017

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

Inheritance (continued) Inheritance

COE318 Lecture Notes Week 5 (Oct 3, 2011)

More Relationships Between Classes

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

Programming Languages and Techniques (CIS120)

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Chapter 14 Abstract Classes and Interfaces

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

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

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

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

CS/ENGRD 2110 SPRING 2018

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

OVERRIDING. 7/11/2015 Budditha Hettige 82

Fundamental Java Methods

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

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

Java Object Oriented Design. CSC207 Fall 2014

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Software Development (cs2500)

INSTRUCTIONS TO CANDIDATES

Chapter 9 Inheritance

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

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

Brief Summary of Java

Java: introduction to object-oriented features

Transcription:

COE318 Software Systems Lecture Notes: Week 8 1 of 17 COE318 Lecture Notes Week 8 (Oct 24, 2011) Topics == vs..equals(...): A first look Casting Inheritance, interfaces, etc Introduction to Juni (unit testing) Vocabulary There are new words and concepts to learn this week including: inheritance (subclassing) subclass superclass extends implements Exception try catch casting abstract interface overrides overloads super(...) this(...) super.foo(...) instanceof marker interface

COE318 Software Systems Lecture Notes: Week 8 2 of 17 polymorphism == vs. equals(...) For primitive data types (int, double, boolean, char, etc.) the only way to test if two values are equal is with ==; thus if i and j are ints then (i == j) is true if and only if they both have the same value. For reference data types, however, you can test for different types of equality : It is legal (but usually wrong) to use == just as with primitive types. Two reference objects are equal in this sense only if they refer to the identical object. The equals(...) method is almost always the correct way to check for equality of two reference variables. Consider the example below (where we assume a ComplexNukber class similar to what you did in the lab except that it has an equals() method that returns true if both complex numbers have exactly the same real and imaginary parts.) ComplexNumber w, y, z; w = new ComplexNumber(1, 2); z = new ComplexNumber(1, 2); y = z; //Now w == z is false; they are different objects! //But w.equals(z) is true since they represent the same //complex number. //Both y == z and y.equals(z) are true since they are //the same object. Casts primitive data types come in different sizses; an int is a 32-bit signed integer; a short is 16-bits; a byte is 8 bits and a long is 64 bits; When a smaller type is assigned to a larger type, a cast is required. This kind of primitive data casting is the same as in C. In the following example, all the casts are necessary (or there will be compilation errors.) We will consider casting of reference type data later on. public class Casts { public static void main(string[] args) { byte b = 1; short s = 1;

COE318 Software Systems Lecture Notes: Week 8 3 of 17 int i = 0x7fff0005; s = (short) i; System.out.println("s: " + s); System.out.println("i: " + i); double d = 2.99; i = (int) d; System.out.println("i: " + i); s = -1; char ch = (char) s; i = s; System.out.println("i: " + i); i = ch; System.out.println("i: " + i); By the way, the output from this program is: s: 5 i: 2147418117 i: 2 i: -1 i: 65535 Can you see why this is the output? (Hint: when something is too big for a smaller number of bytes, it is truncated. Also, all numbers in Java are signed except for char which is 16-bit unsigned (short is 16-bit signed.)) Will be explained in next lecure. Zoo: Version 1 This simple version models a Zoo that contains an arbitrary number of Lions. There is nothing new here. It's just a starting point for exploring, using and understanding some important features of object-oriented programming. But, some notes... The instance variables of Lion are private and final. Consequently, Lion objects are (for the moment) immutable. public class Lion {

COE318 Software Systems Lecture Notes: Week 8 4 of 17 private final String name; private final int birthyear; public Lion(String name, int year) { this.name = name; birthyear = year; public int age() { return 2011 - birthyear; public String getname() { return name; public String noise() { return "GGGrowl"; public boolean eatspeople() { return true; import java.util.arraylist; public class Zoo { private final ArrayList<Lion> lions = new ArrayList<Lion>(); public void add(lion lion) { lions.add(lion); public String getnames() { String s = ""; for(lion leo : lions) { s += leo.getname() + "\n";

COE318 Software Systems Lecture Notes: Week 8 5 of 17 return s; public static void main(string[] args) { Zoo zoo = new Zoo(); Lion a = new Lion("Alex", 2010); zoo.add(a); zoo.add(new Lion("Betty", 2009)); System.out.println(zoo.getNames()); Zoo Version 2: A First Look at Inheritance The zoo wants to have more animals than just lions. But proceeding as before would be very tedious if, say, there were 100 different kinds of animals: We'd need to create Pig, Cat, Crocodile, Elephant, Hummingbird, etc. classes and they would all have just about the same code; We'd need separate ArrayLists for each type of animal in the Zoo class. It would be difficult to treat all the animals as a group: to sort them alphabetically by name or by age, for example. Inheritance (subclassing) to the rescue! We can put practically all the code for Lion into another class we call AbstractAnimal. (Why not call it Animal instead of AbstractAnimal? Good question! To be explained in the next version.) We can then rewrite the Lion class to extend the AbstractAnimal class; all of its methods are inherited by Lion and do not have to be copied and pasted. The Lion class now looks like: public class Lion extends AbstractAnimal { public Lion(String name, int year) { super(name, year);

COE318 Software Systems Lecture Notes: Week 8 6 of 17 But what's this super(name, year); stuff? It invokes the constructor (with the same signature) of its superclass. It is not always necessary to invoke the superclass constructor as is done here; however, If your do, it must be the first statement in the subclass's constructor. The superclass is AbstractAnimal and here's what it looks like: public class AbstractAnimal { private final String name; private final int birthyear; public AbstractAnimal(String name, int year) { this.name = name; birthyear = year; public int age() { return 2011 - birthyear; public String getname() { return name; public String noise() { return "GGGrowl"; public boolean eatspeople() { return true; We can now write the Hedgehog class. However, the inherited methods eatspeople() and noise() give the wrong answer for a hedgehog. An inherited method can be overridden by simply re-writing it. (A subclass almost always overrides at least one method from its superclass.)

COE318 Software Systems Lecture Notes: Week 8 7 of 17 Note: The line is not required. It can be useful for the compiler and some tools. Netbeans recommends you put it in. It also reminds programmers that they are modifying an inherited method.) In general, lines beginning with @ are called annotations. Programmers can define their own annotations but that is beyond the scope of this course.) public class Hedgehog extends AbstractAnimal { public Hedgehog(String name, int year) { super(name, year); public boolean eatspeople() { return false; How do we declare the data type of a Lion or Hedgehog? In the past (before inheritance), this question would appear foolish. For example, we would write things like: Resistor r; r = new Resistor(10); r.setvoltage(10); There would be no other choice. But with inheritance we can declare a Lion as either of type Lion or type AbstractAnimal: Lion p = new Lion( Leo ); AbstractAnimal q = new Lion( Simba ); There is absolutely no difference between how p and q behave. They are both Lions and behave as such no matter how their data types were declared. And there is no difference in runtime efficiency. But declaring Lions or Hedgehogs or Pigs to be AbstractAnimals has big advantages. For example, the Zoo class can keep all of the animals in a single ArrayList<AbstractAnimal>. Below is the new, improved Zoo class: import java.util.arraylist;

COE318 Software Systems Lecture Notes: Week 8 8 of 17 public class Zoo { private final ArrayList<AbstractAnimal> animals = new ArrayList<AbstractAnimal>(); public void add(abstractanimal animal) { animals.add(animal); public String getnames() { String s = ""; for (AbstractAnimal a : animals) { s += a.getname() + "\n"; return s; public AbstractAnimal[] getanimals() { AbstractAnimal[] ts = new AbstractAnimal[0]; return animals.toarray(ts); public static void main(string[] args) { Zoo zoo = new Zoo(); Lion a = new Lion("Leo", 2010); zoo.add(a); zoo.add(new Lion("Simba", 2009)); zoo.add(new Hedgehog("Sonic", 2010)); System.out.println(zoo.getNames()); AbstractAnimal[] animals = zoo.getanimals(); for (AbstractAnimal an : animals) { System.out.println(an.getClass().getName() + " " + an.getname() + ": " + (an.eatspeople()? "eats" : "does not eat") + " people");

COE318 Software Systems Lecture Notes: Week 8 9 of 17 Zoo Version 3: Abstract classes and methods AbstractAnimal is just a normal class and it is legal to write: AbstractAnimal a = new AbstractAnimal( Sue ); This should not be allowed! It makes no sense to create an animal when we don't know what kind of animal it is. AbstractAnimal exists only for the purpose of subclassing. We can make this clear by declaring it to be abstract. The compiler will now disallow: AbstractAnimal a = new AbstractAnimal( Sue ); We now refer to AbstractAnimal as an abstract class that cannot be instantiated. But Lion and Hedgehog are concrete classes that can be instantiated. We can also declare methods to be abstract. If we do so, we must declare the class to be abstract. Furthermore, when we extend an abstract class as a concrete class, the compiler will insist that we write implementation code for any abstract methods. The code for our classes is given below. Note: the Zoo class needs no changes. The AbstractAnimal class is now declared abstract as is its noise() method. Both Lion and Hedgehog now have to implement (and not just inherit) the noise() method. public abstract class AbstractAnimal { private final String name; private final int birthyear; public AbstractAnimal(String name, int year) { this.name = name; birthyear = year; public int age() { return 2011 - birthyear; public String getname() { return name;

COE318 Software Systems Lecture Notes: Week 8 10 of 17 public abstract String noise(); public boolean eatspeople() { return true; public class Hedgehog extends AbstractAnimal { public Hedgehog(String name, int year) { super(name, year); public boolean eatspeople() { return false; public String noise() { return "Chirp...chirp"; public class Lion extends AbstractAnimal { public Lion(String name, int year) { super(name, year); public String noise() { return "Rrroar (yum yum)";

COE318 Software Systems Lecture Notes: Week 8 11 of 17 Zoo Version 4: Interfaces The zoo owner is happy with version 3; lots and lots of concrete animal classes can be easily added; no changes needed to the Zoo or AbstractAnimal classes. But then one day he says, I love my fridge. I want to have it as one of the amimals in my zoo. The guy may be a lunatic, but we have to respect his wishes. Tell him just to subclass Refrigerator from AbstractAnimal. But he can't do that; Refrigerator is already subclassed from ElectricalAppliance. OK, we'll make an interface called Animal. He just has to implement the interface. What is an interface? An interface is simply a collection of zero or more public abstract methods. A class can implement as many interfaces as it wants. It just has to ensure that all the abstract methods are actually implemented. (And the compiler will ensure that this is done.) We also take advantage of the Animal interface and have AbstractAnimal implement it. Objects can be declared as their own type or as any of their superclass's type. In addition, an object can be declared as any interface type implemented by the actual class. Consequently, we can now declare all the animals (and the fridge) as type Animal. We also have AbstractAnimal implement a standard imterface, Comparable<Animal>. Note (lab 5): You are provided with an interface in lab 5: UserInterface which describes what the methods should do. The reason for this so that any user interface has the same mthods and can be easily plugged into the existing code. (A graphical user interface would have buttons and show the cards in a graphical way. A networked interface coudl also be written.) interface Animal { String noise(); boolean eatspeople(); int age(); String getname(); public abstract class AbstractAnimal implements Animal, Comparable<Animal> { private final String name; private final int birthyear;

COE318 Software Systems Lecture Notes: Week 8 12 of 17 public AbstractAnimal(String name, int year) { this.name = name; birthyear = year; public int age() { return 2011 - birthyear; public String getname() { return name; public abstract String noise(); public boolean eatspeople() { return this instanceof Dangerous; public int compareto(animal other) { return getname().compareto(other.getname()); public boolean equals(object obj) { if (obj == null) { return false; if (getclass()!= obj.getclass()) { return false; final AbstractAnimal other = (AbstractAnimal) obj; if ((this.name == null)? (other.name!= null) :!this.name.equals(other.name)) { return false; return true;

COE318 Software Systems Lecture Notes: Week 8 13 of 17 public int hashcode() { int hash = 7; hash = 83 * hash + (this.name!= null? this.name.hashcode() : 0); return hash; public interface Dangerous { public class Lion extends AbstractAnimal implements Dangerous{ public Lion(String name, int year) { super(name, year); public String noise() { return "GGGrowl"; public class Hedgehog extends AbstractAnimal { public Hedgehog(String name, int year) { super(name, year);

COE318 Software Systems Lecture Notes: Week 8 14 of 17 public String noise() { return "Chirp chirp"; import java.util.arraylist; public class Zoo { private final ArrayList<Animal> animals = new ArrayList<Animal>(); public void add(animal animal) { animals.add(animal); public String getnames() { String s = ""; for (Animal a : animals) { s += a.getname() + "\n"; return s; public Animal[] getanimals() { Animal[] ts = new AbstractAnimal[0]; return animals.toarray(ts); public static void main(string[] args) { Zoo zoo = new Zoo(); Animal a = new Lion("Alex", 2010); zoo.add(a); zoo.add(new Lion("Betty", 2009)); zoo.add(new Hedgehog("Charlie", 2010)); System.out.println(zoo.getNames()); Animal[] animals = zoo.getanimals(); for (Animal an : animals) { System.out.println(an.getClass().getName() + " " + an.getname() + ": " + (an.eatspeople()? "eats" : "does not eat")

COE318 Software Systems Lecture Notes: Week 8 15 of 17 + " people"); Zoo Version 5: Exceptions import java.util.arraylist; public class Zoo { private final ArrayList<Animal> animals = new ArrayList<Animal>(); public void add(animal animal) { if (animals.contains(animal)) { throw new IllegalArgumentException("Duplicate name not allowed"); animals.add(animal); public String getnames() { String s = ""; for (Animal a : animals) { s += a.getname() + "\n"; return s; public Animal[] getanimals() { Animal[] ts = new AbstractAnimal[0]; return animals.toarray(ts); public static void main(string[] args) {

COE318 Software Systems Lecture Notes: Week 8 16 of 17 Zoo zoo = new Zoo(); Animal a = new Lion("Alex", 2010); zoo.add(a); zoo.add(new Lion("Betty", 2009)); Animal charlie1 = new Hedgehog("Charlie", 2010); zoo.add(charlie1); System.out.println(zoo.getNames()); Animal charlie2 = new Lion("Charlie", 2007); try { zoo.add(charlie2); catch (IllegalArgumentException ex) { System.err.println("Duplicate name not added"); Animal[] animals = zoo.getanimals(); for (Animal an : animals) { System.out.println(an.getClass().getName() + " " + an.getname() + ": " + (an.eatspeople()? "eats" : "does not eat") + " people"); Unit testing and Junit Unit testing (widely used in the software industry) allows you to write test methods independently for the implementation code. Netbeans supports a framefork for doing this with a prodcut called Junit. (We use version 4.) Test methods are annotated with @Test and must be public void. Suppose we have a class called Resistor (as in Lab 1). We could write a test method to verify that getvoltage() worked:

COE318 Software Systems Lecture Notes: Week 8 17 of 17 Questions @Test public void getvoltage() { Resistor r = new Resistor(10); r.setcurrent(30.); assertequals(300.0, r.getvoltage()); 1. Write an equals(object ob) for the ComplexNumber class. Answers 1. Just: public boolean equals(object ob) { if (ob == null) return false; if (!(ob instanceof ComplexNumber)) return false; ComplexNumber c = (ComplexNumber) ob; return (c.getreal() == getreal()) && (c.getimag() == getimag());