Chapter 11 Classes Continued

Similar documents
Chapter 10 Classes Continued. Fundamentals of Java

APCS Java Lesson 10: Classes Continued Modifications by Mr. Dave Clausen Updated for Java 1.5

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

OBJECT ORİENTATİON ENCAPSULATİON

Inheritance, Polymorphism, and Interfaces

Object Oriented Programming. Java-Lecture 11 Polymorphism

Programming II (CS300)

CMSC 132: Object-Oriented Programming II

CS-202 Introduction to Object Oriented Programming

Polymorphism and Interfaces. CGS 3416 Spring 2018

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

Inheritance and Interfaces

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

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

Java Object Oriented Design. CSC207 Fall 2014

8. Polymorphism and Inheritance

CMSC 132: Object-Oriented Programming II

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Inheritance and Polymorphism

Programming II (CS300)

First IS-A Relationship: Inheritance

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

More Relationships Between Classes

Polymorphism and Inheritance

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

What is Inheritance?

More on Objects in JAVA TM

Chapter 6 Introduction to Defining Classes

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

COP 3330 Final Exam Review

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

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

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

Inheritance. Inheritance

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

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

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

Declarations and Access Control SCJP tips

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

EXCEPTION HANDLING. Summer 2018

1 Shyam sir JAVA Notes

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

Chapter 14 Abstract Classes and Interfaces

Full file at Chapter 2 - Inheritance and Exception Handling

Inheritance and object compatibility

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

More on Exception Handling

CH. 2 OBJECT-ORIENTED PROGRAMMING

More on Exception Handling

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

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

Inheritance -- Introduction

ITI Introduction to Computing II

Chapter 5 Object-Oriented Programming

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

Inheritance (Outsource: )

ITI Introduction to Computing II

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Inheritance. Transitivity

CS 520 Theory and Practice of Software Engineering Fall 2018

Subclasses, Superclasses, and Inheritance

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

CS313D: ADVANCED PROGRAMMING LANGUAGE

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

CS 11 java track: lecture 3

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Inheritance and Polymorphism

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Classes and Inheritance Extending Classes, Chapter 5.2

Object Oriented Java

Java How to Program, 8/e

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Inheritance. The Java Platform Class Hierarchy

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

Definition of DJ (Diminished Java)

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

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

CS111: PROGRAMMING LANGUAGE II

CS 251 Intermediate Programming Inheritance

Inheritance & Polymorphism

Lecture 4: Extending Classes. Concept

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Self-review Questions

Polymorphism: Inheritance Interfaces

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

A Java Execution Simulator

[ L5P1] Object-Oriented Programming: Advanced Concepts

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

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

More On inheritance. What you can do in subclass regarding methods:

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

Inheritance (continued) Inheritance

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Programming II (CS300)

Classes, interfaces, & documentation. Review of basic building blocks

Transcription:

Chapter 11 Classes Continued The real power of object-oriented programming comes from its capacity to reduce code and to distribute responsibilities for such things as error handling in a software system. Important concepts of this chapter o Static Variables and Methods: When information needs to be shared among all instances of a class, that information can be represented in terms of static variables and it can be accessed by means of static method. o Interfaces: A Java interface specifies the set of methods available to clients of a class. An interface provides a way of requiring a class to implement a set of methods and a way of informing clients about services regardless of implementation details. Interfaces thus provide the glue that holds a set of cooperating classes together. o Inheritance: Java organizes classes in a hierarchy. Classes inherit the instance variables and methods of the classes above them in the hierarchy. A class can extend its inherited characteristics by adding instance variables and methods and by overriding inherited methods. Thus inheritance provides a mechanism for reusing code and can greatly reduce the effort required to implement a new class. o Abstract Classes: Some classes in a hierarchy must never be instantiated. They are called abstract classes. Their sole purpose is to define features and behavior common to their subclasses. o Polymorphism: Methods in different classes with a similar function are usually given the same name. This is called polymorphism. Polymorphism makes classes easier to use because programmers need to memorize fewer method names. In a well-designed class hierarchy, polymorphism is employed as much as possible. A good example of a polymorphic message is tostring. Every object, no matter which class it belongs to, understands the tostring message and responds by returning a string that describes the object. o Preconditions and Postconditions: Clients need to know how to use a method correctly and what results to expect if it is so used. Preconditions specify the correct use of a method and postconditions describe what will result if the preconditions are satisfied.

o Exceptions for Error Handling: When a method's preconditions are violated, a foolproof way to catch the errors is to throw exceptions, thereby halting program execution at the point of the errors. o Reference Types: The identity of an object and the fact that there can be multiple references to the same object are issues that arise when comparing two objects for equality and when copying an object. There are also subtle rules to master when manipulating objects of different but related types in a hierarchy. Instance Variables: belongs to object and storage is allocated when object created. Each object has own set of instance variables, activated when a message is sent to object. Instance Method: is activated when a message is sent to an object. Class Variable: belongs to a class. Its storage is allocated at program start-up and is independent of the number of instances created. Class Method: is activated when a message is sent to the class rather than to an object The modifier static is used to designate class variables and methods. We use a static variable in any situation in which all instances share a common data value. We then use static methods to provide public access to these data. By using the modifier final in conjunction with static, we create a class constant. Value is assigned when variable is declared and cannot change. Rules for using static Variables 1) Class methods can reference only static variables, and never the instance variables. 2) Instance methods can reference static and instance variables. Rather than decompose a problem into a system of cooperating static methods, it is much better to decompose it into a system of cooperating classes. With the later approach, static main usually is limited to the small but essential role of starting everything off.

In Turtle Graphics a pen is initially: o In the center of a graphics window (0, 0) o In the down position o Pointing north To use Turtle Graphics remember to: o import TurtleGraphics.StandardPen; o Standard pen = new StandardPen(); // Or other type pen Interface is used in two ways: 1) Part of a software system that interacts with human users 2) It is a list of a class's public methods. (We will talk about this one) A class's interface provides the information needed to use a class without revealing anything about its implementation. Types of Pens (all have same general behavior and respond to same messages) o StandardPen o WigglePen o RainbowPen

// Pen.java: The behavior common to all types of pens import java.awt.color public interface Pen { public String } down(); drawstring(string text); home(); move(double distance); move(double x, double y); setcolor(color color); setdirection(double direction); setwidth(int width); tostring(); turn(double degrees); up(); Interface only contains the signatures of the methods followed by semicolon. It provides programmers the information need to use pens of any type correctly. Interface is not a class; however, when a class is defined there is a mechanism, to specify that the class conforms to the interface. ** Look at code in DifferentPens that puts drawing of a square in a method. Using an interface name has two benefits: 1. Methods that use interface types are more general, in that they work with any classes that implement the interface. 2. It s easier to maintain a program that uses interface types. If you want to modify the program to use a different type of pen, for example, you only have to change the code that instantiates the pen. The code that manipulates the pen does not have to change at all. See page 394 and interface Shape public class Circle implements Shape { } public class Rect implements Shape { }

The key feature is the phrase implements Shape. The presence of this phrase implies that: Both classes implement all the methods listed in the Shape interface. A variable declared as Shape can be associated with an object of either class. Look at code page 395-397 Final Observations on Interface An interface contains only methods, never variables. The methods in an interface are usually public. If more than one class implements an interface, its methods are polymorphic. A class can implement methods in addition to those listed in the interface, as we will illustrate soon. A class can implement more than one interface, a feature that we are not going to explore. Interfaces can be organized in an inheritance hierarchy, another feature we are not going to explore. Code reuse through inheritance All classes part of immense hierarchy, with class Object at the root. Each class inherits the characteristics (variables and methods) of the classes above it in the hierarchy. A class can add new variables to these inherited characteristics as needed. It also can add new methods and/or modifies inherited methods. Root top position in an upside down tree (Object class) Subclasses are below the Object class (subclasses extend Object) Superclass class immediately above another A class can have many subclasses, and all classes, except Object have exactly one superclass. Descendants of a class consists of its subclasses, plus their subclasses, and so on. See Wheel example. Wheel v1 = new Wheel(); Shape v2 = new Wheel(); v1.setspokes(6); v2.setspokes(6); ******error*****

((Wheel)v2).setSpokes(6); ***cast to wheel before use method*** Arrays of Objects Shapes[] shapes = new Shape[10]; shapes[0] = new Rect(20,20,40,40); shapes[1] = new Circle(100,100,20); shapes[2] = new Wheel(200,200,20,6); Pen pen = new StandardPen(); for (int i = 0; i < 3; i++) shapes[i].draw(pen); setspokes(n)???????? for(int i = 0; i < shapes.length; i++) if (shapes[i] instanceof Wheel) ((Wheel)shapes[i]).setSpokes(5); Important facts about objects in arrays: 1. When an element type of an array is a reference type or interface, objects of those types or any subtype (subclass or implementing class) can be directly inserted into the array. 2. After accessing an object in an array, care must be taken to send it the appropriate messages or to cast it down to a type that can receive the appropriate messages. 11.7 Inheritance and Abstract Classes The Circle and Rect classes duplicate code. Inheritance provides ways to reduce duplication. We define a new class that is a superclass of Circle and Rect and contains all the variables and methods common to both. We will never instantiate this class, so we will make it an abstract class, that is, a class that cannot be instantiated. The classes that extend this class and that are instantiated are called concrete classes. Call the class AbstractShape and have it implement the Shape interface. Then, the subclasses of Circle and Rect no longer need to implement Shape explicitly. Since AbstractShape implements Shape, it must include all the Shape methods, even those that are completely different in the subclasses and share no code (like area). Methods in AbstractShape such as area for which we cannot write any code are

called abstract methods, and we indicate that fact by including the word abstract in their headers. See code pages 407-408 A final method is a method that cannot be overridden by a subclass. Some Observations about Interfaces and Inheritance A Java interface has a name and consists of a list of method headers. One or more classes can implement the same interface. If a variable is declared to be of an interface type, then it can be associated with an object from any class that implements the interface. If a class implements an interface, then all its subclasses do so implicitly. A subclass inherits all the characteristics of its superclass. To this basis, the subclass can add new variables and methods or modify inherited methods. Characteristics common to several classes can be collected in a common abstract superclass that is never instantiated.

An abstract class can contain headers for abstract methods that are implemented in the subclasses. A class's constructors and methods can utilize constructors and methods in the superclass. Inheritance reduces repetition and promotes the reuse of code. Interfaces and inheritance promote the use of polymorphism. There are four ways in which methods in a subclass can be related to methods in a superclass: 1. Implementation of an abstract method: As we have seen, each subclass is forced to implement the abstract methods specified in its superclass. Abstract methods are thus a means of requiring certain behavior in all subclasses. 2. Extension: There are two kinds of extension: a. The subclass method does not exist in the superclass. b. The subclass method invokes the same method in the superclass and also extends the superclass's behavior with its own operations. 3. Overriding: In the case of overriding, the subclass method does not invoke the superclass method. Instead, the subclass method is intended as a complete replacement of the superclass method. 4. Finality: The method in the superclass is complete and cannot be modified by the subclasses. We declare such a method to be final. Page 413 (Working without interfaces) So should you or shouldn't you use interfaces? This is not really a question of vital importance in an introductory programming class where all the programs are relatively short and simple: however, the prevailing wisdom suggests that we use hierarchies of interfaces to organize behavior and hierarchies of classes to maximize code reuse. Justifying this advice is beyond this book's scope.

Relationships among Classes 1. An object of one class can send a message to an object of another class. For example, a Circle object sends a message to a StandardPen object to draw a shape. In this case, the sender object s class depends on the receiver object s class, and their relationship is called dependency. 2. An object of one class can contain objects of another class as structural components. For example, the TestScoresModel object contains Student objects. The relationship between a container class and the classes of the objects it contains is called aggregation or the has-a relationship. 3. An object s class can be a subclass of a more general class. For example, the Wheel class is a subclass of Circle, which in turn is a subclass of AbstractShape. These classes are related by inheritance, or the is-a relationship. UML diagrams

Acceptable Classes for parameters & return types: In any situation in which an object of a superclass is expected, it is always acceptable to substitute an object of a subclass, but never a superclass AbstractShape A; Circle B; Rect C; A = new Circle(); B = new AbstractShape(); C = new AbstractShape(); A = new Rect(); B = new Rect(); C = new Circle(); A = new Wheel(); B = new Wheel(); C = new Wheel(); Wheel D; D = new AbstractShape(); D = new Circle(); D = new Rect(); Object (references) can be passes to/from methods Changes made to objects in methods persist after method. An object returned by method is usually created in method. It continues to exist after method stops executing. Preconditions describe the expected values of a parameters and instance variable that the method is about to use. Postconditions describe the return value and any changes made to instance variables. Preconditions and Postconditions are written as comments placed immediately before a method s header. /* * Precondition: 1 <= i< =number of scores * Precondition: 0 <= score <= 100 * Postcondition: test score at position i is set to score * Returns: true if the preconditions are satisfied or false otherwise */ public boolean setscore(int i, int score) { if (i < 1 i > tests.length score < 0 score > 100) return false; tests[i 1] = score; return true; }

Commonly used exceptions classes in package java.lang Exception RuntimeException ArithmeticException IllegalArgumentException IllegalStateException IndexOutOfBoundsException StringIndexOutOfBoundsException ArrayIndexOutOfBoundsException NullPointerException UnsupportedOperationException throw new <exception class>(<a string>); Could use to enforce preconditions. if (number < 0) throw new RuntimeException( Number should be nonnegative); ***See student example Could use try catch so exceptions do not halt execution of program (See page 421-422) Show documentation using Bluej /** */ @param @return @throws 11.12: Reference types, Equality, and Object Identity Alias (aliasing) a situation in which two or more names in a program can refer to the same location. This happens when a programmer assigns one object variable to another. Comparing Objects for Equality 1. Use equality operator == 2. Use instance method equals (in object class defined and uses == by default. May be overridden in other classes like tostring)

String str = reader.nextline( ); System.out.println(str == "Java"); System.out.println(str.equals("Java")); The objects referenced by the variable str and the literal "Java" are two different string objects in memory, even though the characters they contain might be the same. The first string object was created in the keyboard reader during user input; the second string object was created internally within the program. The operator == compares the references to the objects, not the contents of the objects. Thus, if the two references do not point to the same object in memory, == returns false. Because the two strings in our example are not the same object in memory, == returns false. The method equals returns true, even when two strings are not the same object in memory, if their characters happen to be the same. If at least one pair of characters fails to match, the method equals returns false. A corollary of these facts is that the operator!= can return true for two strings, even though the method equals returns true. To summarize, == tests for object identity, whereas equals tests for structural similarity as defined by the implementing class. Write own equal class for other objects do not use ==. //Compare two students for equality public Boolean equals (Object other) { if (this == other) //test for identity return true; if (!(Other instanceof Student)) // test for Student return false; Student s = (Student)other; //cast to student Return name.equals(s.getname( )); //compare the names }

Copying Objects Student s1, s2; s1 = new Student("Mary", 100, 80, 75); s2 = s1; s1 and s2 refer to the same object When clients of a class might copy objects, there is a standard way of providing a method to do this. The class implements the Java interface Cloneable. This interface authorizes the method clone, which is defined in the Object class, to construct a copy of the object. Student s1, s2; s1 = new Student("Mary", 100, 80, 75); s2 = s1.clone( );